|
| 1 | +//! # Race Condition |
| 2 | +use crate::util::grid::*; |
| 3 | +use crate::util::point::*; |
| 4 | +use crate::util::thread::*; |
| 5 | +use std::collections::VecDeque; |
| 6 | +use std::sync::atomic::{AtomicU32, Ordering}; |
| 7 | + |
| 8 | +pub struct Input { |
| 9 | + grid: Grid<u8>, |
| 10 | + forward: Grid<i32>, |
| 11 | + reverse: Grid<i32>, |
| 12 | + full: i32, |
| 13 | +} |
| 14 | + |
| 15 | +pub fn parse(input: &str) -> Input { |
| 16 | + let grid = Grid::parse(input); |
| 17 | + let start = grid.find(b'S').unwrap(); |
| 18 | + let end = grid.find(b'E').unwrap(); |
| 19 | + |
| 20 | + let forward = bfs(&grid, start); |
| 21 | + let reverse = bfs(&grid, end); |
| 22 | + let full = forward[end]; |
| 23 | + |
| 24 | + Input { grid, forward, reverse, full } |
| 25 | +} |
| 26 | + |
| 27 | +pub fn part1(input: &Input) -> u32 { |
| 28 | + let Input { grid, forward, reverse, full } = input; |
| 29 | + let mut total = 0; |
| 30 | + |
| 31 | + for y in 1..grid.height - 1 { |
| 32 | + for x in 1..grid.width - 1 { |
| 33 | + let first = Point::new(x, y); |
| 34 | + |
| 35 | + if grid[first] != b'#' { |
| 36 | + for second in ORTHOGONAL.map(|o| first + o * 2) { |
| 37 | + if grid.contains(second) && grid[second] != b'#' { |
| 38 | + let cost = forward[first] + reverse[second] + 2; |
| 39 | + |
| 40 | + if *full - cost >= 100 { |
| 41 | + total += 1; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + total |
| 50 | +} |
| 51 | + |
| 52 | +pub fn part2(input: &Input) -> u32 { |
| 53 | + let Input { grid, .. } = input; |
| 54 | + let mut items = Vec::with_capacity(10_000); |
| 55 | + |
| 56 | + for y in 1..grid.height - 1 { |
| 57 | + for x in 1..grid.width - 1 { |
| 58 | + let point = Point::new(x, y); |
| 59 | + if grid[point] != b'#' { |
| 60 | + items.push(point); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + let total = AtomicU32::new(0); |
| 66 | + spawn_batches(items, |batch| worker(input, &total, batch)); |
| 67 | + total.into_inner() |
| 68 | +} |
| 69 | + |
| 70 | +fn worker(input: &Input, total: &AtomicU32, batch: Vec<Point>) { |
| 71 | + let Input { grid, forward, reverse, full } = input; |
| 72 | + let mut cheats = 0; |
| 73 | + |
| 74 | + for first in batch { |
| 75 | + for y in -20..21_i32 { |
| 76 | + for x in (y.abs() - 20)..(21 - y.abs()) { |
| 77 | + let second = first + Point::new(x, y); |
| 78 | + |
| 79 | + if grid.contains(second) && grid[second] != b'#' { |
| 80 | + let manhattan = x.abs() + y.abs(); |
| 81 | + let cost = forward[first] + reverse[second] + manhattan; |
| 82 | + |
| 83 | + if *full - cost >= 100 { |
| 84 | + cheats += 1; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + total.fetch_add(cheats, Ordering::Relaxed); |
| 92 | +} |
| 93 | + |
| 94 | +fn bfs(grid: &Grid<u8>, start: Point) -> Grid<i32> { |
| 95 | + let mut todo = VecDeque::new(); |
| 96 | + let mut seen = grid.same_size_with(i32::MAX); |
| 97 | + |
| 98 | + todo.push_back((start, 0)); |
| 99 | + seen[start] = 0; |
| 100 | + |
| 101 | + while let Some((position, cost)) = todo.pop_front() { |
| 102 | + let cost = cost + 1; |
| 103 | + |
| 104 | + for next in ORTHOGONAL.map(|o| position + o) { |
| 105 | + if grid[next] != b'#' && cost < seen[next] { |
| 106 | + todo.push_back((next, cost)); |
| 107 | + seen[next] = cost; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + seen |
| 113 | +} |
0 commit comments