-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.rs
65 lines (56 loc) · 1.91 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
use std::collections::HashMap;
use itertools::Itertools;
use crate::utils::v2::solver;
pub struct Solver;
impl Solver {
fn solve(&self, input: &str, iterations: usize) -> usize {
let mut stones = input.split_whitespace().map(|c| c.to_owned()).counts();
for _ in 0..iterations {
stones = stones
.into_iter()
.flat_map(|(stone, count)| {
if stone == "0" {
return vec![("1".to_owned(), count)];
}
if stone.len() % 2 == 0 {
let l = stone[..stone.len() / 2].to_owned();
let mut r = (stone[stone.len() / 2..])
.trim_start_matches('0')
.to_owned();
if r.is_empty() {
r = "0".to_owned();
}
return vec![(l, count), (r, count)];
}
return vec![((stone.parse::<u64>().unwrap() * 2024).to_string(), count)];
})
.fold(HashMap::new(), |mut acc, (stone, count)| {
*acc.entry(stone).or_insert(0) += count;
acc
});
}
stones.into_values().sum()
}
}
impl solver::Solver<2024, 11> for Solver {
type Part1 = usize;
type Part2 = usize;
fn solve_part_one(&self, input: &str) -> Self::Part1 {
self.solve(input, 25)
}
fn solve_part_two(&self, input: &str) -> Self::Part2 {
self.solve(input, 75)
}
}
#[cfg(test)]
mod tests {
use crate::utils::v2::solver::Solver;
use rstest::rstest;
#[rstest]
#[case("125 17", 55312)]
fn test_part_one(#[case] input: &str, #[case] expected: usize) {
let solver = super::Solver;
let result = solver.solve_part_one(input);
assert_eq!(result, expected);
}
}