generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
214 lines (167 loc) · 5.16 KB
/
08.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use core::panic;
use std::collections::HashMap;
advent_of_code::solution!(8);
pub fn part_one(input: &str) -> Option<u64> {
let mut lines = input.lines();
let navigation_instructions = lines.next().unwrap();
let nodes_map = get_nodes_hash_map(lines.skip(1));
let (steps_count, _) =
get_steps_until_end(navigation_instructions, &nodes_map, "AAA", |node| {
node == "ZZZ"
});
Some(steps_count)
}
pub fn part_two(input: &str) -> Option<u64> {
let mut lines = input.lines();
let navigation_instructions = lines.next().unwrap();
let nodes_map = get_nodes_hash_map(lines.clone().skip(1));
let start_nodes = get_starting_nodes(lines.skip(1));
let end_nodes_per_start_node = start_nodes
.iter()
.map(|start_node| {
let mut end_nodes = Vec::new();
let mut current_node_id = *start_node;
for _ in 0..start_nodes.len() {
let result = get_steps_until_end(
navigation_instructions,
&nodes_map,
current_node_id,
|node| node.ends_with('Z'),
);
end_nodes.push(result);
current_node_id = result.1;
}
end_nodes
})
.collect::<Vec<_>>();
// Based on input I have I know that this is true and therefore skip more complicated computations.
if end_nodes_per_start_node.iter().all(|end_nodes| {
let mut step_count_outer = 0;
end_nodes
.iter()
.enumerate()
.all(|(index, (step_count, _))| {
if index == 0 {
step_count_outer = *step_count
}
*step_count == step_count_outer
})
}) {
let mut factor_map = HashMap::new();
for end_nodes in end_nodes_per_start_node {
let factors = get_prime_factorization(end_nodes[0].0);
for (factor, count) in factors {
let existing_count = factor_map.get(&factor).unwrap_or(&0);
if count > *existing_count {
factor_map.insert(factor, count);
}
}
}
let mut result = 1;
for (factor, count) in factor_map {
result *= factor.pow(count);
}
Some(result)
} else {
None
}
}
fn get_nodes_hash_map<'a>(lines: impl Iterator<Item = &'a str>) -> HashMap<&'a str, Node<'a>> {
let mut map = HashMap::new();
for line in lines {
map.insert(&line[0..3], Node::new(&line[7..10], &line[12..15]));
}
map
}
fn get_steps_until_end<'a>(
navigation_instructions: &'a str,
nodes_map: &'a HashMap<&'a str, Node<'a>>,
start: &'a str,
is_end: fn(&str) -> bool,
) -> (u64, &'a str) {
let mut current_node_id = start;
let mut step_count = 0;
while step_count == 0 || !is_end(current_node_id) {
for char in navigation_instructions.chars() {
step_count += 1;
let current_node = nodes_map.get(current_node_id).unwrap();
current_node_id = match char {
'L' => current_node.left,
'R' => current_node.right,
_ => panic!("Invalid navigation instruction"),
};
if is_end(current_node_id) {
break;
}
}
}
(step_count, current_node_id)
}
fn get_starting_nodes<'a>(lines: impl Iterator<Item = &'a str>) -> Vec<&'a str> {
let mut nodes = Vec::new();
for line in lines {
if line[0..3].ends_with('A') {
nodes.push(&line[0..3]);
}
}
nodes
}
fn get_prime_factorization(number: u64) -> Vec<(u64, u32)> {
let mut factors = Vec::new();
let mut current_number = number;
while current_number > 1 {
let mut has_found_factor = false;
for index in 2..current_number {
if current_number % index == 0 {
has_found_factor = true;
factors.push(index);
current_number /= index;
break;
}
}
if !has_found_factor {
factors.push(current_number);
break;
}
}
factors.sort();
let mut factors_summarized = Vec::new();
for factor in factors {
if let Some((_, count)) = factors_summarized
.iter_mut()
.find(|(inner_factor, _)| *inner_factor == factor)
{
*count += 1;
} else {
factors_summarized.push((factor, 1));
}
}
factors_summarized
}
struct Node<'a> {
left: &'a str,
right: &'a str,
}
impl Node<'_> {
fn new<'a>(left: &'a str, right: &'a str) -> Node<'a> {
Node { left, right }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 1,
));
assert_eq!(result, Some(6));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(6));
}
}