-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rs
140 lines (121 loc) · 3.03 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
use std::{collections::HashMap, mem::swap};
static INPUT_TXT: &str = include_str!("../../input/08.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
enum Choice {
Left,
Right,
}
impl From<char> for Choice {
fn from(c: char) -> Self {
match c {
'L' => Self::Left,
'R' => Self::Right,
_ => unreachable!(),
}
}
}
fn parse_input(input: &str) -> (Vec<Choice>, HashMap<&str, (&str, &str)>) {
let (dir_str, map_str) = input.trim().split_once("\n\n").unwrap();
let directions = dir_str.trim().chars().map(Choice::from).collect::<Vec<_>>();
let map = map_str
.lines()
.map(|l| {
l.split_once(" = ")
.map(|(k, v)| {
let value = v
.split_once(", ")
.map(|s| (s.0.trim_matches('('), s.1.trim_matches(')')))
.expect("Invalid tuple");
(k, value)
})
.expect("Invalid map")
})
.collect::<HashMap<_, _>>();
(directions, map)
}
fn traverse(
directions: &[Choice],
map: &HashMap<&str, (&str, &str)>,
start_position: &str,
ends_with: &str,
) -> usize {
let mut pos = start_position;
let mut count = 0;
for choice in directions.iter().cycle() {
let next = map.get(pos).unwrap();
count += 1;
pos = match choice {
Choice::Left => next.0,
Choice::Right => next.1,
};
if pos.ends_with(ends_with) {
break;
}
}
count
}
fn part_1(input: &str) -> usize {
let (directions, map) = parse_input(input);
traverse(&directions, &map, "AAA", "ZZZ")
}
fn gcd(first: usize, second: usize) -> usize {
let mut max = first;
let mut min = second;
if min > max {
swap(&mut max, &mut min);
}
loop {
let res = max % min;
if res == 0 {
break min;
}
max = min;
min = res;
}
}
fn part_2(input: &str) -> usize {
let (directions, map) = parse_input(input);
map.keys()
.filter(|k| k.ends_with('A'))
.map(|pos| traverse(&directions, &map, pos, "Z"))
.fold(1, |a, b| a * b / gcd(a, b))
}
#[cfg(test)]
mod tests {
use super::*;
static INPUT_1: &str = "RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)";
static INPUT_2: &str = "LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)";
static INPUT_3: &str = "LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT_1), 2);
assert_eq!(part_1(INPUT_2), 6);
assert_eq!(part_1(INPUT_TXT), 12169);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT_3), 6);
assert_eq!(part_2(INPUT_TXT), 12_030_780_859_469);
}
}