-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
157 lines (138 loc) · 4.39 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use itertools::Itertools;
#[allow(clippy::missing_panics_doc)]
pub fn main() {
let data = include_str!("input.txt");
let (instructions, map) = data.split_once("\n\n").unwrap();
let mut net = Network::<666>::new(map);
println!("Part 1: {}", part_one::<666>(&mut net, instructions));
println!("Part 2: {}", part_two::<666>(&net, instructions));
}
fn part_one<const N: usize>(net: &mut Network<N>, instructions: &str) -> usize {
net.run(instructions)
}
fn part_two<const N: usize>(net: &Network<N>, instructions: &str) -> usize {
net.data
.iter()
.enumerate()
.filter(|&(_, &(n, _, _))| n.ends_with('A'))
.map(|(i, _)| Network::<N> {
data: net.data,
current_node: (net.data[i].0, i),
})
.map(|mut n| n.run(instructions))
.fold(1, |acc, v| v * (acc / gcd(acc, v)))
}
#[derive(Debug)]
struct Network<'a, const N: usize> {
data: [(&'a str, usize, usize); N],
current_node: (&'a str, usize),
}
impl<const N: usize> Network<'_, N> {
fn run(&mut self, instructions: &str) -> usize {
for (i, &dir) in instructions.as_bytes().iter().cycle().enumerate() {
if self.current_node.0.ends_with('Z') {
return i - 1;
}
self.step(dir);
}
0
}
fn step(&mut self, direction: u8) {
let node = self.data[self.current_node.1];
match direction {
b'L' => self.current_node = (node.0, node.1),
b'R' => self.current_node = (node.0, node.2),
_ => unreachable!(),
}
}
// Test code to explore the shape of the problem.
// I'm deliberately leaving it in to prove I did my DD
#[allow(dead_code)]
fn find_cycle(&mut self, instructions: &str) {
for (total, (instruction_step, &dir)) in instructions
.as_bytes()
.iter()
.enumerate()
.cycle()
.enumerate()
.take(100_000)
{
if self.current_node.0.ends_with('A') || self.current_node.0.ends_with('Z') {
println!("{total} {instruction_step} {:?}", self.current_node);
}
self.step(dir);
}
}
}
impl<'a, const N: usize> Network<'a, N> {
fn new(s: &'a str) -> Self {
let nodes = s
.lines()
.map(|l| {
let (node, dest) = l.split_once(" = ").unwrap();
let (a, b) = dest
.trim_start_matches('(')
.trim_end_matches(')')
.split_once(", ")
.unwrap();
(node, a, b)
})
.sorted_unstable_by_key(|&(node, _, _)| node)
.collect_vec();
let mut data = [("", 0, 0); N];
for (&(node, left, right), d) in nodes.iter().zip(data.iter_mut()) {
let left = nodes.binary_search_by_key(&left, |&(n, _, _)| n).unwrap();
let right = nodes.binary_search_by_key(&right, |&(n, _, _)| n).unwrap();
*d = (node, left, right);
}
Self {
data,
current_node: (nodes[0].0, 0),
}
}
}
fn gcd(mut u: usize, mut v: usize) -> usize {
if u == 0 {
return v;
}
if v == 0 {
return u;
}
let gcd_exponent_on_two = (u | v).trailing_zeros();
u >>= u.trailing_zeros();
v >>= v.trailing_zeros();
while u != v {
if u < v {
core::mem::swap(&mut u, &mut v);
}
u -= v;
u >>= u.trailing_zeros();
}
u << gcd_exponent_on_two
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
let (instructions, map) = data.split_once("\n\n").unwrap();
let mut net = Network::<7>::new(map);
assert_eq!(2, part_one::<7>(&mut net, instructions));
}
#[test]
fn one_two() {
let data = include_str!("test2.txt");
let (instructions, map) = data.split_once("\n\n").unwrap();
let mut net = Network::<3>::new(map);
assert_eq!(6, part_one::<3>(&mut net, instructions));
}
#[test]
fn two() {
let data = include_str!("test3.txt");
let (instructions, map) = data.split_once("\n\n").unwrap();
let net = Network::<8>::new(map);
assert_eq!(6, part_two::<8>(&net, instructions));
}
}