|
| 1 | +use std::{collections::VecDeque, convert::identity, u32}; |
| 2 | + |
| 3 | +use aoc_lib::{direction::cardinal::Direction, matrix::Grid}; |
| 4 | +use common::{solution, Answer}; |
| 5 | +use itertools::Itertools; |
| 6 | +use nd_vec::{vector, Vec2}; |
| 7 | + |
| 8 | +solution!("Race Condition", 20); |
| 9 | + |
| 10 | +fn part_a(input: &str) -> Answer { |
| 11 | + Problem::parse(input).solve(2).into() |
| 12 | +} |
| 13 | + |
| 14 | +fn part_b(input: &str) -> Answer { |
| 15 | + Problem::parse(input).solve(20).into() |
| 16 | +} |
| 17 | + |
| 18 | +struct Problem { |
| 19 | + board: Grid<char>, |
| 20 | + start: Vec2<usize>, |
| 21 | + end: Vec2<usize>, |
| 22 | +} |
| 23 | + |
| 24 | +impl Problem { |
| 25 | + fn parse(input: &str) -> Self { |
| 26 | + let board = Grid::parse(input, identity); |
| 27 | + |
| 28 | + let start = board.find('S').unwrap(); |
| 29 | + let end = board.find('E').unwrap(); |
| 30 | + |
| 31 | + Self { board, start, end } |
| 32 | + } |
| 33 | + |
| 34 | + fn solve(&self, max_skip: i32) -> u32 { |
| 35 | + let (sc, ec) = (self.cost_map(self.start), self.cost_map(self.end)); |
| 36 | + let base_cost = sc[self.end]; |
| 37 | + |
| 38 | + let mut out = 0; |
| 39 | + |
| 40 | + for (pos, tile) in self.board.iter() { |
| 41 | + if *tile == '#' || sc[pos] == u32::MAX { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + for (x, y) in (-max_skip..=max_skip).cartesian_product(-max_skip..=max_skip) { |
| 46 | + let offset = vector!(x, y); |
| 47 | + let dist = offset.manhattan_distance(&Vec2::zero()); |
| 48 | + if dist > max_skip { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + let end = pos.try_cast::<i32>().unwrap() + offset; |
| 53 | + if !self.board.contains(end) || self.board[end] == '#' || ec[end] == u32::MAX { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + let cost = sc[pos] + ec[end] + dist as u32; |
| 58 | + out += (cost + 100 <= base_cost) as u32; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + out |
| 63 | + } |
| 64 | + |
| 65 | + fn cost_map(&self, start: Vec2<usize>) -> Grid<u32> { |
| 66 | + let mut costs = Grid::new(self.board.size, u32::MAX); |
| 67 | + let mut queue = VecDeque::new(); |
| 68 | + queue.push_back((start, 0)); |
| 69 | + |
| 70 | + while let Some((pos, dist)) = queue.pop_front() { |
| 71 | + if costs[pos] != u32::MAX { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + costs[pos] = dist; |
| 76 | + for dir in Direction::ALL { |
| 77 | + let next = dir.wrapping_advance(pos); |
| 78 | + if let Some(tile) = self.board.get(next) { |
| 79 | + if matches!(tile, '.' | 'E') { |
| 80 | + queue.push_back((next, dist + 1)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + costs |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod test { |
| 92 | + use indoc::indoc; |
| 93 | + |
| 94 | + const CASE: &str = indoc! {" |
| 95 | + ############### |
| 96 | + #...#...#.....# |
| 97 | + #.#.#.#.#.###.# |
| 98 | + #S#...#.#.#...# |
| 99 | + #######.#.#.### |
| 100 | + #######.#.#...# |
| 101 | + #######.#.###.# |
| 102 | + ###..E#...#...# |
| 103 | + ###.#######.### |
| 104 | + #...###...#...# |
| 105 | + #.#####.#.###.# |
| 106 | + #.#...#.#.#...# |
| 107 | + #.#.#.#.#.#.### |
| 108 | + #...#...#...### |
| 109 | + ############### |
| 110 | + "}; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn part_a() { |
| 114 | + assert_eq!(super::part_a(CASE), 44.into()); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn part_b() { |
| 119 | + assert_eq!(super::part_b(CASE), ().into()); |
| 120 | + } |
| 121 | +} |
0 commit comments