-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
154 lines (133 loc) · 3.87 KB
/
11.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::collections::HashMap;
use advent_of_code::ws;
use itertools::Itertools;
use nom::{character::complete::u64, multi::many1, IResult};
advent_of_code::solution!(11);
fn parse_input(line: &str) -> Vec<u64> {
let result: IResult<_, _> = many1(ws(u64))(line);
result.expect("🐸").1
}
pub enum Blink {
Single(u64),
Double(u64, u64),
}
fn blink(b: u64) -> Blink {
if b == 0 {
Blink::Single(1)
} else {
let digits = ((b as f64).log10().floor() + 1.) as u64;
if digits % 2 == 0 {
let power_of_ten = 10_u64.pow(digits as u32 / 2);
let right = b % power_of_ten;
let left = b / power_of_ten;
Blink::Double(left, right)
} else {
Blink::Single(b * 2024)
}
}
}
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
struct Problem {
s: u64,
times: usize,
}
impl Problem {
fn blink(self) -> (Problem, Option<Problem>) {
match blink(self.s) {
Blink::Double(l, r) => (
Problem {
s: l,
times: self.times - 1,
},
Some(Problem {
s: r,
times: self.times - 1,
}),
),
Blink::Single(s) => (
Problem {
s,
times: self.times - 1,
},
None,
),
}
}
}
fn blink_fast(stones: Vec<u64>, times: usize) -> u64 {
let mut cache = HashMap::new();
let mut to_solve = stones
.into_iter()
.map(|s| Problem { s, times })
.collect_vec();
let original = to_solve.clone();
while let Some(problem) = to_solve.pop() {
match cache.get(&problem).copied() {
Some(_) => {}
None if problem.times == 0 => {
cache.insert(problem, 1);
}
None => match problem.blink() {
(pl, Some(pr)) => {
match (cache.get(&pl).copied(), cache.get(&pr).copied()) {
(Some(al), Some(ar)) => {
cache.insert(problem, al + ar);
}
(Some(_), None) => {
to_solve.extend_from_slice(&[problem, pr]);
}
(None, Some(_)) => {
to_solve.extend_from_slice(&[problem, pl]);
}
_ => {
to_solve.extend_from_slice(&[problem, pl, pr]);
}
}
}
(p, None) => match cache.get(&p).copied() {
Some(ans) => {
cache.insert(problem, ans);
}
None => {
to_solve.extend_from_slice(&[problem, p]);
}
},
},
}
}
original
.iter()
.map(|s| {
let Some(ans) = cache.get(s).copied() else {
unreachable!("Answers are present")
};
ans
})
.sum()
}
pub fn part_one(input: &str) -> Option<u64> {
let stones = parse_input(input);
let result = blink_fast(stones, 25);
Some(result)
}
pub fn part_two(input: &str) -> Option<u64> {
let stones = parse_input(input);
let result = blink_fast(stones, 75);
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result =
part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(55312));
}
#[test]
fn test_part_two() {
let result =
part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(65601038650482));
}
}