|
| 1 | +//! # Bridge Repair |
| 2 | +//! |
| 3 | +//! The equations can be validated using a recursive solution that checks all possible combinations |
| 4 | +//! of operators. However the number of states to check grows exponentially as 2n in part one |
| 5 | +//! and 3n in part two. |
| 6 | +//! |
| 7 | +//! As much faster approach works in reverse from the end of the equation to prune as many states |
| 8 | +//! as possible by checking which operations are possible. For example: |
| 9 | +//! |
| 10 | +//! ```none |
| 11 | +//! Test Value: 123456 |
| 12 | +//! Equation: 2 617 56 |
| 13 | +//! Addition is possible as 123456 >= 56 |
| 14 | +//! Multiplication is not possible as 56 is not a factor of 123456 |
| 15 | +//! Concatenation is possible as 1234 || 56 = 123456 |
| 16 | +//! ``` |
| 17 | +//! |
| 18 | +//! Following the concatenation branch and applying an inverse concentation |
| 19 | +//! (the full solution would also follow the addition branch): |
| 20 | +//! |
| 21 | +//! ```none |
| 22 | +//! Test Value: 1234 |
| 23 | +//! Equation: 2 617 |
| 24 | +//! Addition is possible as 1234 >= 617 |
| 25 | +//! Multiplication is possible as 2 * 617 = 1234 |
| 26 | +//! Concatenation is not possible as 1234 does not end in 617 |
| 27 | +//! ``` |
| 28 | +//! |
| 29 | +//! Following the multiplication branch: |
| 30 | +//! |
| 31 | +//! ```none |
| 32 | +//! Test Value: 2 |
| 33 | +//! Equation: 2 |
| 34 | +//! Addition is possible |
| 35 | +//! Multiplication is possible |
| 36 | +//! Concatenation is possible |
| 37 | +//! ``` |
| 38 | +//! |
| 39 | +//! Following the addition or concatentation branches results in a test value of 0 which means |
| 40 | +//! that all terms have been applied successfully and the equation is valid. |
| 41 | +//! |
| 42 | +//! Inverse concenation can be implemented without time consuming conversion to or from |
| 43 | +//! strings by dividing the left term by the next power of ten greater than the right term. |
| 44 | +//! For example: |
| 45 | +//! |
| 46 | +//! * 7 || 9 => 79 => 79 / 10 => 7 |
| 47 | +//! * 12 || 34 => 1234 => 1234 / 100 => 12 |
| 48 | +//! * 123 || 789 => 123789 => 123789 / 1000 => 123 |
| 49 | +use crate::util::parse::*; |
| 50 | + |
| 51 | +type Input = (u64, u64); |
| 52 | + |
| 53 | +pub fn parse(input: &str) -> Input { |
| 54 | + let mut equation = Vec::new(); |
| 55 | + let mut part_one = 0; |
| 56 | + let mut part_two = 0; |
| 57 | + |
| 58 | + for line in input.lines() { |
| 59 | + equation.extend(line.iter_unsigned::<u64>()); |
| 60 | + |
| 61 | + // If an equation is valid for part one then it's also valid for part two. |
| 62 | + if valid(&equation, equation[0], equation.len() - 1, false) { |
| 63 | + part_one += equation[0]; |
| 64 | + part_two += equation[0]; |
| 65 | + } else if valid(&equation, equation[0], equation.len() - 1, true) { |
| 66 | + part_two += equation[0]; |
| 67 | + } |
| 68 | + |
| 69 | + equation.clear(); |
| 70 | + } |
| 71 | + |
| 72 | + (part_one, part_two) |
| 73 | +} |
| 74 | + |
| 75 | +pub fn part1(input: &Input) -> u64 { |
| 76 | + input.0 |
| 77 | +} |
| 78 | + |
| 79 | +pub fn part2(input: &Input) -> u64 { |
| 80 | + input.1 |
| 81 | +} |
| 82 | + |
| 83 | +fn valid(terms: &[u64], test_value: u64, index: usize, concat: bool) -> bool { |
| 84 | + (test_value == 0) |
| 85 | + || (concat |
| 86 | + && test_value % next_power_of_ten(terms[index]) == terms[index] |
| 87 | + && valid(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat)) |
| 88 | + || (test_value % terms[index] == 0 |
| 89 | + && valid(terms, test_value / terms[index], index - 1, concat)) |
| 90 | + || (test_value >= terms[index] |
| 91 | + && valid(terms, test_value - terms[index], index - 1, concat)) |
| 92 | +} |
| 93 | + |
| 94 | +fn next_power_of_ten(n: u64) -> u64 { |
| 95 | + let mut power = 10; |
| 96 | + |
| 97 | + while power <= n { |
| 98 | + power *= 10; |
| 99 | + } |
| 100 | + |
| 101 | + power |
| 102 | +} |
0 commit comments