Skip to content

Commit 57b22ac

Browse files
committed
More 2024 day 20 optimization
1 parent cb4f58f commit 57b22ac

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

crates/year2024/src/day20.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,22 @@ impl Day20 {
8181

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

88-
let mut cheats = 0;
89+
let start_index = self.cols * 20 + 20;
90+
let end_index = self.distances.len() - start_index;
8991
let offset = y_offset * (self.cols as isize) + x_offset;
90-
for (index, target) in (self.cols * 20..self.distances.len() - (self.cols * 20))
91-
.zip((self.cols * 20).wrapping_add_signed(offset)..)
92-
{
93-
let this_distance = self.distances[index];
94-
let target_distance = self.distances[target];
95-
cheats += u16::from(
96-
target_distance
97-
.wrapping_add(1)
98-
.saturating_sub(this_distance)
99-
.saturating_sub(cheat_length)
100-
>= 101,
101-
);
102-
}
103-
cheats as u32
92+
93+
self.distances[start_index..end_index]
94+
.iter()
95+
.zip(self.distances[start_index.wrapping_add_signed(offset)..].iter())
96+
.map(|(&current, &target)| {
97+
u16::from(target.wrapping_add(1).saturating_sub(current) >= threshold)
98+
})
99+
.sum::<u16>() as u32
104100
}
105101
}
106102

0 commit comments

Comments
 (0)