Skip to content

Commit e03d532

Browse files
committed
Day 11 solutions
1 parent 16ffa4e commit e03d532

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ This should start the server at `localhost:8080`.
9696
❄️ [Day 08](aoc-solver/src/y2024/day08.rs)
9797
❄️ [Day 09](aoc-solver/src/y2024/day09.rs)
9898
❄️ [Day 10](aoc-solver/src/y2024/day10.rs)
99+
❄️ [Day 11](aoc-solver/src/y2024/day11.rs)

aoc-solver/src/y2024/day11.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use std::collections::HashMap;
2+
3+
use crate::solution::{AocError, Solution};
4+
5+
type Cache = HashMap<u64, HashMap<usize, usize>>;
6+
7+
fn parse(input: &str) -> Result<Vec<u64>, AocError> {
8+
input
9+
.split_ascii_whitespace()
10+
.map(|number| {
11+
number
12+
.parse::<u64>()
13+
.map_err(|err| AocError::parse(number, err))
14+
})
15+
.collect()
16+
}
17+
18+
fn split(stone: u64) -> Option<(u64, u64)> {
19+
let digits = stone.ilog10() + 1;
20+
if digits % 2 != 0 {
21+
return None;
22+
}
23+
24+
let divisor = 10u64.pow(digits / 2);
25+
let left = stone / divisor;
26+
let right = stone % divisor;
27+
28+
Some((left, right))
29+
}
30+
31+
fn simulate(stone: u64, steps: usize, cache: &mut Cache) -> usize {
32+
if steps == 0 {
33+
return 1;
34+
}
35+
36+
if let Some(&count) = cache.get(&stone).and_then(|counts| counts.get(&steps)) {
37+
return count;
38+
}
39+
40+
let count = if stone == 0 {
41+
simulate(1, steps - 1, cache)
42+
} else if let Some((left, right)) = split(stone) {
43+
simulate(left, steps - 1, cache) + simulate(right, steps - 1, cache)
44+
} else {
45+
simulate(stone * 2024, steps - 1, cache)
46+
};
47+
48+
cache.entry(stone).or_default().insert(steps, count);
49+
50+
count
51+
}
52+
53+
pub struct Day11;
54+
impl Solution for Day11 {
55+
type A = usize;
56+
type B = usize;
57+
58+
fn default_input(&self) -> &'static str {
59+
include_str!("../../../inputs/2024/day11.txt")
60+
}
61+
62+
fn part_1(&self, input: &str) -> Result<usize, AocError> {
63+
let stones = parse(input)?;
64+
let mut cache = HashMap::new();
65+
66+
let count = stones
67+
.into_iter()
68+
.map(|stone| simulate(stone, 25, &mut cache))
69+
.sum();
70+
71+
Ok(count)
72+
}
73+
74+
fn part_2(&self, input: &str) -> Result<usize, AocError> {
75+
let stones = parse(input)?;
76+
let mut cache = HashMap::new();
77+
78+
let count = stones
79+
.into_iter()
80+
.map(|stone| simulate(stone, 75, &mut cache))
81+
.sum();
82+
83+
Ok(count)
84+
}
85+
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
91+
#[test]
92+
fn it_solves_part1_example_1() {
93+
assert_eq!(Day11.part_1("125 17"), Ok(55312));
94+
}
95+
}

aoc-solver/src/y2024/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod day07;
1010
pub mod day08;
1111
pub mod day09;
1212
pub mod day10;
13-
// pub mod day11;
13+
pub mod day11;
1414
// pub mod day12;
1515
// pub mod day13;
1616
// pub mod day14;
@@ -26,7 +26,7 @@ pub mod day10;
2626
// pub mod day24;
2727
// pub mod day25;
2828

29-
pub const MAX_DAYS: u8 = 10;
29+
pub const MAX_DAYS: u8 = 11;
3030

3131
pub struct Y2024;
3232

@@ -43,7 +43,7 @@ impl Solver for Y2024 {
4343
8 => day08::Day08.run(input, 8, 2024),
4444
9 => day09::Day09.run(input, 9, 2024),
4545
10 => day10::Day10.run(input, 10, 2024),
46-
// 11 => day11::Day11.run(input, 11, 2024),
46+
11 => day11::Day11.run(input, 11, 2024),
4747
// 12 => day12::Day12.run(input, 12, 2024),
4848
// 13 => day13::Day13.run(input, 13, 2024),
4949
// 14 => day14::Day14.run(input, 14, 2024),
@@ -85,7 +85,7 @@ impl Solver for Y2024 {
8585
8 => include_str!("./day08.rs"),
8686
9 => include_str!("./day09.rs"),
8787
10 => include_str!("./day10.rs"),
88-
// 11 => include_str!("./day11.rs"),
88+
11 => include_str!("./day11.rs"),
8989
// 12 => include_str!("./day12.rs"),
9090
// 13 => include_str!("./day13.rs"),
9191
// 14 => include_str!("./day14.rs"),

aoc-web/src/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn header(props: &HeaderProps) -> Html {
2929
<NavLink route={Route::Solution { year: 2024, day: 8 }} current={props.route.clone()} text={"8"}/>
3030
<NavLink route={Route::Solution { year: 2024, day: 9 }} current={props.route.clone()} text={"9"}/>
3131
<NavLink route={Route::Solution { year: 2024, day: 10 }} current={props.route.clone()} text={"10"}/>
32+
<NavLink route={Route::Solution { year: 2024, day: 11 }} current={props.route.clone()} text={"11"}/>
3233
</>
3334
}
3435
},

inputs/2024/day11.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 54 992917 5270417 2514 28561 0 990

0 commit comments

Comments
 (0)