generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.rs
289 lines (262 loc) · 9.22 KB
/
23.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
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
285
286
287
288
289
use std::{
collections::BinaryHeap,
fmt::{Display, Write},
};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tinyvec::ArrayVec;
advent_of_code::solution!(23);
pub fn part_one(input: &str) -> Option<u16> {
let (graph, start, goal) = build_graph(input, true);
// Dijkstras with negative costs to find the maximal path.
let mut best_so_far = vec![0; graph.vertices];
let mut queue = BinaryHeap::new();
queue.push((0, start));
while let Some((so_far, cur)) = queue.pop() {
for &(next, cost) in &graph.adjacency[cur] {
let next_so_far = so_far + cost;
if next_so_far > best_so_far[next] {
best_so_far[next] = next_so_far;
queue.push((next_so_far, next));
}
}
}
Some(best_so_far[goal])
}
pub fn part_two(input: &str) -> Option<u16> {
let (mut graph, mut start, mut goal) = build_graph(input, false);
debug_assert!(graph.vertices <= 64, "some optimizations only work with 64 vertices or less. plus it will be too slow with high vertex counts");
// The start and goal usually only has one connection, so trim it to save on the search space.
let mut trimmed_length = 0;
let mut trim = |mut cur: usize| {
while graph.adjacency[cur].len() == 1 {
let (new_cur, distance) = graph.adjacency[cur][0];
trimmed_length += distance;
graph.adjacency[cur].clear();
let idx = graph.adjacency[new_cur]
.iter()
.position(|&(adj, _)| adj == cur)
.unwrap();
graph.adjacency[new_cur].remove(idx);
cur = new_cur;
}
cur
};
goal = trim(goal);
start = trim(start);
// Find all paths to depth N iteratively to use rayon on the resulting paths.
const PRESEARCH_DEPTH: u8 = 8;
let mut paths = Vec::new();
let mut stack = Vec::new();
stack.push((start, 1u64 << start, 0, 0));
while let Some((cur, visited, distance, depth)) = stack.pop() {
if depth >= PRESEARCH_DEPTH || cur == goal {
paths.push((cur, visited, distance));
continue;
}
for &(neighbor, cost) in &graph.adjacency[cur] {
let neighbor_bit = 1u64 << neighbor;
if visited & neighbor_bit == 0 {
stack.push((neighbor, visited | neighbor_bit, distance + cost, depth + 1));
}
}
}
paths
.into_par_iter()
.map(|(start, visited, distance)| {
trimmed_length + part_two_recursive_brute_force(&graph, start, goal, visited, distance)
})
.max()
}
fn part_two_recursive_brute_force(
graph: &Graph,
cur: usize,
goal: usize,
visited: u64,
so_far: u16,
) -> u16 {
if cur == goal {
return so_far;
}
let mut max = 0;
for &(neighbor, cost) in &graph.adjacency[cur] {
let neighbor_bit = 1 << neighbor;
if (visited & neighbor_bit) == 0 {
let next_so_far = part_two_recursive_brute_force(
graph,
neighbor,
goal,
visited | neighbor_bit,
so_far + cost,
);
max = max.max(next_so_far);
}
}
max
}
fn build_graph(input: &str, obey_slopes: bool) -> (Graph, usize, usize) {
let mut graph = Graph::default();
let input = input.as_bytes();
let width = input.iter().position(|&ch| ch == b'\n').unwrap();
let height = (input.len() + 1) / (width + 1);
let start = Coordinate::new(0, 1);
let goal = Coordinate::new(height - 1, width - 2);
let mut visited = vec![None; width * height];
visited[1] = Some(0);
graph.vertices = 1;
graph.adjacency.resize_with(1, ArrayVec::default);
let mut stack = vec![(0, start, Coordinate::new(0, 0))];
// A helper to get valid neighbors.
let find_neighbors = |cur: Coordinate, prev: Coordinate, obey_slopes: bool| {
Direction::ALL
.into_iter()
.flat_map(|d| cur.move_in_dir(d, width, height).map(|n| (n, d)))
.filter_map(|(n, d)| {
if n == prev {
return None;
}
let index = n.row * (width + 1) + n.col;
let cell = input[index];
use Direction::{Down, Left, Right, Up};
match (cell, d, obey_slopes) {
(b'#', _, _) => return None,
(_, _, false) | (b'.', _, _) => {}
(b'^', Up, _) | (b'>', Right, _) | (b'<', Left, _) | (b'v', Down, _) => {}
_ => return None,
}
Some(n)
})
.collect::<ArrayVec<[_; 4]>>()
};
let mut goal_id = 0;
while let Some((vertex_id, vertex, prev)) = stack.pop() {
if vertex == goal {
goal_id = vertex_id;
continue;
}
// For each valid neighbor, we want to find the next vertex to add to the stack for
// processing.
let neighbors = find_neighbors(vertex, prev, obey_slopes);
for neighbor in neighbors {
if visited[neighbor.row * width + neighbor.col].is_some() {
continue;
}
let mut cur = neighbor;
let mut prev = vertex;
let mut distance = 1;
let (next_vertex, next_prev) = loop {
let neighbors_ignoring_slopes = find_neighbors(cur, prev, false);
if neighbors_ignoring_slopes.len() > 1 {
// This is a vertex.
break (cur, prev);
}
let next_neighbors = if obey_slopes {
find_neighbors(cur, prev, true)
} else {
neighbors_ignoring_slopes
};
if next_neighbors.is_empty() {
// We hit the goal node.
break (cur, prev);
}
visited[cur.row * width + cur.col] = Some(0);
prev = cur;
cur = next_neighbors[0];
distance += 1;
};
let next_vertex_map_index = next_vertex.row * width + next_vertex.col;
let next_vertex_id = if let Some(idx) = visited[next_vertex_map_index] {
idx
} else {
let idx = graph.vertices;
graph.vertices += 1;
graph
.adjacency
.resize_with(graph.vertices, ArrayVec::default);
visited[next_vertex_map_index] = Some(idx);
stack.push((idx, next_vertex, next_prev));
idx
};
graph.adjacency[vertex_id].push((next_vertex_id, distance));
if !obey_slopes {
graph.adjacency[next_vertex_id].push((vertex_id, distance));
}
}
}
(graph, 0, goal_id)
}
#[derive(Debug, Default, Clone)]
struct Graph {
vertices: usize,
adjacency: Vec<ArrayVec<[(usize, u16); 4]>>,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
struct Coordinate {
row: usize,
col: usize,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Direction {
Up,
Down,
Left,
Right,
}
impl Coordinate {
pub const fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
pub fn move_in_dir(&self, dir: Direction, width: usize, height: usize) -> Option<Coordinate> {
match dir {
Direction::Up => (self.row > 0).then(|| Self::new(self.row - 1, self.col)),
Direction::Down => (self.row + 1 < height).then(|| Self::new(self.row + 1, self.col)),
Direction::Left => (self.col > 0).then(|| Self::new(self.row, self.col - 1)),
Direction::Right => (self.col + 1 < width).then(|| Self::new(self.row, self.col + 1)),
}
}
}
impl Direction {
const ALL: [Self; 4] = [Self::Up, Self::Down, Self::Left, Self::Right];
}
impl Display for Graph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn format_vertex(vertex: usize) -> String {
let mut ret = String::new();
if vertex > 25 {
ret.push(char::from((vertex / 26) as u8 + b'A'));
}
ret.push(char::from((vertex % 26) as u8 + b'A'));
ret
}
for vertex in 0..self.vertices {
if !self.adjacency[vertex].is_empty() {
writeln!(
f,
"{} -> {{{}}}",
format_vertex(vertex),
self.adjacency[vertex]
.iter()
.fold(String::new(), |mut s, &(n, _)| {
write!(&mut s, "{} ", format_vertex(n)).expect("should succeed");
s
})
.trim_end()
)?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(94));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(154));
}
}