-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
52 lines (43 loc) · 1.11 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
#![feature(test)]
use rustc_hash::FxHashMap;
type Input = Vec<u64>;
fn setup(input: &str) -> Input {
input
.split_whitespace()
.map(|n| n.parse().unwrap())
.collect()
}
fn solve(input: &Input, steps: usize) -> usize {
let mut nums = FxHashMap::default();
let mut new_nums = FxHashMap::default();
for &n in input {
*nums.entry(n).or_default() += 1;
}
for _ in 0..steps {
new_nums.clear();
for (&n, &cnt) in &nums {
let mut insert = |n| *new_nums.entry(n).or_default() += cnt;
if n == 0 {
insert(1);
continue;
}
let d = n.ilog10() + 1;
if d & 1 == 0 {
let m = 10u64.pow(d >> 1);
insert(n / m);
insert(n % m);
} else {
insert(n * 2024);
}
}
std::mem::swap(&mut nums, &mut new_nums);
}
nums.values().sum()
}
fn part1(input: &Input) -> usize {
solve(input, 25)
}
fn part2(input: &Input) -> usize {
solve(input, 75)
}
aoc::main!(2024, 11, ex: 1);