Skip to content

Commit ca4a6e2

Browse files
committed
Year 2024 Day 11
1 parent b3d91b2 commit ca4a6e2

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8282
| 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Source](src/year2024/day08.rs) | 8 |
8383
| 9 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | [Source](src/year2024/day09.rs) | 106 |
8484
| 10 | [Hoof It](https://adventofcode.com/2024/day/10) | [Source](src/year2024/day10.rs) | 38 |
85+
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Source](src/year2024/day11.rs) | 6500 |
8586

8687
## 2023
8788

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,5 @@ mod year2024 {
302302
benchmark!(year2024, day08);
303303
benchmark!(year2024, day09);
304304
benchmark!(year2024, day10);
305+
benchmark!(year2024, day11);
305306
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,5 @@ pub mod year2024 {
301301
pub mod day08;
302302
pub mod day09;
303303
pub mod day10;
304+
pub mod day11;
304305
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,6 @@ fn year2024() -> Vec<Solution> {
371371
solution!(year2024, day08),
372372
solution!(year2024, day09),
373373
solution!(year2024, day10),
374+
solution!(year2024, day11),
374375
]
375376
}

src/year2024/day11.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::util::hash::*;
2+
use crate::util::parse::*;
3+
4+
pub fn parse(input: &str) -> Vec<u64> {
5+
input.iter_unsigned().collect()
6+
}
7+
8+
pub fn part1(input: &[u64]) -> u64 {
9+
let cache = &mut FastMap::new();
10+
input.iter().map(|&stone| helper(cache, stone, 25)).sum()
11+
}
12+
13+
pub fn part2(input: &[u64]) -> u64 {
14+
let cache = &mut FastMap::new();
15+
input.iter().map(|&stone| helper(cache, stone, 75)).sum()
16+
}
17+
18+
fn helper(cache: &mut FastMap<(u64, u64), u64>, stone: u64, blinks: u64) -> u64 {
19+
if blinks == 0 {
20+
return 1;
21+
}
22+
23+
let key = (stone, blinks);
24+
if let Some(&value) = cache.get(&key) {
25+
return value;
26+
}
27+
28+
let next = if stone == 0 {
29+
helper(cache, 1, blinks - 1)
30+
} else {
31+
let digits = stone.ilog10() + 1;
32+
if digits % 2 == 0 {
33+
let power = 10_u64.pow(digits / 2);
34+
helper(cache, stone / power, blinks - 1) + helper(cache, stone % power, blinks - 1)
35+
} else {
36+
helper(cache, stone * 2024, blinks - 1)
37+
}
38+
};
39+
40+
cache.insert(key, next);
41+
next
42+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,5 @@ mod year2024 {
291291
mod day08_test;
292292
mod day09_test;
293293
mod day10_test;
294+
mod day11_test;
294295
}

tests/year2024/day11_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use aoc::year2024::day11::*;
2+
3+
const EXAMPLE: &str = "125 17";
4+
5+
#[test]
6+
fn part1_test() {
7+
let input = parse(EXAMPLE);
8+
assert_eq!(part1(&input), 55312);
9+
}
10+
11+
#[test]
12+
fn part2_test() {
13+
let input = parse(EXAMPLE);
14+
assert_eq!(part2(&input), 65601038650482);
15+
}

0 commit comments

Comments
 (0)