-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.rs
158 lines (149 loc) · 4.32 KB
/
day16.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
158
use if_chain::if_chain;
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap};
type YX = (usize, usize);
fn parse(data: &str) -> Option<(BTreeSet<YX>, YX, YX)> {
let mut maze = BTreeSet::new();
let mut start = None;
let mut end = None;
for (y, line) in data.lines().enumerate() {
for (x, b) in line.bytes().enumerate() {
match b {
b'#' => {
maze.insert((y, x));
}
b'S' => {
if start.replace((y, x)).is_some() {
return None;
}
}
b'E' => {
if end.replace((y, x)).is_some() {
return None;
}
}
_ => {}
}
}
}
Some((maze, start?, end?))
}
pub fn part1(data: &str) -> Option<usize> {
let (maze, start, end) = parse(data)?;
let mut queue: BinaryHeap<_> = [(Reverse(0), start, (0, 1))].into();
let mut visited = BTreeSet::new();
while let Some((Reverse(score), (y, x), (dy, dx))) = queue.pop() {
if !visited.insert(((y, x), (dy, dx))) {
continue;
}
if (y, x) == end {
return Some(score);
}
for (score, (dy, dx)) in [
(score + 1, (dy, dx)),
(score + 1001, (-dx, dy)),
(score + 1001, (dx, -dy)),
] {
if_chain! {
if let Some(y) = y.checked_add_signed(dy);
if let Some(x) = x.checked_add_signed(dx);
if !maze.contains(&(y, x));
then {
queue.push((Reverse(score), (y, x), (dy, dx)));
}
}
}
}
None
}
pub fn part2(data: &str) -> Option<usize> {
let (maze, start, end) = parse(data)?;
let mut best = None;
let mut acc = BTreeSet::new();
let mut queue: BinaryHeap<(_, _, _, BTreeSet<_>)> =
[(Reverse(0), start, (0, 1), [start].into())].into();
let mut visited = BTreeMap::new();
while let Some((Reverse(score), (y, x), (dy, dx), mut path)) = queue.pop() {
if best.filter(|best| *best < score).is_some() {
break;
}
if *visited.entry(((y, x), (dy, dx))).or_insert(score) < score {
continue;
}
if (y, x) == end {
best = Some(score);
acc.append(&mut path);
continue;
}
for (score, (dy, dx)) in [
(score + 1, (dy, dx)),
(score + 1001, (-dx, dy)),
(score + 1001, (dx, -dy)),
] {
if_chain! {
if let Some(y) = y.checked_add_signed(dy);
if let Some(x) = x.checked_add_signed(dx);
if !maze.contains(&(y, x));
then {
let mut path = path.clone();
if path.insert((y, x)) {
queue.push((Reverse(score), (y, x), (dy, dx), path));
}
}
}
}
}
Some(acc.len())
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE_1: &str = indoc! {"
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
"};
static EXAMPLE_2: &str = indoc! {"
#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
"};
#[test]
fn part1_examples() {
assert_eq!(Some(7036), part1(EXAMPLE_1));
assert_eq!(Some(11048), part1(EXAMPLE_2));
}
#[test]
fn part2_examples() {
assert_eq!(Some(45), part2(EXAMPLE_1));
assert_eq!(Some(64), part2(EXAMPLE_2));
}
}