retroactive, to derust my rust

day7 part2

+94 -3
+4
input/day7_custom.test.txt
··· 1 + 12345: 12 34 5 2 + 12034050: 120 340 50 3 + 15: 1 2 3 4 5 4 + 120: 1 2 3 4 5
+77 -3
src/day7.rs
··· 1 1 pub fn day7_part1(input: &str) -> String { 2 2 let input = parse(input); 3 - let sum: u64 = input.into_iter().filter(|(res, operands)| possible(*res, operands)).map(|(res, _)| res).sum(); 3 + let sum: u64 = input 4 + .into_iter() 5 + .filter(|(res, operands)| possible(*res, operands)) 6 + .map(|(res, _)| res) 7 + .sum(); 4 8 sum.to_string() 5 9 } 6 10 11 + pub fn day7_part2(input: &str) -> String { 12 + let input = parse(input); 13 + let sum: u64 = input 14 + .into_iter() 15 + .filter(|(res, operands)| possible_with_concatenation(*res, operands)) 16 + .map(|(res, _)| res) 17 + .sum(); 18 + sum.to_string() 19 + } 20 + 21 + fn possible_with_concatenation(res: u64, operands: &[u64]) -> bool { 22 + let operator_count = operands.len() - 1; 23 + let possibilities = 0..(3u64.pow(operator_count as u32)); 24 + for possibility in possibilities { 25 + if evaluate_with_concatenation(possibility, operands) == res { 26 + return true; 27 + } 28 + } 29 + dbg!(res); 30 + false 31 + } 32 + 33 + fn evaluate_with_concatenation(mut bitpattern: u64, operands: &[u64]) -> u64 { 34 + // dbg!(bitpattern, operands); 35 + let selected_option = bitpattern % 3; 36 + let mut result = if selected_option == 0 { 37 + operands[0] + operands[1] 38 + } else if selected_option == 1 { 39 + operands[0] * operands[1] 40 + } else { 41 + concatenate(operands[0], operands[1]) 42 + }; 43 + 44 + let mut debug_operators = vec![]; 45 + 46 + for operator in 2..operands.len() { 47 + bitpattern /= 3; 48 + let selected_option = bitpattern % 3; 49 + 50 + result = if selected_option == 0 { 51 + result + operands[operator] 52 + } else if selected_option == 1 { 53 + result * operands[operator] 54 + } else { 55 + concatenate(result, operands[operator]) 56 + }; 57 + 58 + debug_operators.push(if selected_option == 0 { 59 + '+' 60 + } else if selected_option == 1 { 61 + '*' 62 + } else { 63 + '|' 64 + }); 65 + } 66 + 67 + // dbg!(result, bitpattern, debug_operators); 68 + result 69 + } 70 + 71 + fn concatenate(left: u64, right: u64) -> u64 { 72 + let decimal_places_shift = (right as f64).log10().ceil() as u32; 73 + left * 10u64.pow(decimal_places_shift) + right 74 + } 75 + 76 + #[test] 77 + fn test_concatenate() { 78 + assert_eq!(concatenate(15, 6), 156) 79 + } 80 + 7 81 fn possible(res: u64, operands: &[u64]) -> bool { 8 82 // dbg!(res, operands); 9 83 let operator_count = operands.len() - 1; ··· 13 87 return true; 14 88 } 15 89 } 16 - 90 + 17 91 false 18 92 } 19 93 ··· 41 115 #[derive(Copy, Clone, Eq, PartialEq, Debug)] 42 116 enum Operator { 43 117 Times, 44 - Plus 118 + Plus, 45 119 } 46 120 47 121 fn parse(input: &str) -> Vec<(u64, Vec<u64>)> {
+13
src/lib.rs
··· 356 356 assert_eq!(result, "3598800864292"); 357 357 } 358 358 359 + // #[ignore] 360 + #[test] 361 + fn day7_part2_test() { 362 + // let simple_result = day7::day7_part2(include_str!("../input/day7_custom.test.txt")); 363 + // assert_eq!(simple_result, (12345 + 12034050 + 15 + 120).to_string()); 364 + 365 + // let test_result = day7::day7_part2(include_str!("../input/day7.test.txt")); 366 + // assert_eq!(test_result, "11387"); 367 + let result = day7::day7_part2(include_str!("../input/day7.txt")); 368 + let parsed_result: u64 = result.parse::<u64>().unwrap(); 369 + assert!(parsed_result > 318_910_516_761_637, "{parsed_result} was too low"); 370 + } 371 + 359 372 #[test] 360 373 fn day6_part1_test() { 361 374 let test_result = day6::day6_part1(include_str!("../input/day6.test.txt"));