Skip to content

Commit 2c2de67

Browse files
committed
11th day
1 parent 8085770 commit 2c2de67

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

data/examples/11.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
125 17

src/bin/11.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
advent_of_code::solution!(11);
2+
3+
use advent_of_code::majcn::math::*;
4+
use advent_of_code::maneatingape::hash::*;
5+
use advent_of_code::maneatingape::parse::*;
6+
7+
fn parse_data(input: &str) -> Vec<u32> {
8+
input.iter_unsigned().collect()
9+
}
10+
11+
fn calculate_step(v: u64) -> [Option<u64>; 2] {
12+
if v == 0 {
13+
return [Some(1), None];
14+
}
15+
16+
let n = v.count_digits() as u32;
17+
if n % 2 == 0 {
18+
let split_value = 10_u64.pow(n / 2);
19+
return [Some(v / split_value), Some(v % split_value)];
20+
}
21+
22+
[Some(v * 2024), None]
23+
}
24+
25+
fn calculate<const N: usize>(v: u64, i: usize, cache: &mut FastMap<(usize, u64), u64>) -> u64 {
26+
if i == N {
27+
return 1;
28+
}
29+
30+
if let Some(cached_result) = cache.get(&(i, v)) {
31+
return *cached_result;
32+
}
33+
34+
let result = calculate_step(v)
35+
.into_iter()
36+
.filter_map(|x| Some(calculate::<N>(x?, i + 1, cache)))
37+
.sum();
38+
39+
cache.insert((i, v), result);
40+
41+
result
42+
}
43+
44+
fn part_x<const N: usize>(data: Vec<u32>) -> u64 {
45+
let mut cache = FastMap::new();
46+
47+
data.into_iter()
48+
.map(|x| calculate::<N>(x as u64, 0, &mut cache))
49+
.sum()
50+
}
51+
52+
pub fn part_one(input: &str) -> Option<u64> {
53+
let data = parse_data(input);
54+
55+
let result = part_x::<25>(data);
56+
57+
Some(result)
58+
}
59+
60+
pub fn part_two(input: &str) -> Option<u64> {
61+
let data = parse_data(input);
62+
63+
let result = part_x::<75>(data);
64+
65+
Some(result)
66+
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn test_part_one() {
74+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
75+
assert_eq!(result, Some(55312));
76+
}
77+
78+
#[test]
79+
fn test_part_two() {
80+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
81+
assert_eq!(result, Some(65601038650482));
82+
}
83+
}

0 commit comments

Comments
 (0)