- commit
- bce59773e45df8012ff7ed4fd588f1c1214dfa07
- parent
- f31c202b7a601a53f261ded29cc0e4b4d88dcc90
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-12-12 18:59
2024-12-11
Diffstat
A | 2024/11/input.txt | 1 | + |
A | 2024/11/solution.rs | 69 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/11/test.txt | 1 | + |
3 files changed, 71 insertions, 0 deletions
diff --git a/2024/11/input.txt b/2024/11/input.txt
@@ -0,0 +1 @@ -1 1 5 62914 65 972 0 805922 6521 1639064
diff --git a/2024/11/solution.rs b/2024/11/solution.rs
@@ -0,0 +1,69 @@ -1 1 use std::collections::HashMap; -1 2 -1 3 #[path = "../lib.rs"] -1 4 mod lib; -1 5 -1 6 fn parse_input() -> Vec<u64> { -1 7 let line = lib::iter_input().next().unwrap(); -1 8 return line.split(' ').map(|s| s.parse::<u64>().unwrap()).collect(); -1 9 } -1 10 -1 11 fn count_digits(value: u64) -> usize { -1 12 let mut count = 0; -1 13 let mut v = value; -1 14 while v > 0 { -1 15 v /= 10; -1 16 count += 1; -1 17 } -1 18 return count; -1 19 } -1 20 -1 21 fn pow(base: u64, exp: usize) -> u64 { -1 22 if exp == 1 { -1 23 return base; -1 24 } else { -1 25 let p = pow(base, exp / 2); -1 26 if exp % 2 == 0 { -1 27 return p * p; -1 28 } else { -1 29 return p * p * base; -1 30 } -1 31 } -1 32 } -1 33 -1 34 fn next(value: u64, blinks: usize, cache: &mut HashMap<(u64, usize), usize>) -> usize { -1 35 let solution; -1 36 if blinks == 0 { -1 37 return 1; -1 38 } else if let Some(s) = cache.get(&(value, blinks)) { -1 39 return *s; -1 40 } else if value == 0 { -1 41 solution = next(1, blinks - 1, cache); -1 42 } else { -1 43 let digits = count_digits(value); -1 44 if digits % 2 == 0 { -1 45 let k = pow(10, digits / 2); -1 46 solution = next(value / k, blinks - 1, cache) + next(value % k, blinks - 1, cache); -1 47 } else { -1 48 solution = next(value * 2024, blinks - 1, cache); -1 49 } -1 50 } -1 51 -1 52 cache.insert((value, blinks), solution); -1 53 return solution; -1 54 } -1 55 -1 56 fn main() { -1 57 let values = parse_input(); -1 58 let mut sum1 = 0; -1 59 let mut sum2 = 0; -1 60 let mut cache = HashMap::new(); -1 61 -1 62 for value in values.iter() { -1 63 sum1 += next(*value, 25, &mut cache); -1 64 sum2 += next(*value, 75, &mut cache); -1 65 } -1 66 -1 67 println!("part1: {}", sum1); -1 68 println!("part2: {}", sum2); -1 69 }
diff --git a/2024/11/test.txt b/2024/11/test.txt
@@ -0,0 +1 @@ -1 1 125 17