|
| 1 | +use rayon::iter::ParallelIterator; |
| 2 | +use itertools::Itertools; |
| 3 | +use rayon::prelude::ParallelString; |
| 4 | + |
| 5 | +advent_of_code::solution!(7); |
| 6 | + |
| 7 | +fn parse_and_solve<F>(input: &str, equation_checker: F) -> u64 |
| 8 | + where F: Fn(&u64, &[u64], &u64) -> bool + Sync |
| 9 | +{ |
| 10 | + input.par_lines() |
| 11 | + .map(|line| { |
| 12 | + let mut split = line.split_whitespace(); |
| 13 | + let Some(expected) = split.next() else { panic!() }; |
| 14 | + let expected = expected[0..expected.len() - 1].parse::<u64>().unwrap(); |
| 15 | + let terms = split.map(|num| num.parse::<u64>().unwrap()).collect_vec(); |
| 16 | + (terms, expected) |
| 17 | + }) |
| 18 | + .filter_map(|(terms, expected)| { |
| 19 | + if equation_checker(&terms[0], &terms[1..], &expected) { Some(expected) } else { None } |
| 20 | + }) |
| 21 | + .sum() |
| 22 | +} |
| 23 | + |
| 24 | +fn is_valid_equation(current_term: &u64, terms: &[u64], expected: &u64) -> bool { |
| 25 | + if terms.is_empty() { |
| 26 | + return current_term == expected; |
| 27 | + } |
| 28 | + |
| 29 | + let next_term = terms[0]; |
| 30 | + let new_terms = &terms[1..]; |
| 31 | + [current_term * next_term, current_term + next_term].iter() |
| 32 | + .filter(|term| *term <= expected) |
| 33 | + .any(|term| is_valid_equation(term, new_terms, expected)) |
| 34 | +} |
| 35 | + |
| 36 | +fn is_valid_equation_with_concat(current_term: &u64, terms: &[u64], expected: &u64) -> bool { |
| 37 | + if terms.is_empty() { |
| 38 | + return current_term == expected; |
| 39 | + } |
| 40 | + |
| 41 | + let next_term = terms[0]; |
| 42 | + let new_terms = &terms[1..]; |
| 43 | + [ |
| 44 | + current_term * next_term, |
| 45 | + current_term + next_term, |
| 46 | + (current_term.to_string() + &next_term.to_string()).parse().unwrap(), |
| 47 | + ].iter() |
| 48 | + .filter(|term| *term <= expected) |
| 49 | + .any(|term| is_valid_equation_with_concat(term, new_terms, expected)) |
| 50 | +} |
| 51 | + |
| 52 | +pub fn part_one(input: &str) -> Option<u64> { |
| 53 | + Some(parse_and_solve(input, is_valid_equation)) |
| 54 | +} |
| 55 | + |
| 56 | +pub fn part_two(input: &str) -> Option<u64> { |
| 57 | + Some(parse_and_solve(input, is_valid_equation_with_concat)) |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::*; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_part_one() { |
| 66 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 67 | + assert_eq!(result, Some(3749)); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_part_two() { |
| 72 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 73 | + assert_eq!(result, Some(11387)); |
| 74 | + } |
| 75 | +} |
0 commit comments