-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
96 lines (82 loc) · 2.28 KB
/
mod.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
use std::collections::HashMap;
use aoc_runner_derive::{aoc, aoc_generator};
#[allow(unused)]
use itertools::Itertools;
type Num = usize;
type Output = Num;
type Input = HashMap<Num, Num>;
#[aoc_generator(day11)]
pub fn input_generator(input: &str) -> Input {
input.split_whitespace().fold(HashMap::new(), |mut map, n| {
map.entry(n.parse().unwrap())
.and_modify(|count| *count += 1)
.or_insert(1);
map
})
}
fn transform_stone(num: Num) -> (Num, Option<Num>) {
match num {
0 => (1, None),
n if (num.ilog10() + 1) % 2 == 0 => {
let digits = num.ilog10() + 1; // As num must not be 0 it can only be precomputed here
(
n / 10_usize.pow(digits / 2),
Some(n - (n / 10_usize.pow(digits / 2) * 10_usize.pow(digits / 2))),
)
}
n => (n * 2024, None),
}
}
fn blink(map: Input) -> Input {
map.iter()
.fold(HashMap::new(), |mut new_map, (num, count)| {
let next = transform_stone(*num);
new_map
.entry(next.0)
.and_modify(|old_count| *old_count += *count)
.or_insert(*count);
if let Some(next2) = next.1 {
new_map
.entry(next2)
.and_modify(|old_count| *old_count += *count)
.or_insert(*count);
}
new_map
})
}
fn observ(map: &Input, time: Num) -> Output {
let mut map = map.clone();
for _ in 0..time {
map = blink(map)
}
map.values().sum()
}
#[aoc(day11, part1)]
pub fn solve_part1(input: &Input) -> Output {
observ(input, 25)
}
#[aoc(day11, part2)]
pub fn solve_part2(input: &Input) -> Output {
observ(input, 75)
}
pub fn part1(input: &str) -> impl std::fmt::Display {
solve_part1(&input_generator(input))
}
pub fn part2(input: &str) -> impl std::fmt::Display {
solve_part2(&input_generator(input))
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> &'static str {
"125 17"
}
#[test]
fn samples_part1() {
assert_eq!(55312, solve_part1(&input_generator(sample())));
}
#[test]
fn samples_part2() {
assert_eq!(65601038650482, solve_part2(&input_generator(sample())));
}
}