generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rs
133 lines (115 loc) · 3.57 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
use std::collections::HashMap;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{alphanumeric1, line_ending};
use nom::combinator::value;
use nom::multi::{many1, separated_list1};
use nom::sequence::{delimited, separated_pair};
use nom::IResult;
use num::integer::lcm;
advent_of_code::solution!(8);
#[derive(Debug, Copy, Clone)]
enum Instruction {
Left,
Right,
}
fn parse_input(input: &str) -> IResult<&str, (Vec<Instruction>, Vec<(&str, (&str, &str))>)> {
separated_pair(
many1(alt((
value(Instruction::Left, tag("L")),
value(Instruction::Right, tag("R")),
))),
many1(line_ending),
separated_list1(
line_ending,
separated_pair(
alphanumeric1,
tag(" = "),
delimited(
tag("("),
separated_pair(alphanumeric1, tag(", "), alphanumeric1),
tag(")"),
),
),
),
)(input)
}
fn solve(input: &str, part_two: bool) -> Option<usize> {
let (_, (instructions, map)) = parse_input(input).unwrap();
let (map, starting_nodes, target_node_mask) = {
let node_to_index = map
.iter()
.enumerate()
.map(|(i, (node, _))| (*node, i as u32))
.collect::<HashMap<&str, u32>>();
let mut starting_nodes = vec![];
let mut target_node_mask = vec![false; map.len()];
let map = map
.iter()
.map(|(node, (left, right))| {
if match part_two {
false => *node == "AAA",
true => node.ends_with('A'),
} {
starting_nodes.push(node_to_index[node]);
}
if match part_two {
false => *node == "ZZZ",
true => node.ends_with('Z'),
} {
target_node_mask[node_to_index[node] as usize] = true;
}
let left = node_to_index[left];
let right = node_to_index[right];
(left, right)
})
.collect::<Vec<_>>();
(map, starting_nodes, target_node_mask)
};
starting_nodes
.into_iter()
.map(|starting_node| {
let mut node = starting_node;
let mut steps = 0;
for instruction in instructions.iter().cycle() {
node = match (instruction, map[node as usize]) {
(Instruction::Left, (left, _)) => left,
(Instruction::Right, (_, right)) => right,
};
steps += 1;
if target_node_mask[node as usize] {
return steps;
}
}
unreachable!()
})
.reduce(lcm)
}
pub fn part_one(input: &str) -> Option<usize> {
solve(input, false)
}
pub fn part_two(input: &str) -> Option<usize> {
solve(input, true)
}
#[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(2));
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(6));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 3,
));
assert_eq!(result, Some(6));
}
}