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