generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
93 lines (77 loc) · 2.65 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
use aoc_parse::{parser, prelude::*};
use itertools::Itertools;
use num::Integer;
use rustc_hash::FxHashMap;
advent_of_code::solution!(8);
fn parse_maps(input: &str) -> (Vec<usize>, FxHashMap<String, [String; 2]>) {
let p = parser!(
line(char_of("LR")+)
line("")
lines(string(alnum+) " = (" string(alnum+) ", " string(alnum+) ")")
);
let (instructions, _, maps_definition) = p.parse(input).unwrap();
let mut maps = FxHashMap::default();
for map in maps_definition {
maps.insert(map.0, [map.1, map.2]);
}
// instructions is a vector of 0 = L, 1 = R; maps is src => dest(L, R)
(instructions, maps)
}
fn traverse_map(
start: &str,
instructions: &Vec<usize>,
maps: &FxHashMap<String, [String; 2]>,
) -> usize {
// calculate the distance from the start to a destination
let mut pos = start;
let mut count = 0;
while !pos.ends_with('Z') {
// get the next instruction, looping to the start after all are read
let inst = instructions[count % instructions.len()];
pos = &maps[pos][inst];
count += 1;
}
count
}
pub fn part_one(input: &str) -> Option<usize> {
let (instructions, maps) = parse_maps(input);
let count = traverse_map("AAA", &instructions, &maps);
Some(count)
}
pub fn part_two(input: &str) -> Option<usize> {
let (instructions, maps) = parse_maps(input);
// calculate the distance to the destination of each possible start point
let starts = maps.keys().filter(|k| k.ends_with('A')).collect_vec();
let counts = starts
.iter()
.map(|start| traverse_map(start, &instructions, &maps));
// Calculate the least common multiple of the routes - this will be the point in time that each
// of the routes land at a destination at the same time.
//
// This works because of an assumption in the input data - it is structed such that continuing
// to traverse past the destination will reach the same destination again in the same distance
// as the original route.
counts.reduce(|acc, e| acc.lcm(&e))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 11,
));
assert_eq!(result, Some(2));
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 12,
));
assert_eq!(result, Some(6));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 21,
));
assert_eq!(result, Some(6));
}
}