-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_16.rs
183 lines (152 loc) Β· 4.59 KB
/
day_16.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::{
cmp::Ordering,
collections::{BinaryHeap, HashSet},
};
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::Vec2;
solution!("Reindeer Maze", 16);
fn part_a(input: &str) -> Answer {
let map = Maze::parse(input);
let (_, shortest) = map.foreword();
shortest.into()
}
fn part_b(input: &str) -> Answer {
let map = Maze::parse(input);
let (scores, _) = map.foreword();
map.reverse(scores).into()
}
struct Maze {
map: Grid<Tile>,
start: Vec2<usize>,
end: Vec2<usize>,
}
#[derive(PartialEq, Eq)]
enum Tile {
Empty,
Wall,
Start,
End,
}
#[derive(PartialEq, Eq)]
struct Item {
pos: Vec2<usize>,
dir: Direction,
score: u32,
}
impl Maze {
fn parse(input: &str) -> Self {
let map = Grid::parse(input, |c| match c {
'.' => Tile::Empty,
'#' => Tile::Wall,
'S' => Tile::Start,
'E' => Tile::End,
_ => panic!(),
});
let start = map.find(Tile::Start).unwrap();
let end = map.find(Tile::End).unwrap();
Self { map, start, end }
}
/// Use dijkstra's to find the shortest path, populating a costs grid with
/// the minimum cost needed to reach that tile, which will be used for part B.
fn foreword(&self) -> (Grid<[u32; 4]>, u32) {
let mut queue = BinaryHeap::new();
let mut seen = HashSet::new();
let mut costs = Grid::new(self.map.size, [u32::MAX; 4]);
queue.push(Item::new(self.start, Direction::Right, 0));
while let Some(Item { pos, dir, score }) = queue.pop() {
let min = &mut costs[pos];
min[dir as usize] = min[dir as usize].min(score);
if !seen.insert((pos, dir)) {
continue;
}
if self.map[pos] == Tile::End {
return (costs, score);
}
let next = dir.wrapping_advance(pos);
if self.map.contains(next) && self.map[next] != Tile::Wall {
queue.push(Item::new(next, dir, score + 1));
}
for dir in [dir.turn_left(), dir.turn_right()] {
queue.push(Item::new(pos, dir, score + 1000));
}
}
unreachable!("No path found")
}
/// Walks backwards from the end using a BFS to find all the tiles that are
/// on any of the shortest path.
fn reverse(&self, mut scores: Grid<[u32; 4]>) -> u32 {
let mut seen = HashSet::new();
let mut seen_queue = vec![];
let end_lowest = scores.get(self.end).unwrap();
for dir in Direction::ALL {
let min_cost = end_lowest[dir as usize];
seen_queue.push(Item::new(self.end, dir, min_cost));
}
while let Some(item) = seen_queue.pop() {
seen.insert(item.pos);
if item.pos == self.start {
continue;
}
let next = item.dir.opposite().wrapping_advance(item.pos);
for next in [
Item::new(next, item.dir, item.score - 1),
Item::new(item.pos, item.dir.turn_left(), item.score - 1000),
Item::new(item.pos, item.dir.turn_right(), item.score - 1000),
] {
if self.map.contains(next.pos)
&& self.map[next.pos] != Tile::Wall
&& next.score == scores.get(next.pos).unwrap()[next.dir as usize]
{
scores.get_mut(next.pos).unwrap()[next.dir as usize] = u32::MAX;
seen_queue.push(next);
}
}
}
seen.len() as u32
}
}
impl Item {
fn new(pos: Vec2<usize>, dir: Direction, score: u32) -> Self {
Self { pos, dir, score }
}
}
impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
other.score.cmp(&self.score)
}
}
impl PartialOrd for Item {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 7036.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 45.into());
}
}