-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path18.rs
More file actions
284 lines (266 loc) · 13.1 KB
/
18.rs
File metadata and controls
284 lines (266 loc) · 13.1 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::time::Instant;
use std::collections::{HashMap, HashSet, VecDeque};
use itertools::Itertools;
const INPUT: [&str; 81] = [
"#################################################################################",
"#.............#...#...O.#.#...........#.#...#.........#.......#.....#.......#.Z.#",
"#####.#######.#H#.#.###.#.#.#####.###.#.#.###.#####.#.#.#####.#.###.#.###.###.#.#",
"#.....#.#...#.#.#...#.#.#.#.#.#...#.....#.#...#...#.#.#...#w..#...#...#.#.....#.#",
"#.#####.#.#.#.#.#####.#.#.#B#.#.#########.#.###.#.#.#####.#.#####.#####.#######.#",
"#.#...#...#.....#...#.#.#.....#.........#.#.#...#.#.....#.#...#..y..#...#.....#.#",
"#.#.#.#.#########.#.#.#.###########.###.#.#.#.#######.###.###.#####.#.#.#.###.#.#",
"#.#.#...#.........#.#.......#.....#...#.#.#.#.#.....#.....#.#.#...#...#.#.#...#.#",
"#.###.###.#########.#######.#.###.#####.#.#.#.#.###.#######.#.#.#.#####.#.#.###.#",
"#...#.#.....#x....#.#..f..#...#...#...#.#.#.#.....#.....#i..#...#.#...#.#.#.#...#",
"#.#.#.#.#####.###.#.#A###.#####.###.#.#.#.#.#########.#.#.#.#####.#.#.###.#.#.#.#",
"#.#.#.#.#...#.#.#.#...#.#.#...#.#.T.#...#.#...#.....#.#...#.#.#..e#.#.....#.#.#.#",
"###.###.#X#.#.#.#.#.###.#.###.#.###.###.#.###.#.###.#.#####.#.#.###.#######.#.#.#",
"#...#d..#.#...#.#.#.....#...#.#...#...#.#...#.#.#.....#.......#.#.....#.#...#.#.#",
"#.###.###.#####R#.#####.###.#.###.###.#.#.#.#.#.###########.###.#.###.#.#.#####.#",
"#...#.#.#...#.......#...#...#...#.....#.#.#...#.....#.....#.#...#...#.#...#...#.#",
"###E#.#.###.#######.#####.###.#.#######.#.#####.###N#.###.###.###.###.#.###.#.#.#",
"#.#...#...#..c..#...#r..F.#...#.....#.#.#...#.#.#.#.#...#.#...#...#...#.....#.#.#",
"#.#####.#######.###.#.#######.#####.#.#.###.#.#.#.#.###.#.#.###.###.#########.#.#",
"#z....#.......#...#...#.....#s#.......#.#.#.#.....#...#.#...#v..#.#...#.....#...#",
"#.#.###.#.#######.#####G###.#.#######.#.#.#.#####.#.###.#######.#.###.#.#######.#",
"#.#.#...#.......#...#...#.#.#...#...#.#.#.#.....#.#...#.D.....#.....#.#.......#.#",
"###.#.#########.###C#####.#.#.#.#.#.###.#.#####.#####.###############.#.#####.#.#",
"#...#...#.....#...#....g....#.#.#.#.....#.....#.....#...........#.....#.....#...#",
"#.#####.#.###.#.#############.#.#.#######.#########.#.#######.#.#.#########.#####",
"#.....#...#...#.#.........#...#.#.#.....#.........#.#.#.....#.#...#.......#.#..u#",
"#Q#.#######.###.#.#.#.#####.###.#.#####.#.#######.#.###.###.#.#####.###.#.#.#.###",
"#.#.......#.#...#.#.#.#...#.#.#...#...#.#.#.....#.#.#...#.#.#...#.....#.#.#.#...#",
"#.#####.###.#.###.#.###.#.#.#.#####.#.#.#.#.###.#.#.#.###.#.#.#.#######.#.#.#.#.#",
"#...#...#...#.....#.....#...#.......#...#.#.#.#.K.#...#.#...#.#.#.......#.#.#.#.#",
"###.#.###.#####################.###.#####.#.#.#.#######.#.#####.#.#####.###.#.#.#",
"#...#.....#...#.....#.........#...#.#...#.#.#...#.......#...#...#...#...#...#.#.#",
"#.#########.###.###.#.###.###.###.#.#.#.###.#####.#.###.###.#.###.#.#.###.#####.#",
"#.#.....#.........#...#...#.#.#.#.#.#.#.#...#.....#.#l..#.#.#...#.#.#...#...#...#",
"#.###.#.###.###########.###.#.#.#.#.#.#.#.###.#####.#.###.#.###.#.#.#.#####.#.#.#",
"#.#...#...#...#...#.....#...#.#...#...#.#...#.#.#...#.....#.....#.#.#.#...#.#.#.#",
"#.#.#####.#.###.#.#.#####.###.#########.#.#.#.#.#.#################.###.#.#P#.#.#",
"#.#.#.....#.#...#.#...#.#...#.#.....#...#.#.#.#.#.#.................#...#...#.#.#",
"#.#.#.#######.###.###.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#########.#######.#######.#.#",
"#.U.#p........#....j..#...#.....#.....#...#.....#...........#...........J.....#.#",
"#######################################.@.#######################################",
"#...#.....#...#.......#.........#.........#.....#.....#.....#.....#.............#",
"#.#.#.###.###.#.#L###.#.#####.###.#.###.#.#.#.###.#.###.#.#.#.#.#.#.#########.#.#",
"#.#...#.....#...#...#.#.#...#.....#...#.#...#.....#.....#.#.#.#.#.#.#.......#q#.#",
"#.#.#######.#.#####.#.###.#.#########.#.#.###############.###.#.#.###.#####.###.#",
"#.#.#.....#.#...#...#.....#.#.........#.#.#.....#.....#...#...#.#.....#...#...#.#",
"#.#.#.###.#.#####.#########.#.#########.#.#.#.#.#.###.#.###.###.#.#####.#.###.#.#",
"#.#.#...#.#.....#.....#...#...#.......#.#.#.#.#...#...#.#.....#.#.#...#.#...#...#",
"#.#####.#.#####.#####.#.#######.###.###.#.#.#.#####.###.#.#####.#.#.#.#.###.###.#",
"#.....#.#...#...#...#.#...........#...#.#k#.#...#.....#.#.#.....#.#.#.#...#...#.#",
"#####.#.###.###.#.#.#.#.#############.#.#.#.###.#.#####.#.#.#######.#.###.#.###.#",
"#.....#...#...#...#...#.#...........#...#.#...#.#...#...#.#...#...#.#...#.#.....#",
"#.#######.###.#####.#####.#########.#.###.#####.###.#.###.###.#.#.#.###.#.#######",
"#.......#...#.....#.#.....#.......#.#...#.......#.#.#.#...#.#...#.....#.#.#.....#",
"#.###.#####.#####.###.#####.#.#.###.#############.#.#.#.###.###########.#.#.###.#",
"#...#.#.........#...#.#.....#.#.#...#...#...#.....#.#...#...#.......#...#.#.#m..#",
"#.###.#.###########.#.#######.###.###.#.#.#.#.#.###.#####.#.#######.#.###.#.#.#.#",
"#.#...#...#.#.......#...#...#.W...#...#.#.#.#.#.....#.....#...#.....#...#.#.#.#.#",
"###.#.###.#.#.#########.#.#.#####.###.#.#.###.#######.#######.#.###.###.#.###.#.#",
"#...#...#...#...#.....#...#.....#...#.#.#.#...#.........#.......#.#.#...#.....#.#",
"#.#########.###.#.#.###########.###.#.#.#.#.###.#######.#.#######.#.#.#########.#",
"#.........#.#...#.#.#....a......#.#...#.#.#...#.#.......#.#...#.....#.#...#.....#",
"#.#######.#.#.###.#.#############.#####.#.###.#.#.#######.#.###.#####.###.#.#####",
"#.#.....#...#.#...#.....#...#.......#h..#.....#.#.#.......#.#...#...#...#.#.#...#",
"#.###.#.#####.#.#####.#.#.#.#.#####.#.###.#######.#######.#.#####.#.###.#.#.#.#.#",
"#...#.#...#...#.#...#.#.#.#..o#...#...#.#.......#.......#.#.....#.#.#...#.....#.#",
"###.#####.#.#.#.###.#.###.#######.#####.#######.#.#####.#######.#.#.#.###########",
"#.#...#...#.#.#.....#.....#.#.......#...#...#...#.....#.......#.#.#.#...#.......#",
"#.###.#.###.#.#####.#######.#.#I###.###.#.#.#.###########.###.#.#.#.###.#.#####.#",
"#...#...#...#.....#.#.#.....#.#...#t..#.#.#...#.........#.#.#.#...#...#...#.....#",
"#.#####.#.#########.#.#.###.#.###.###.#.#.#####.#######.#.#.#.#######.#####.#####",
"#...M...#.......#...#...#.#...#.#...#...#.......#.....#.#.#.........#.....#.#...#",
"#.#############.#.###.###.#####.###.#############.#####.#.#.#######.#####.#.###.#",
"#.#.....Y...#.#.#.#.#.#..b..#...#.#.#...#...#.........#.#.#.#...#.#.#...#...#...#",
"#.#.#######.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#######.#.#.###.#.#.#.#.#.#####.#.#",
"#...#n....#.#.#...#.......#...#...#...#.#.#.#.#.....#...#.....#.#...#.#.......#.#",
"#####.###.#.#.###################.#####.#.###.#.###.#####V#####.#.###.###.#####.#",
"#.....#...#...#.....#...#...#.....#.....#.....#...#...........#.#...#.#...#.....#",
"#.###########.###.#.#.#.#.#.#######.###.#.#####################.###.###.###.#####",
"#.................#...#...#.........#.S.#.......................#.......#.......#",
"#################################################################################",
];
type Pos = (usize, usize);
type Graph = HashMap<Pos, Vec<(Pos,usize)>>;
type Map = [Vec<char>];
type Path = (usize, usize, usize);
// Remove all nodes that are empty spaces with just one neighbour, i.e dead-ends
fn remove_dead_ends(nodes: &mut HashSet<Pos>, map: &Map) {
loop {
let to_remove = nodes.iter()
.filter(|&&(i,j)| map[i][j] == '.')
.filter(|&&(i,j)| {
let mut n = 0;
if nodes.contains(&(i-1,j)) { n += 1; }
if nodes.contains(&(i+1,j)) { n += 1; }
if nodes.contains(&(i,j-1)) { n += 1; }
if nodes.contains(&(i,j+1)) { n += 1; }
n == 1
})
.cloned()
.collect_vec();
if to_remove.is_empty() { return; }
for pos in &to_remove { nodes.remove(pos); }
}
}
// Contract nodes that are empty spaces and only functions as bridges between other nodes.
// This does not "change" the graph, it simply removes all uninteresting nodes,
// leaving only 3/4-way intersections and doors/keys as nodes in the graph.
fn contract_corridors(g: &mut Graph, map: &Map) {
let to_contract = g.iter()
.filter(|&((x,y), _)| map[*x][*y] == '.')
.filter(|&(_, n)| n.len() == 2)
.map(|(k,_)| *k)
.collect_vec();
for p in &to_contract {
let (p1, d1) = g[p][0];
let (p2, d2) = g[p][1];
g.entry(p1).and_modify(|n| {
let index = n.iter().position(|(pos,_)| pos == p).unwrap();
n[index] = (p2,d1+d2);
});
g.entry(p2).and_modify(|n| {
let index = n.iter().position(|(pos,_)| pos == p).unwrap();
n[index] = (p1,d1+d2);
});
g.remove(&p);
}
}
// This process reduces the huge maze above to just 79 nodes.
fn to_minimized_graph(map: &Map) -> Graph {
let mut nodes = (0..map.len()).cartesian_product(0..map[0].len())
.filter(|&(x,y)| map[x][y] != '#')
.collect::<HashSet<_>>();
remove_dead_ends(&mut nodes, &map);
let mut g = nodes.iter().map(|&(i,j)| {
let n = [(i-1,j), (i+1,j), (i,j-1), (i,j+1)].iter()
.filter(|p| nodes.contains(p))
.map(|&p| (p,1))
.collect();
((i,j), n)
}).collect();
contract_corridors(&mut g, &map);
g
}
// Store keys as bits instead so we can speed up computations via bitwise operations
fn char_to_bit(c: char, offset: u8) -> usize {
match c {
'@' => 1 << 26,
'%' => 1 << 27,
'!' => 1 << 28,
'€' => 1 << 29,
_ => 1 << (c as u8 - offset),
}
}
fn bfs(g: &Graph, map: &Map, start: Pos, end: Pos) -> Option<Path> {
let mut queue = VecDeque::new();
let mut visited = HashSet::new();
queue.push_back(vec![(start,0)]);
let path = loop {
let path = match queue.pop_front() {
Some(path) => path,
None => return None,
};
let (u, _) = *path.last().unwrap();
if u == end { break path; }
visited.insert(u);
g[&u].iter()
.filter(|&(v,_)| !visited.contains(v))
.for_each(|&(v,d)| {
let mut new_path = path.clone();
new_path.push((v,d));
queue.push_back(new_path);
})
};
let doors = path.iter().map(|&((x,y),_)| map[x][y]).filter(|c| c.is_ascii_uppercase()).fold(0, |mask, c| mask | char_to_bit(c, b'A'));
let keys = path.iter().map(|&((x,y),_)| map[x][y]).filter(|c| c.is_ascii_lowercase()).fold(0, |mask, c| mask | char_to_bit(c, b'a'));
let len = path.iter().map(|(_,d)| d).sum();
Some((doors, keys, len))
}
// Compute the length of path between all key pairs,
// what keys are picked up along the way,
// and what doors are required to get there.
fn compute_paths(g: &Graph, map: &Map) -> HashMap<usize, HashMap<usize, Path>> {
let keys = g.keys()
.filter(|&&(x,y)| map[x][y].is_ascii_lowercase() || ['@','%','!','€'].contains(&map[x][y]))
.cloned()
.collect_vec();
let mut paths = HashMap::new();
for i in 0..keys.len() {
let (x1,y1) = keys[i];
let key1 = char_to_bit(map[x1][y1], b'a');
for j in i..keys.len() {
let (x2,y2) = keys[j];
let key2 = char_to_bit(map[x2][y2], b'a');
if let Some(path) = bfs(&g, &map, keys[i], keys[j]) {
paths.entry(key1).or_insert(HashMap::new()).insert(key2, path);
paths.entry(key2).or_insert(HashMap::new()).insert(key1, path);
}
}
}
paths
}
fn shortest_path(
g: &Graph,
paths: &HashMap<usize, HashMap<usize,Path>>,
cache: &mut HashMap<(usize,usize), usize>,
all_keys: usize,
keys: usize,
curr: usize,
) -> usize {
if keys == all_keys { return 0; }
if let Some(&d) = cache.get(&(keys,curr)) { return d; }
let d = paths[&curr].iter()
.filter(|&(key,_)| key & keys == 0)
.filter(|(_,(doors,_,_))| keys & doors == *doors)
.map(|(&key, (_, new_keys, len))|
len + shortest_path(g, paths, cache, all_keys, keys | new_keys, key)
)
.min()
.unwrap();
cache.insert((keys,curr), d);
d
}
fn part_one(map: &Map) -> usize {
let g = to_minimized_graph(&map);
let paths = compute_paths(&g, &map);
let start = char_to_bit('@',0);
let all_keys = (1 << 27) - 1;
shortest_path(&g, &paths, &mut HashMap::new(), all_keys, start, start)
}
fn part_two(map: &mut Map) -> usize {
map[39][39] = '@'; map[40][39] = '#'; map[41][39] = '!';
map[39][40] = '#'; map[40][40] = '#'; map[41][40] = '#';
map[39][41] = '%'; map[40][41] = '#'; map[41][41] = '€';
let g = to_minimized_graph(&map);
let mut paths = compute_paths(&g, &map);
// Any doors this robot cannot open will be opened by
// another robot at some point so we can just remove
// those doors before calculating the shortest path.
['@','%','!','€'].iter().map(|&c| {
let k = char_to_bit(c,0);
let keys = paths[&k].keys().cloned().collect_vec();
let reachable_keys = keys.iter().fold(k, |ks, k| ks | k);
for k in keys {
paths.entry(k).and_modify(|p| {
for (_, (doors,_,_)) in p.iter_mut() {
*doors &= reachable_keys;
}
});
}
shortest_path(&g, &paths, &mut HashMap::new(), reachable_keys, k, k)
}).sum()
}
fn main() {
let now = Instant::now();
let mut map = INPUT.iter().map(|s| s.chars().collect_vec()).collect_vec();
let part_one = part_one(&map);
let part_two = part_two(&mut map);
println!("Part one: {}", part_one);
println!("Part two: {}", part_two);
println!("Time: {}ms", now.elapsed().as_millis());
}