generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rs
103 lines (86 loc) · 2.71 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
use std::collections::HashMap;
advent_of_code::solution!(11);
pub fn part_one(input: &str) -> Option<u64> {
let mut lookup = HashMap::new();
let stones: u64 = input
.trim()
.split(" ")
.map(|num_str| {
let curr_stone = num_str.parse().unwrap();
blink(&mut lookup, curr_stone, 0)
})
.sum();
Some(stones)
}
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct LookupKey(u64, u8);
fn blink(lookup: &mut HashMap<LookupKey, u64>, curr_stone: u64, curr_step: u8) -> u64 {
// Break case
if curr_step == 25 {
return 1;
}
// Check Lookup
if let Some(&cached) = lookup.get(&LookupKey(curr_stone, curr_step)) {
return cached;
};
let res = if curr_stone == 0 {
blink(lookup, 1, curr_step + 1)
} else if curr_stone.to_string().len() % 2 == 0 {
let num_str = curr_stone.to_string();
let (l, r) = num_str.split_at(num_str.len() / 2);
blink(lookup, l.parse::<u64>().unwrap(), curr_step + 1)
+ blink(lookup, r.parse::<u64>().unwrap(), curr_step + 1)
} else {
blink(lookup, curr_stone * 2024, curr_step + 1)
};
lookup.insert(LookupKey(curr_stone, curr_step), res);
res
}
pub fn part_two(input: &str) -> Option<u64> {
let mut lookup = HashMap::new();
let stones: u64 = input
.trim()
.split(" ")
.map(|num_str| {
let curr_stone = num_str.parse().unwrap();
blink2(&mut lookup, curr_stone, 0)
})
.sum();
Some(stones)
}
fn blink2(lookup: &mut HashMap<LookupKey, u64>, curr_stone: u64, curr_step: u8) -> u64 {
// Break case
if curr_step == 75 {
return 1;
}
// Check Lookup
if let Some(&cached) = lookup.get(&LookupKey(curr_stone, curr_step)) {
return cached;
};
let res = if curr_stone == 0 {
blink2(lookup, 1, curr_step + 1)
} else if curr_stone.to_string().len() % 2 == 0 {
let num_str = curr_stone.to_string();
let (l, r) = num_str.split_at(num_str.len() / 2);
blink2(lookup, l.parse::<u64>().unwrap(), curr_step + 1)
+ blink2(lookup, r.parse::<u64>().unwrap(), curr_step + 1)
} else {
blink2(lookup, curr_stone * 2024, curr_step + 1)
};
lookup.insert(LookupKey(curr_stone, curr_step), res);
res
}
#[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));
}
}