Skip to content

Commit

Permalink
day20
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 20, 2024
1 parent de4fc88 commit 81d312a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/day20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ macro_rules! generate_jumps {
const CHEATING_JUMPS1: [(isize, isize, usize); 12] = generate_jumps!(12, 2);
const CHEATING_JUMPS2: [(isize, isize, usize); 840] = generate_jumps!(840, 20);

unsafe fn calc_distances(grid: &[u8], pos: usize, dist: usize, distances: &mut [usize; GRID_SIZE]) {
unsafe fn calc_distances(
grid: &[u8],
end: usize,
pos: usize,
dist: usize,
distances: &mut [usize; GRID_SIZE],
) {
if *distances.get_unchecked(end) != usize::MAX {
return;
}
if *distances.get_unchecked(pos) != usize::MAX {
return;
}
Expand All @@ -48,7 +57,7 @@ unsafe fn calc_distances(grid: &[u8], pos: usize, dist: usize, distances: &mut [
let next_pos = $next_pos;

if *grid.get_unchecked(next_pos) != b'#' {
calc_distances(grid, next_pos, next_dist, distances);
calc_distances(grid, end, next_pos, next_dist, distances);
}
};
}
Expand All @@ -65,7 +74,7 @@ unsafe fn solve(input: &str, cheating_jumps: &[(isize, isize, usize)]) -> usize
let end = grid.iter().position(|&c| c == b'E').unwrap_unchecked();

let mut distances = [usize::MAX; GRID_SIZE];
calc_distances(&grid, end, 0, &mut distances);
calc_distances(&grid, start, end, 0, &mut distances);

let initial_total_time = *distances.get_unchecked(start);

Expand Down

0 comments on commit 81d312a

Please sign in to comment.