Skip to content

Commit

Permalink
More 2024 day 20 optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Dec 20, 2024
1 parent cb4f58f commit 57b22ac
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions crates/year2024/src/day20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,22 @@ impl Day20 {

fn cheat_count(&self, x_offset: isize, y_offset: isize) -> u32 {
let cheat_length = (x_offset.unsigned_abs() + y_offset.unsigned_abs()) as u16;
let threshold = 101 + cheat_length;
if cheat_length == 0 {
return 0;
}

let mut cheats = 0;
let start_index = self.cols * 20 + 20;
let end_index = self.distances.len() - start_index;
let offset = y_offset * (self.cols as isize) + x_offset;
for (index, target) in (self.cols * 20..self.distances.len() - (self.cols * 20))
.zip((self.cols * 20).wrapping_add_signed(offset)..)
{
let this_distance = self.distances[index];
let target_distance = self.distances[target];
cheats += u16::from(
target_distance
.wrapping_add(1)
.saturating_sub(this_distance)
.saturating_sub(cheat_length)
>= 101,
);
}
cheats as u32

self.distances[start_index..end_index]
.iter()
.zip(self.distances[start_index.wrapping_add_signed(offset)..].iter())
.map(|(&current, &target)| {
u16::from(target.wrapping_add(1).saturating_sub(current) >= threshold)
})
.sum::<u16>() as u32
}
}

Expand Down

0 comments on commit 57b22ac

Please sign in to comment.