diff --git a/src/day2.rs b/src/day2.rs index c3d0508..b3510d7 100644 --- a/src/day2.rs +++ b/src/day2.rs @@ -88,14 +88,22 @@ fn part2_arithmetic_brute(input: &[RangeInclusive]) -> u64 { let n_digits = product.ilog10() + 1; for n in 1..=n_digits / 2 { + let repetitions = n_digits / n; if n_digits % n != 0 { continue; } - let decade = 10u64.pow(n); + let decade = 10u64.pow(n_digits - n); let lhs = product / decade; - let rhs = product % decade; - if lhs == rhs { - invalid_sum += product + let remainder = product % decade; + + // for each repetition we multiply by 10^(rep * n) + let expected_remainder = (0..repetitions - 1) + .map(|rep| lhs * 10u64.pow(rep * n)) + .sum(); + + if remainder == expected_remainder { + invalid_sum += product; + break; } } } @@ -155,7 +163,7 @@ mod tests { } #[test] fn part2_arithmetic_example() { - assert_eq!(part2_arithmetic_brute(&parse(EXAMPLE)), 1227775554); + assert_eq!(part2_arithmetic_brute(&parse(EXAMPLE)), 4174379265); } #[test] fn part1_clever_example() {