|
| 1 | +use std::collections::VecDeque; |
| 2 | + |
| 3 | +use mygrid::{ |
| 4 | + direction::{Direction, DOWN, LEFT, RIGHT, UP}, |
| 5 | + grid::Grid, |
| 6 | + point::Point, |
| 7 | +}; |
| 8 | + |
| 9 | +advent_of_code::solution!(16); |
| 10 | + |
| 11 | +pub fn part_one(input: &str) -> Option<u64> { |
| 12 | + let (grid, start) = Grid::new_from_str_capture_start(input, &|c| c, &|c| c == 'S'); |
| 13 | + let target_pos = grid |
| 14 | + .iter_item_and_position() |
| 15 | + .filter(|&(_, c)| *c == 'E') |
| 16 | + .map(|(p, _)| p) |
| 17 | + .next() |
| 18 | + .unwrap(); |
| 19 | + |
| 20 | + #[derive(Debug, PartialEq, Eq)] |
| 21 | + struct State { |
| 22 | + pos: Point, |
| 23 | + dir: Direction, |
| 24 | + cost: u64, |
| 25 | + } |
| 26 | + |
| 27 | + let mut q = VecDeque::new(); |
| 28 | + q.push_back(State { |
| 29 | + pos: start, |
| 30 | + dir: RIGHT, |
| 31 | + cost: 0, |
| 32 | + }); |
| 33 | + |
| 34 | + let mut best_cost_grid = Grid::new(grid.width, grid.height, [u64::MAX; 4]); |
| 35 | + let dir_to_index = |d: Direction| match d { |
| 36 | + RIGHT => 0, |
| 37 | + DOWN => 1, |
| 38 | + LEFT => 2, |
| 39 | + UP => 3, |
| 40 | + _ => unreachable!(), |
| 41 | + }; |
| 42 | + |
| 43 | + while let Some(s) = q.pop_front() { |
| 44 | + if s.pos == target_pos { |
| 45 | + best_cost_grid[s.pos][dir_to_index(s.dir)] = s.cost; |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + let right = s.dir.rotate_clockwise(); |
| 50 | + let left = s.dir.rotate_counterclockwise(); |
| 51 | + |
| 52 | + for &(pos, dir, cost) in [ |
| 53 | + (s.pos + s.dir, s.dir, s.cost + 1), |
| 54 | + (s.pos + right, right, s.cost + 1000 + 1), |
| 55 | + (s.pos + left, left, s.cost + 1000 + 1), |
| 56 | + ] |
| 57 | + .iter() |
| 58 | + { |
| 59 | + if grid[pos] == '#' { |
| 60 | + continue; |
| 61 | + } |
| 62 | + let best_cost = best_cost_grid[pos][dir_to_index(dir)]; |
| 63 | + if best_cost <= cost { |
| 64 | + continue; |
| 65 | + } |
| 66 | + best_cost_grid[pos][dir_to_index(dir)] = cost; |
| 67 | + |
| 68 | + let new_state = State { pos, dir, cost }; |
| 69 | + q.push_back(new_state); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + let min_cost = *best_cost_grid[target_pos].iter().min().unwrap(); |
| 74 | + Some(min_cost) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn part_two(input: &str) -> Option<u64> { |
| 78 | + let (grid, start) = Grid::new_from_str_capture_start(input, &|c| c, &|c| c == 'S'); |
| 79 | + let target_pos = grid |
| 80 | + .iter_item_and_position() |
| 81 | + .filter(|&(_, c)| *c == 'E') |
| 82 | + .map(|(p, _)| p) |
| 83 | + .next() |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + #[derive(Debug, PartialEq, Eq)] |
| 87 | + struct State { |
| 88 | + path: Vec<Point>, |
| 89 | + pos: Point, |
| 90 | + dir: Direction, |
| 91 | + cost: u64, |
| 92 | + } |
| 93 | + |
| 94 | + let mut q = VecDeque::new(); |
| 95 | + let mut path = Vec::with_capacity(1024); |
| 96 | + path.push(start); |
| 97 | + q.push_back(State { |
| 98 | + path, |
| 99 | + pos: start, |
| 100 | + dir: RIGHT, |
| 101 | + cost: 0, |
| 102 | + }); |
| 103 | + |
| 104 | + let base_false_grid = Grid::new(grid.width, grid.height, false); |
| 105 | + let mut best_spots_grid = base_false_grid.clone(); |
| 106 | + let mut best_target_cost = u64::MAX; |
| 107 | + let mut best_cost_grid = Grid::new(grid.width, grid.height, [u64::MAX; 4]); |
| 108 | + let dir_to_index = |d: Direction| match d { |
| 109 | + RIGHT => 0, |
| 110 | + DOWN => 1, |
| 111 | + LEFT => 2, |
| 112 | + UP => 3, |
| 113 | + _ => unreachable!(), |
| 114 | + }; |
| 115 | + |
| 116 | + while let Some(s) = q.pop_front() { |
| 117 | + if s.pos == target_pos { |
| 118 | + if best_target_cost < s.cost { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + // reset best_spots_grid if we found a better path |
| 123 | + if best_target_cost > s.cost { |
| 124 | + best_spots_grid = base_false_grid.clone(); |
| 125 | + } |
| 126 | + |
| 127 | + for &p in s.path.iter() { |
| 128 | + best_spots_grid[p] = true; |
| 129 | + } |
| 130 | + |
| 131 | + best_target_cost = s.cost; |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + best_cost_grid[s.pos][dir_to_index(s.dir)] = s.cost; |
| 136 | + |
| 137 | + let right = s.dir.rotate_clockwise(); |
| 138 | + let left = s.dir.rotate_counterclockwise(); |
| 139 | + for &(pos, dir, cost) in [ |
| 140 | + (s.pos + s.dir, s.dir, s.cost + 1), |
| 141 | + (s.pos + left, left, s.cost + 1000 + 1), |
| 142 | + (s.pos + right, right, s.cost + 1000 + 1), |
| 143 | + ] |
| 144 | + .iter() |
| 145 | + { |
| 146 | + if grid[pos] == '#' { |
| 147 | + continue; |
| 148 | + } |
| 149 | + let best_cost_to_next_pos = best_cost_grid[pos][dir_to_index(dir)]; |
| 150 | + if best_cost_to_next_pos < cost { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + if cost > best_target_cost { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + let mut new_path = s.path.clone(); |
| 159 | + new_path.push(pos); |
| 160 | + let new_state = State { |
| 161 | + path: new_path, |
| 162 | + pos, |
| 163 | + dir, |
| 164 | + cost, |
| 165 | + }; |
| 166 | + q.push_back(new_state); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + let tile_count = best_spots_grid.iter().filter(|&&b| b).count(); |
| 171 | + Some(tile_count as u64) |
| 172 | +} |
| 173 | + |
| 174 | +#[cfg(test)] |
| 175 | +mod tests { |
| 176 | + use super::*; |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test_part_one_1() { |
| 180 | + let result = part_one(&advent_of_code::template::read_file_part( |
| 181 | + "examples", DAY, 1, |
| 182 | + )); |
| 183 | + assert_eq!(result, Some(7036)); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_part_one_2() { |
| 188 | + let result = part_one(&advent_of_code::template::read_file_part( |
| 189 | + "examples", DAY, 2, |
| 190 | + )); |
| 191 | + assert_eq!(result, Some(11048)); |
| 192 | + } |
| 193 | + |
| 194 | + #[test] |
| 195 | + fn test_part_two_1() { |
| 196 | + let result = part_two(&advent_of_code::template::read_file_part( |
| 197 | + "examples", DAY, 1, |
| 198 | + )); |
| 199 | + assert_eq!(result, Some(45)); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_part_two_2() { |
| 204 | + let result = part_two(&advent_of_code::template::read_file_part( |
| 205 | + "examples", DAY, 2, |
| 206 | + )); |
| 207 | + assert_eq!(result, Some(64)); |
| 208 | + } |
| 209 | +} |
0 commit comments