generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
152 lines (118 loc) · 3.63 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
use std::collections::HashMap;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
advent_of_code::solution!(8);
#[derive(Debug)]
struct NodeDirection {
left: String,
right: String,
}
#[derive(Debug)]
struct Map {
directions: Vec<Direction>,
map: HashMap<String, NodeDirection>,
}
#[derive(Debug)]
enum Direction {
Left,
Right,
}
const START: &str = "AAA";
const END: &str = "ZZZ";
fn file_to_node_map(s: &str) -> Map {
let mut lines = s.lines();
let directions = lines
.next()
.expect("Always a first line")
.chars()
.map(|char| match char {
'L' => Direction::Left,
'R' => Direction::Right,
_ => panic!("Invalid Direction Char {}", char),
})
.collect_vec();
let lines = lines.skip(1); // Empty Line
static LINE_MAP_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?P<node>\w{3}) = \((?P<left>\w{3}), (?P<right>\w{3})\)").expect("Valid Regex")
});
let map = lines
.map(|line| {
let matches = LINE_MAP_RE
.captures(line)
.expect("Every line contains a valid map line");
(
matches["node"].to_string(),
NodeDirection {
left: matches["left"].to_string(),
right: matches["right"].to_string(),
},
)
})
.collect();
Map { directions, map }
}
pub fn part_one(input: &str) -> Option<u32> {
let map = file_to_node_map(input);
let mut current_node = START;
let mut directions = map.directions.iter().cycle();
let mut count = 0;
while current_node != END {
let next_dir = directions.next().expect("Cycled Iter never ends");
let potential_directions = map.map.get(current_node).expect("Node always exists");
match next_dir {
Direction::Left => current_node = &potential_directions.left,
Direction::Right => current_node = &potential_directions.right,
}
count += 1;
}
Some(count)
}
pub fn part_two(input: &str) -> Option<usize> {
let map = file_to_node_map(input);
let current_nodes = map
.map
.keys()
.filter(|node| node.ends_with('A'))
.cloned()
.collect_vec();
// Key insight: All paths are cycles.
let path_cycle_lengths = current_nodes
.iter()
.map(|node| {
let mut current_node = node;
let mut directions = map.directions.iter().cycle();
let mut count: usize = 0;
while !current_node.ends_with('Z') {
let next_dir = directions.next().expect("Cycled Iter never ends");
let potential_directions = map.map.get(current_node).expect("Node always exists");
match next_dir {
Direction::Left => current_node = &potential_directions.left,
Direction::Right => current_node = &potential_directions.right,
}
count += 1;
}
count
})
.collect_vec();
Some(
path_cycle_lengths
.iter()
.fold(1usize, |acc, len| num::integer::lcm(acc, *len)),
)
}
//11678319315857
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples/part1", DAY));
assert_eq!(result, Some(6));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples/part2", DAY));
assert_eq!(result, Some(6));
}
}