use std::collections::HashMap; #[path = "../lib.rs"] mod lib; fn parse_input() -> Vec { let line = lib::iter_input().next().unwrap(); return line.split(' ').map(|s| s.parse::().unwrap()).collect(); } fn count_digits(value: u64) -> usize { let mut count = 0; let mut v = value; while v > 0 { v /= 10; count += 1; } return count; } fn pow(base: u64, exp: usize) -> u64 { if exp == 1 { return base; } else { let p = pow(base, exp / 2); if exp % 2 == 0 { return p * p; } else { return p * p * base; } } } fn next(value: u64, blinks: usize, cache: &mut HashMap<(u64, usize), usize>) -> usize { let solution; if blinks == 0 { return 1; } else if let Some(s) = cache.get(&(value, blinks)) { return *s; } else if value == 0 { solution = next(1, blinks - 1, cache); } else { let digits = count_digits(value); if digits % 2 == 0 { let k = pow(10, digits / 2); solution = next(value / k, blinks - 1, cache) + next(value % k, blinks - 1, cache); } else { solution = next(value * 2024, blinks - 1, cache); } } cache.insert((value, blinks), solution); return solution; } fn main() { let values = parse_input(); let mut sum1 = 0; let mut sum2 = 0; let mut cache = HashMap::new(); for value in values.iter() { sum1 += next(*value, 25, &mut cache); sum2 += next(*value, 75, &mut cache); } println!("part1: {}", sum1); println!("part2: {}", sum2); }