This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rs
142 lines (111 loc) · 3.4 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
use std::collections::HashMap;
use itertools::Itertools;
advent_of_code::solution!(8);
type Int = u32;
/* -------------------------------------------------------------------------- */
#[derive(Clone, Copy)]
enum Direction {
Left,
Right,
}
struct Outcomes<'a> {
left: &'a str,
right: &'a str,
}
fn parse_input(input: &str) -> (Vec<Direction>, HashMap<&str, Outcomes>) {
let mut lines = input.lines();
let instructions = lines
.next()
.unwrap()
.as_bytes()
.iter()
.map(|b| match b {
b'L' => Direction::Left,
b'R' => Direction::Right,
_ => unreachable!(),
})
.collect_vec();
let empty = lines.next();
debug_assert!(empty.unwrap().is_empty());
let paths = lines
.map(|line| {
let (name, outcomes) = line.split_once('=').unwrap();
let name = name.trim();
let (left, right) = outcomes
.trim()
.strip_prefix('(')
.unwrap()
.strip_suffix(')')
.unwrap()
.split_once(',')
.unwrap();
let left = left.trim();
let right = right.trim();
(name, Outcomes { left, right })
})
.collect::<HashMap<_, _>>();
(instructions, paths)
}
/* -------------------------------------------------------------------------- */
pub fn part_one(input: &str) -> Option<Int> {
let (instructions, paths) = parse_input(input);
let mut location = "AAA";
let mut instructions = instructions.iter().copied().cycle();
let mut step = 0;
loop {
step += 1;
let path = paths.get(location).unwrap();
location = match instructions.next().unwrap() {
Direction::Left => path.left,
Direction::Right => path.right,
};
if location == "ZZZ" {
break;
}
}
Some(step)
}
/* -------------------------------------------------------------------------- */
pub fn part_two(input: &str) -> Option<u64> {
let (instructions, paths) = parse_input(input);
let start_locations = paths.keys().filter(|name| name.ends_with('A')).copied();
let result = start_locations
.map(|mut location| {
let mut step = 0u64;
let mut instructions = instructions.clone().into_iter().cycle();
loop {
let instruction = instructions.next().unwrap();
step += 1;
let path = paths.get(location).unwrap();
location = match instruction {
Direction::Left => path.left,
Direction::Right => path.right,
};
if location.ends_with('Z') {
break step;
}
}
})
.reduce(num::integer::lcm)
.unwrap();
Some(result)
}
/* -------------------------------------------------------------------------- */
#[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));
}
}