Skip to main content

aoc/year2023/
day23.rs

1//! # A Long Walk
2//!
3//! The [longest path problem](https://en.wikipedia.org/wiki/Longest_path_problem) is NP-hard and
4//! requires an exhaustive search through all possible permutations. To speed things up we use
5//! several tricks to reduce the complexity.
6//!
7//! ## Compression
8//!
9//! First we "compress" the maze into a much smaller simpler graph. For example, the following maze
10//! converts into an undirected weighted graph.
11//!
12//! ```none
13//! #.#####
14//! #....##    Start - A - B
15//! ##.#.## =>         |   |
16//! ##....#            C - D - End   (edge weights are 2)
17//! #####.#
18//! ```
19//!
20//! Each actual input forms a graph of the same shape but with different edge weights that
21//! looks like:
22//!
23//! ```none
24//! Start -  a - b - c - d - e
25//!          |   |   |   |   | \
26//!          f - A - B - C - D - g
27//!          |   |   |   |   |   |
28//!          h - E - F - G - H - k
29//!          |   |   |   |   |   |
30//!          m - K - M - N - P - n
31//!          |   |   |   |   |   |
32//!          p - Q - R - S - T - q
33//!            \ |   |   |   |   |
34//!              r - s - t - u - v - End
35//! ```
36//!
37//! ## Conversion to grid
38//!
39//! Next we convert this graph into a 6 x 6 square graph that can be represented in an array. The
40//! start and end are moved to the corners and extra nodes added to the other corners.
41//!
42//! ```none
43//! Start - b - c - d - e - e`
44//!     |   |   |   |   |   |
45//!     f - A - B - C - D - g
46//!     |   |   |   |   |   |
47//!     h - E - F - G - H - k
48//!     |   |   |   |   |   |
49//!     m - K - M - N - P - n
50//!     |   |   |   |   |   |
51//!     p - Q - R - S - T - q
52//!     |   |   |   |   |   |
53//!     p`- r - s - t - u - End
54//! ```
55//!
56//! ## Dynamic programming
57//!
58//! For a 6 x 6 grid graph there are 1262816 total possible rook walks, given by
59//! [OEIS A007764](https://oeis.org/A007764). However, since we want the longest path it only makes
60//! sense to consider the paths that visit the most possible nodes, in this case 35 (we have to
61//! skip 1). There are only 10180 of these paths making it much faster.
62//!
63//! A row by row dynamic programming approach from top to bottom finds these paths. For each row
64//! we calculate all possible next rows. Interestingly, it turns out that there are only 76 possible
65//! different rows. Then at each y coordinate we **deduplicate** rows to find the maximum value.
66//! This is the most important optimization as it means that each row is at most 76 elements
67//! instead of growing exponentially (76², 76³, ...).
68//!
69//! ## Example paths
70//!
71//! Using `S` to represent the start of a line segment and `E` to represent the end, the starting
72//! state looks like `S.....` and the end state `.....S`. One example:
73//!
74//! ```none
75//! Start    S..... |
76//! Row 0    ..SS.E └─┐┌─┐
77//! Row 1    S..S.E ┌─┘|.|
78//! Row 2    ..SSE. └─┐|┌┘
79//! Row 3    SE...S ┌┐└┘└┐
80//! Row 4    S..... |└───┘
81//! Row 5    .....S └────┐
82//! End      .....S      |
83//! ```
84//!
85//! Another example:
86//!
87//! ```none
88//! Start    S..... |
89//! Row 0    .SSESE └┐┌┐┌┐
90//! Row 1    S.SESE ┌┘||||
91//! Row 2    ...SSE └─┘|||
92//! Row 3    S.E..S ┌─┐└┘|
93//! Row 4    S..... |.└──┘
94//! Row 5    .....S └────┐
95//! End      .....S      |
96//! ```
97//!
98//! ## Next row generation
99//!
100//! To create the next row from a given row, there are 5 possibilities for each of the 6 columns.
101//!
102//! ### Leave a blank space, skipping over the column.
103//!
104//! We do this at most once per row. For example:
105//!
106//! ```none
107//! Previous .SSESE └┐┌┐┌┐
108//! Current  .....S .└┘└┘|
109//!                 ^ Blank space
110//! ```
111//!
112//! ### Start a new (start, end) pair of lines.
113//!
114//! All lines must eventually connect so we must create lines in pairs. For example:
115//!
116//! ```none
117//! Previous .....S └────┐
118//! Current  S...ES ┌───┐|
119//!                 ^   ^
120//!                 New pair
121//! ```
122//!
123//! ### Continue a straight line down from the previous row.
124//!
125//! The line stays the same kind (`S` or `E`).
126//!
127//! ```none
128//! Previous .....S └────┐
129//! Current  S...ES ┌───┐|
130//!                      ^ Continuation
131//! ```
132//!
133//! ### Move a previous downwards line to the left or right into a different column.
134//!
135//! The line stays the same kind (`S` or `E`).
136//!
137//! ```none
138//! Previous .....S └────┐
139//! Current  S..... ┌────┘
140//!                 ^ Move
141//! ```
142//!
143//! ### Join two open segments from a previous row.
144//!
145//! A restriction is that we can't create closed cycles that don't connect to the start or end,
146//! as this would skip several nodes. For example, this is not allowed:
147//!
148//! ```none
149//! Previous .....S |┌───┐
150//! Current  S..... |└───┘
151//!                  ^ Closed cycles not allowed
152//! ```
153//!
154//! We implement this by not joining any (`S`, `E`) pairs in that order. Joining the reverse order
155//! (`E`, `S`) is allowed.
156//!
157//! Finally, there are two special rules when joining two nested line segments.
158//! When joining (`S`, `S`) the next `E` converts to an `S` to maintain balance.
159//!
160//! ```none
161//! Previous S..E ┌──┐
162//! Previous SSEE |┌┐|
163//! Current  ..SE └┘||
164//! ```
165//!
166//! When joining (`E`, `E`) the previous `S` converts to an `E` to maintain balance.
167//!
168//! ```none
169//! Previous S..E ┌──┐
170//! Previous SSEE |┌┐|
171//! Current  SE.. ||└┘
172//! ```
173use std::collections::VecDeque;
174
175use crate::util::bitset::*;
176use crate::util::grid::*;
177use crate::util::hash::*;
178use crate::util::point::*;
179
180/// We only use 6 elements but 8 is faster to hash.
181type Row = [u8; 8];
182
183/// Undirected weighted graph representing the compressed maze.
184struct Graph {
185    start: Point,
186    end: Point,
187    edges: FastMap<Point, Vec<Point>>,
188    weight: FastMap<(Point, Point), u32>,
189}
190
191/// Distilled two-dimensional array of only weights.
192pub struct Input {
193    extra: u32,
194    horizontal: [[u32; 6]; 6],
195    vertical: [[u32; 6]; 6],
196}
197
198/// Simplify input for faster processing.
199pub fn parse(input: &str) -> Input {
200    // Convert the raw maze input into a compressed graph.
201    let graph = compress(input);
202    // Convert graph to a 6x6 square grid.
203    graph_to_grid(&graph)
204}
205
206/// The graph is directed so the only allowed steps are down or to the right. The maximum value
207/// for any cell is the maximum of either the cell to the left or above.
208pub fn part1(input: &Input) -> u32 {
209    let mut total = [[0; 6]; 6];
210
211    for y in 0..6 {
212        for x in 0..6 {
213            let left = if x > 0 { total[y][x - 1] + input.horizontal[y][x - 1] } else { 0 };
214            let above = if y > 0 { total[y - 1][x] + input.vertical[y - 1][x] } else { 0 };
215            total[y][x] = left.max(above);
216        }
217    }
218
219    input.extra + total[5][5]
220}
221
222/// Graph is undirected so we can also move up or to the right.
223pub fn part2(input: &Input) -> u32 {
224    let start = [b'S', 0, 0, 0, 0, 0, 0, 0];
225    let end = [0, 0, 0, 0, 0, b'S', 0, 0];
226
227    // Compute all possible different 76 rows and the next possible row.
228    let mut todo = VecDeque::from([start]);
229    let mut seen = FastSet::build([start]);
230    let mut graph = FastMap::new();
231
232    while let Some(row) = todo.pop_front() {
233        let mut neighbors = Vec::new();
234        dfs(&mut neighbors, row, [0; 8], 0, false, 0, 0);
235
236        for &(next, ..) in &neighbors {
237            if seen.insert(next) {
238                todo.push_back(next);
239            }
240        }
241
242        graph.insert(row, neighbors);
243    }
244
245    // Step through each row of the grid, keeping track of the maximum value for each
246    // row type.
247    let mut current = FastMap::build([((start, false), 0)]);
248    let mut next = FastMap::new();
249
250    for y in 0..6 {
251        for ((row, gap), steps) in current.drain() {
252            for &(next_row, next_gap, horizontal, vertical) in &graph[&row] {
253                // Only 1 gap total is allowed, otherwise we can make a longer path.
254                if gap && next_gap {
255                    continue;
256                }
257
258                // The bit sets represent the horizontal and vertical moves from the previous row.
259                let extra = horizontal.biterator().map(|x| input.horizontal[y][x]).sum::<u32>()
260                    + vertical.biterator().map(|x| input.vertical[y][x]).sum::<u32>();
261
262                // De-duplicate states so that each row has at most 76 states.
263                let e = next.entry((next_row, gap || next_gap)).or_insert(0);
264                *e = (*e).max(steps + extra);
265            }
266        }
267
268        (current, next) = (next, current);
269    }
270
271    // The maximum path must have skipped 1 node.
272    input.extra + current[&(end, true)]
273}
274
275/// Convert maze to undirected graph.
276fn compress(input: &str) -> Graph {
277    let mut grid = Grid::parse(input);
278
279    // Move start and end away from edge.
280    let start = Point::new(1, 1);
281    let end = Point::new(grid.width - 2, grid.height - 2);
282
283    // Modify edge of grid to remove the need for boundary checks.
284    grid[start + UP] = b'#';
285    grid[end + DOWN] = b'#';
286
287    // BFS to find distances between POIs. Points of interest are the start, the end and junctions.
288    let mut poi = VecDeque::from([start]);
289    let mut seen = FastSet::new();
290    let mut edges = FastMap::new();
291    let mut weight = FastMap::new();
292
293    grid[end] = b'P';
294
295    while let Some(from) = poi.pop_front() {
296        // Mark this POI as done.
297        grid[from] = b'#';
298
299        for direction in ORTHOGONAL {
300            if grid[from + direction] != b'#' {
301                let mut to = from + direction;
302                let mut cost = 1;
303
304                while grid[to] != b'P' {
305                    let mut neighbors =
306                        ORTHOGONAL.iter().map(|&o| to + o).filter(|&n| grid[n] != b'#');
307                    let next = neighbors.next().unwrap();
308
309                    // More than 1 neighbor means that we've reached a junction.
310                    // Mark it as a POI then stop.
311                    if neighbors.next().is_some() {
312                        grid[to] = b'P';
313                        break;
314                    }
315
316                    // Follow maze path toward next POI.
317                    grid[to] = b'#';
318                    to = next;
319                    cost += 1;
320                }
321
322                // Graph is undirected so add both edges.
323                edges.entry(from).or_insert_with(Vec::new).push(to);
324                edges.entry(to).or_insert_with(Vec::new).push(from);
325                weight.insert((from, to), cost);
326                weight.insert((to, from), cost);
327
328                // Queue POI for processing if we haven't seen it before.
329                if seen.insert(to) {
330                    poi.push_back(to);
331                }
332            }
333        }
334    }
335
336    Graph { start, end, edges, weight }
337}
338
339/// Convert graph to 6 x 6 two-dimensional array of weights.
340fn graph_to_grid(graph: &Graph) -> Input {
341    let Graph { start, end, edges, weight } = graph;
342
343    // There's only 1 edge from both the start and end nodes, so we always have to travel these
344    // steps. Add 2 steps to account for moving the start and end positions in the previous step.
345    let extra = 2 + weight[&(*start, edges[start][0])] + weight[&(*end, edges[end][0])];
346
347    // Perimeter nodes only have 3 edges. Interior nodes have 4 edges.
348    let mut seen = FastSet::new();
349    let mut next_perimeter = |point: &Point| {
350        *edges[point].iter().find(|&&next| edges[&next].len() == 3 && seen.insert(next)).unwrap()
351    };
352
353    let mut grid = [[ORIGIN; 6]; 6];
354    let mut horizontal = [[0; 6]; 6];
355    let mut vertical = [[0; 6]; 6];
356
357    // Place start in top left.
358    grid[0][0] = next_perimeter(start);
359
360    // Fill out top edge and left edge. Since the graph is square it doesn't matter which of the
361    // 2 children becomes top and which becomes left.
362    for i in 1..5 {
363        let left = grid[0][i - 1];
364        let above = grid[i - 1][0];
365
366        let next_left = next_perimeter(&left);
367        let next_above = next_perimeter(&above);
368
369        grid[0][i] = next_left;
370        grid[i][0] = next_above;
371        horizontal[0][i - 1] = weight[&(left, next_left)];
372        vertical[i - 1][0] = weight[&(above, next_above)];
373    }
374
375    // Add two extra corners by duplicating the last node in the row or column.
376    // This won't affect the overall path as the weight of the added edge is 0.
377    grid[0][5] = grid[0][4];
378    grid[5][0] = grid[4][0];
379
380    // Add remaining interior nodes.
381    for y in 1..6 {
382        for x in 1..6 {
383            let left = grid[y][x - 1];
384            let above = grid[y - 1][x];
385
386            let (&next, _) = edges
387                .iter()
388                .find(|&(&k, v)| v.contains(&above) && v.contains(&left) && seen.insert(k))
389                .unwrap();
390
391            grid[y][x] = next;
392            horizontal[y][x - 1] = weight[&(left, next)];
393            vertical[y - 1][x] = weight[&(above, next)];
394        }
395    }
396
397    Input { extra, horizontal, vertical }
398}
399
400/// Modified depth-first search that only allows rows that skip one node.
401fn dfs(
402    result: &mut Vec<(Row, bool, u32, u32)>,
403    previous: Row,
404    current: Row,
405    start: usize,
406    gap: bool,
407    mut horizontal: u32,
408    vertical: u32,
409) {
410    // We're done, push the result to the possible successors.
411    if start == 6 {
412        result.push((current, gap, horizontal, vertical));
413        return;
414    }
415
416    // Previous row above has no vertical descending path.
417    if previous[start] == 0 {
418        // Skip at most 1 column per row.
419        if !gap {
420            dfs(result, previous, current, start + 1, true, horizontal, vertical);
421        }
422
423        for end in (start + 1)..6 {
424            horizontal |= 1 << (end - 1);
425
426            if previous[end] == 0 {
427                // Start a new path pair.
428                let mut next = current;
429                next[start] = b'S';
430                next[end] = b'E';
431
432                let vertical = vertical | (1 << start) | (1 << end);
433
434                dfs(result, previous, next, end + 1, gap, horizontal, vertical);
435            } else {
436                // Move an existing path.
437                let mut next = current;
438                next[start] = previous[end];
439
440                let vertical = vertical | (1 << start);
441
442                dfs(result, previous, next, end + 1, gap, horizontal, vertical);
443                break;
444            }
445        }
446    } else {
447        // Continue vertical path straight down.
448        let mut next = current;
449        next[start] = previous[start];
450        dfs(result, previous, next, start + 1, gap, horizontal, vertical | (1 << start));
451
452        for end in (start + 1)..6 {
453            horizontal |= 1 << (end - 1);
454
455            if previous[end] == 0 {
456                // Move existing path.
457                let mut next = current;
458                next[end] = previous[start];
459
460                let vertical = vertical | (1 << end);
461
462                dfs(result, previous, next, end + 1, gap, horizontal, vertical);
463            } else {
464                // Merge two path segments.
465                match (previous[start], previous[end]) {
466                    // No other changes needed.
467                    (b'E', b'S') => {
468                        dfs(result, previous, current, end + 1, gap, horizontal, vertical);
469                    }
470                    // Convert previous S to E.
471                    (b'E', b'E') => {
472                        let mut next = current;
473
474                        if let Some(i) = current[..start].iter().rposition(|&b| b == b'S') {
475                            next[i] = b'E';
476                        }
477
478                        dfs(result, previous, next, end + 1, gap, horizontal, vertical);
479                    }
480                    // Convert next E to S.
481                    (b'S', b'S') => {
482                        let mut modified = previous;
483                        let mut level = 0;
484
485                        for i in (end + 1)..6 {
486                            match previous[i] {
487                                b'S' => level += 1,
488                                b'E' if level == 0 => {
489                                    modified[i] = b'S';
490                                    break;
491                                }
492                                b'E' => level -= 1,
493                                _ => (),
494                            }
495                        }
496
497                        dfs(result, modified, current, end + 1, gap, horizontal, vertical);
498                    }
499                    _ => (), // (S, E) not allowed
500                }
501                break;
502            }
503        }
504    }
505}