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
|
use num::integer::lcm;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
fn main() {
// let filename = "in/day08.ref";
let filename = "in/day08.pzl";
let mut f = File::open(filename).expect("cannot open file");
let mut content = String::new();
f.read_to_string(&mut content).expect("cannot read file");
let mut lines = content.trim_end().split("\n\n");
let ops: Vec<char> = lines.next().unwrap().chars().collect();
let mut tree: HashMap<&str, (&str, &str)> = HashMap::new();
for line in lines.next().unwrap().split('\n') {
let (first, rest) = line.split_once(" = ").unwrap();
let (left, right) = rest.split_once(", ").unwrap();
let left = &left[1..];
let right = &right[..3];
tree.insert(first, (left, right));
}
let traverse_tree_until = |start: &str, f: fn(&str) -> bool| {
let mut loc = start;
let mut steps: usize = 0;
loop {
let (left, right) = tree.get(loc).unwrap();
let op = ops[steps % ops.len()];
if op == 'L' {
loc = left;
} else {
loc = right;
}
if f(&loc) {
break;
}
steps += 1;
}
(steps + 1) as u64
};
let res1 = traverse_tree_until("AAA", |x| x == "ZZZ");
let locs = tree.keys().filter(|x| &x[2..3] == "A").collect::<Vec<_>>();
let mut stps: Vec<u64> = Vec::new();
for i in 0..locs.len() {
stps.push(traverse_tree_until(locs[i], |x| x.ends_with("Z")));
}
let res2 = stps.iter().fold(1, |x, y| lcm(x, *y));
println!("res1: {}", res1);
println!("res2: {}", res2);
assert_eq!(res1, 14257);
assert_eq!(res2, 16187743689077);
}
|