Skip to content

Commit 5185666

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

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-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) | 2378 |
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! # Plutonian Pebbles
2+
//!
3+
//! Each stone is independent and does not affect its neighbours. This means that we can
4+
//! recursively split each stone, skipping trillions of calculations by memoizing the count for
5+
//! each `(stone, blinks)` tuple.
6+
//!
7+
//! Interestingly the number of distinct stones is not too large, about 5000 for part two.
8+
use crate::util::hash::*;
9+
use crate::util::parse::*;
10+
11+
pub fn parse(input: &str) -> Vec<u64> {
12+
input.iter_unsigned().collect()
13+
}
14+
15+
pub fn part1(input: &[u64]) -> u64 {
16+
let cache = &mut FastMap::with_capacity(5_000);
17+
input.iter().map(|&stone| count(cache, stone, 25)).sum()
18+
}
19+
20+
pub fn part2(input: &[u64]) -> u64 {
21+
let cache = &mut FastMap::with_capacity(150_000);
22+
input.iter().map(|&stone| count(cache, stone, 75)).sum()
23+
}
24+
25+
fn count(cache: &mut FastMap<(u64, u64), u64>, stone: u64, blinks: u64) -> u64 {
26+
if blinks == 1 {
27+
if stone == 0 {
28+
return 1;
29+
}
30+
let digits = stone.ilog10() + 1;
31+
return if digits % 2 == 0 { 2 } else { 1 };
32+
}
33+
34+
let key = (stone, blinks);
35+
if let Some(&value) = cache.get(&key) {
36+
return value;
37+
}
38+
39+
let next = if stone == 0 {
40+
count(cache, 1, blinks - 1)
41+
} else {
42+
let digits = stone.ilog10() + 1;
43+
if digits % 2 == 0 {
44+
let power = 10_u64.pow(digits / 2);
45+
count(cache, stone / power, blinks - 1) + count(cache, stone % power, blinks - 1)
46+
} else {
47+
count(cache, stone * 2024, blinks - 1)
48+
}
49+
};
50+
51+
cache.insert(key, next);
52+
next
53+
}

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)