-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_20.rs
224 lines (213 loc) · 8.19 KB
/
day_20.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! https://adventofcode.com/2019/day/20
//! https://adventofcode.com/2019/day/20/input
use std::{
cmp::Reverse,
collections::{hash_map::Entry, BinaryHeap, HashMap, HashSet, VecDeque},
fs::read_to_string,
time::Instant,
};
type Coord = (usize, usize);
const NEIGHBOURS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
#[allow(clippy::type_complexity)]
fn parse(input: &str) -> (Vec<Vec<bool>>, HashMap<Coord, (Coord, isize)>, Coord, Coord) {
let matrix: Vec<Vec<_>> = input.lines().map(|line| line.chars().collect()).collect();
let mut open =
vec![vec![false; matrix.iter().map(|line| line.len()).max().unwrap()]; matrix.len()];
let mut tmp_portals = HashMap::new();
let mut portals_heap = BinaryHeap::new();
for (i, row) in matrix.iter().enumerate() {
for (j, &cell) in row.iter().enumerate() {
match cell {
'#' => open[i][j] = false,
'.' => open[i][j] = true,
char @ 'A'..='Z' => {
tmp_portals.insert((i, j), char);
portals_heap.push(Reverse((i, j)));
}
_ => {}
}
}
}
let mut portal_pairs = HashMap::new();
while let Some(Reverse((i, j))) = portals_heap.pop() {
if !tmp_portals.contains_key(&(i, j)) {
continue;
}
let portal_fst_char = tmp_portals.remove(&(i, j)).unwrap();
let (portal_snd_char, coord) = if tmp_portals.contains_key(&(i, j + 1)) {
let coord = if j + 2 < open[i].len() && open[i][j + 2] {
(i, j + 2)
} else {
(i, j - 1)
};
(tmp_portals.remove(&(i, j + 1)).unwrap(), coord)
} else {
let coord = if i + 2 < open.len() && open[i + 2][j] {
(i + 2, j)
} else {
(i - 1, j)
};
(tmp_portals.remove(&(i + 1, j)).unwrap(), coord)
};
match portal_pairs.entry((portal_fst_char, portal_snd_char)) {
Entry::Vacant(entry) => {
entry.insert((coord, (usize::MAX, usize::MAX)));
}
Entry::Occupied(mut entry) => {
entry.get_mut().1 = coord;
}
}
}
let (mut start, mut end) = ((0, 0), (0, 0));
let mut portals: HashMap<_, (Coord, isize)> = HashMap::new();
let center = (open.len() / 2, open[0].len() / 2);
let mut inner = HashSet::new();
let mut queue = VecDeque::from([center]);
let mut visited = vec![vec![false; open[0].len()]; open.len()];
while let Some(coord @ (i, j)) = queue.pop_front() {
if visited[i][j] {
continue;
}
visited[i][j] = true;
match matrix[i][j] {
' ' | 'A'..='Z' => {
for (ni, nj) in NEIGHBOURS.iter().filter_map(|(di, dj)| {
let (ni, nj) = ((i as isize + di) as usize, (j as isize + dj) as usize);
(ni < open.len() && nj < open[0].len() && !visited[ni][nj]).then_some((ni, nj))
}) {
queue.push_back((ni, nj));
}
}
'.' => {
inner.insert(coord);
}
_ => {}
}
}
for ((fst_char, snd_char), (from, to)) in portal_pairs {
if fst_char == 'A' && snd_char == 'A' {
start = from;
} else if fst_char == 'Z' && snd_char == 'Z' {
end = from;
} else {
let level_diff = if inner.contains(&from) { 1 } else { -1 };
portals.insert(from, (to, level_diff));
portals.insert(to, (from, -level_diff));
}
}
(open, portals, start, end)
}
pub mod part1 {
use std::collections::VecDeque;
use super::{parse, NEIGHBOURS};
pub fn solve(input: &str) -> usize {
let (open, portals, start, end) = parse(input);
let mut visited = vec![vec![false; open[0].len()]; open.len()];
let mut queue = VecDeque::from([(start, 0)]);
while let Some((coord @ (i, j), distance)) = queue.pop_front() {
if visited[i][j] {
continue;
}
if coord == end {
return distance;
}
visited[i][j] = true;
for (ni, nj) in NEIGHBOURS.iter().filter_map(|(di, dj)| {
let (ni, nj) = ((i as isize + di) as usize, (j as isize + dj) as usize);
(ni < open.len() && nj < open[0].len() && open[ni][nj]).then_some((ni, nj))
}) {
queue.push_back(((ni, nj), distance + 1));
}
if let Some(&(warp @ (wi, wj), _)) = portals.get(&coord) {
if wi < visited.len() && wj < visited[0].len() {
queue.push_back((warp, distance + 1));
}
}
}
unreachable!()
}
}
pub mod part2 {
use std::collections::{HashMap, VecDeque};
use super::{parse, NEIGHBOURS};
pub fn solve(input: &str) -> usize {
let (open, portals, start, end) = parse(input);
let mut visited = vec![vec![HashMap::new(); open[0].len()]; open.len()];
let mut queue = VecDeque::from([(start, 0, 0)]);
while let Some((coord @ (i, j), distance, level)) = queue.pop_front() {
if level < 0 || visited[i][j].get(&level).unwrap_or(&usize::MAX) <= &distance {
continue;
}
if coord == end {
if level == 0 {
return distance;
}
continue;
}
visited[i][j].insert(level, distance);
for (ni, nj) in NEIGHBOURS.iter().filter_map(|(di, dj)| {
let (ni, nj) = ((i as isize + di) as usize, (j as isize + dj) as usize);
(ni < open.len() && nj < open[0].len() && open[ni][nj]).then_some((ni, nj))
}) {
queue.push_back(((ni, nj), distance + 1, level));
}
if let Some(&(warp @ (wi, wj), level_diff)) = portals.get(&coord) {
if wi < visited.len() && wj < visited[0].len() {
queue.push_back((warp, distance + 1, level + level_diff));
}
}
}
unreachable!()
}
}
pub fn main(test: bool) {
let test_input = " Z L X W C
Z P Q B K
###########.#.#.#.#######.###############
#...#.......#.#.......#.#.......#.#.#...#
###.#.#.#.#.#.#.#.###.#.#.#######.#.#.###
#.#...#.#.#...#.#.#...#...#...#.#.......#
#.###.#######.###.###.#.###.###.#.#######
#...#.......#.#...#...#.............#...#
#.#########.#######.#.#######.#######.###
#...#.# F R I Z #.#.#.#
#.###.# D E C H #.#.#.#
#.#...# #...#.#
#.###.# #.###.#
#.#....OA WB..#.#..ZH
#.###.# #.#.#.#
CJ......# #.....#
####### #######
#.#....CK #......IC
#.###.# #.###.#
#.....# #...#.#
###.### #.#.#.#
XF....#.# RF..#.#.#
#####.# #######
#......CJ NM..#...#
###.#.# #.###.#
RE....#.# #......RF
###.### X X L #.#.#.#
#.....# F Q P #.#.#.#
###.###########.###.#######.#########.###
#.....#...#.....#.......#...#.....#.#...#
#####.#.###.#######.#######.###.###.#.#.#
#.......#.......#.#.#.#.#...#...#...#.#.#
#####.###.#####.#.#.#.#.###.###.#.###.###
#.......#.....#.#...#...............#...#
#############.#.#.###.###################
A O F N
A A D M "
.to_owned();
let puzzle_input = if test {
test_input
} else {
read_to_string("../inputs/2019/day_20_input.txt").unwrap()
};
let start = Instant::now();
println!("{}", part1::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
let start = Instant::now();
println!("{}", part2::solve(&puzzle_input));
println!("Run in {:?}", start.elapsed());
}