Skip to main content

aoc/year2024/
day20.rs

1//! # Race Condition
2//!
3//! Examining the input shows that there is only a single path from start to finish with
4//! no branches. This simplifies checking for shortcuts as any empty space will be on the shortest
5//! path from start to end. The cheating rules allow us to "warp" up to `n` squares away to any
6//! empty space as measured by [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry).
7//!
8//! For part one this is a distance of 2. When checking surrounding squares we make 3 optimizations:
9//!
10//! * Don't check any squares only 1 away as we can always just move to these normally.
11//! * Checking from point `p => q` is always the negative of `q => p`, e.g. if `p = 30, q = 50` then
12//!   `p => q = 20` and `q => p = -20`. This means we only ever need to check any pair once.
13//! * Additionally, we only need to check down and right. Previous rows and columns will already
14//!   have checked points above and to the left when we reach an empty square.
15//!
16//! ```none
17//!   #         .
18//!  ###       ...
19//! ##P## =>  ....#
20//!  ###       ...
21//!   #         #
22//! ```
23//!
24//! For part two the distance is increased to 20 and the shape now resembles a wonky diamond.
25//! This shape ensures complete coverage without duplicating checks.
26//!
27//! ```none
28//!   #        .
29//!  ###      ...
30//! ##P## => ..P##
31//!  ###      ###
32//!   #        #
33//! ```
34use crate::util::grid::*;
35use crate::util::point::*;
36use crate::util::thread::*;
37
38/// Create a grid the same size as input with the time taken from start to any location.
39pub fn parse(input: &str) -> Grid<i32> {
40    let grid = Grid::parse(input);
41    let start = grid.find(b'S').unwrap();
42    let end = grid.find(b'E').unwrap();
43
44    // Add padding to avoid boundary checks.
45    let mut time = Grid::new(grid.width + 20, grid.height + 20, i32::MAX);
46    let mut elapsed = 0;
47
48    // Find starting direction, assuming start position is surrounded by 3 walls.
49    let mut position = start;
50    let mut direction = ORTHOGONAL.into_iter().find(|&o| grid[position + o] != b'#').unwrap();
51
52    while position != end {
53        time[position] = elapsed;
54        elapsed += 1;
55
56        // There are no branches so we only ever need to go straight ahead or turn left or right.
57        direction = [direction, direction.clockwise(), direction.counter_clockwise()]
58            .into_iter()
59            .find(|&d| grid[position + d] != b'#')
60            .unwrap();
61        position += direction;
62    }
63
64    time[end] = elapsed;
65    time
66}
67
68pub fn part1(time: &Grid<i32>) -> u32 {
69    cheats(time, Point::new(2, 0)) + cheats(time, Point::new(0, 2))
70}
71
72/// Searches for all cheats up to distance 20, parallelizing the work over multiple threads.
73pub fn part2(time: &Grid<i32>) -> u32 {
74    let right = (2..21).map(|x| Point::new(x, 0));
75    let below = (1..21).flat_map(|y| ((y - 20)..(21 - y)).map(move |x| Point::new(x, y)));
76    let deltas: Vec<_> = right.chain(below).collect();
77
78    // Use as many cores as possible to parallelize the remaining search.
79    spawn_parallel_iterator(&deltas, |iter| iter.map(|&delta| cheats(time, delta)).sum::<u32>())
80        .into_iter()
81        .sum()
82}
83
84/// Compare two slices against each other so that the compiler can auto-vectorize the loops.
85/// Boundary checks are not needed due to the padding on the right and bottom of the grid.
86fn cheats(time: &Grid<i32>, delta: Point) -> u32 {
87    let maximum = time.width * time.height;
88    let threshold = 100 + delta.manhattan(ORIGIN);
89    let offset = (time.width * delta.y + delta.x) as usize;
90
91    time.bytes
92        .iter()
93        .zip(&time.bytes[offset..])
94        .map(|(&from, &to)| {
95            let saved = (from - to).abs();
96            u32::from(threshold <= saved && saved <= maximum)
97        })
98        .sum()
99}