|
| 1 | +//! # Reindeer Maze |
| 2 | +//! |
| 3 | +//! Part one is a normal [Dijkstra](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) |
| 4 | +//! search from start to end. |
| 5 | +//! |
| 6 | +//! Part two we modify the search to check all possible paths. Then we perform a BFS *backwards* |
| 7 | +//! from the end to the finish, using the inverse cost to find all possible paths. |
| 8 | +use crate::util::grid::*; |
| 9 | +use crate::util::hash::*; |
| 10 | +use crate::util::heap::*; |
| 11 | +use crate::util::point::*; |
| 12 | +use std::collections::VecDeque; |
| 13 | + |
| 14 | +type Input = (Grid<u8>, Point, Point); |
| 15 | + |
| 16 | +const DIRECTIONS: [Point; 4] = [RIGHT, DOWN, LEFT, UP]; |
| 17 | + |
| 18 | +pub fn parse(input: &str) -> Input { |
| 19 | + let grid = Grid::parse(input); |
| 20 | + let start = grid.find(b'S').unwrap(); |
| 21 | + let end = grid.find(b'E').unwrap(); |
| 22 | + (grid, start, end) |
| 23 | +} |
| 24 | + |
| 25 | +pub fn part1(input: &Input) -> u32 { |
| 26 | + let (grid, ..) = input; |
| 27 | + let &(_, start, end) = input; |
| 28 | + |
| 29 | + let mut todo = MinHeap::new(); |
| 30 | + let mut seen = grid.same_size_with([u32::MAX; 4]); |
| 31 | + |
| 32 | + todo.push(0, (start, 0)); |
| 33 | + seen[start][0] = 0; |
| 34 | + |
| 35 | + while let Some((cost, (position, direction))) = todo.pop() { |
| 36 | + if position == end { |
| 37 | + // Stop search. |
| 38 | + return cost; |
| 39 | + } |
| 40 | + |
| 41 | + let left = (direction + 3) % 4; |
| 42 | + let right = (direction + 1) % 4; |
| 43 | + let next = [ |
| 44 | + (position + DIRECTIONS[direction], direction, cost + 1), |
| 45 | + (position, left, cost + 1000), |
| 46 | + (position, right, cost + 1000), |
| 47 | + ]; |
| 48 | + |
| 49 | + for (next_position, next_direction, next_cost) in next { |
| 50 | + if grid[next_position] != b'#' && next_cost < seen[next_position][next_direction] { |
| 51 | + todo.push(next_cost, (next_position, next_direction)); |
| 52 | + seen[next_position][next_direction] = next_cost; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + unreachable!() |
| 58 | +} |
| 59 | + |
| 60 | +pub fn part2(input: &Input) -> usize { |
| 61 | + let (grid, ..) = input; |
| 62 | + let &(_, start, end) = input; |
| 63 | + |
| 64 | + // Forwards Dijkstra |
| 65 | + let mut todo = MinHeap::new(); |
| 66 | + let mut seen = grid.same_size_with([u32::MAX; 4]); |
| 67 | + let mut lowest = u32::MAX; |
| 68 | + |
| 69 | + todo.push(0, (start, 0)); |
| 70 | + seen[start][0] = 0; |
| 71 | + |
| 72 | + while let Some((cost, (position, direction))) = todo.pop() { |
| 73 | + if position == end { |
| 74 | + lowest = lowest.min(cost); |
| 75 | + // Keep searching. |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + let left = (direction + 3) % 4; |
| 80 | + let right = (direction + 1) % 4; |
| 81 | + let next = [ |
| 82 | + (position + DIRECTIONS[direction], direction, cost + 1), |
| 83 | + (position, left, cost + 1000), |
| 84 | + (position, right, cost + 1000), |
| 85 | + ]; |
| 86 | + |
| 87 | + for (next_position, next_direction, next_cost) in next { |
| 88 | + if grid[next_position] != b'#' && next_cost < seen[next_position][next_direction] { |
| 89 | + todo.push(next_cost, (next_position, next_direction)); |
| 90 | + seen[next_position][next_direction] = next_cost; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Backwards BFS |
| 96 | + let mut todo = VecDeque::new(); |
| 97 | + let mut path = FastSet::new(); |
| 98 | + |
| 99 | + for direction in 0..4 { |
| 100 | + if seen[end][direction] == lowest { |
| 101 | + todo.push_back((end, direction, lowest)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + while let Some((position, direction, cost)) = todo.pop_front() { |
| 106 | + path.insert(position); |
| 107 | + |
| 108 | + if position == start { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + // Subtract cost |
| 113 | + let left = (direction + 3) % 4; |
| 114 | + let right = (direction + 1) % 4; |
| 115 | + let next = [ |
| 116 | + (position - DIRECTIONS[direction], direction, cost - 1), |
| 117 | + (position, left, cost - 1000), |
| 118 | + (position, right, cost - 1000), |
| 119 | + ]; |
| 120 | + |
| 121 | + for (next_position, next_direction, next_cost) in next { |
| 122 | + if next_cost == seen[next_position][next_direction] { |
| 123 | + todo.push_back((next_position, next_direction, next_cost)); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + path.len() |
| 129 | +} |
0 commit comments