generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
58 lines (51 loc) · 1.6 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
use std::collections::HashMap;
advent_of_code::solution!(11);
pub fn solve(input: &str, iterations: usize) -> Option<u64> {
let mut stones: HashMap<u64, u64> = input
.split_whitespace()
.map(|x| (x.parse::<u64>().unwrap(), 1))
.collect();
for _ in 0..iterations {
let mut new_stones: HashMap<u64, u64> = HashMap::new();
for stone in stones.iter() {
let new = if *stone.0 == 0 {
vec![1]
} else {
let stone_str = stone.0.to_string();
if stone_str.len() % 2 == 0 {
vec![
stone_str[0..stone_str.len() / 2].parse::<u64>().unwrap(),
stone_str[(stone_str.len() / 2)..].parse::<u64>().unwrap(),
]
} else {
vec![stone.0 * 2024]
}
};
for n in new.iter() {
*new_stones.entry(*n).or_insert(0) += stone.1;
}
}
stones = new_stones;
}
Some(stones.iter().map(|x| x.1).sum())
}
pub fn part_one(input: &str) -> Option<u64> {
solve(input, 25)
}
pub fn part_two(input: &str) -> Option<u64> {
solve(input, 75)
}
#[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));
}
}