Skip to content

Commit 801ec0c

Browse files
committed
comments
1 parent c724e02 commit 801ec0c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

2024/day20/day20.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,21 @@ impl Puzzle {
8383
fn solve(&self, max_cheats: i32, min_gain: i32) -> u32 {
8484
let mut nb = 0;
8585

86+
// to count the cheats, we wiil iterate over all track positions that are
87+
// at a Manhattan distance less than the asked one (2 or 20 depending on the part)
88+
8689
for cheat_start in &self.track {
8790
for cheat_end in &self.track {
88-
let dist = cheat_start.manhattan_distance(cheat_end);
91+
let cheat_dist = cheat_start.manhattan_distance(cheat_end);
92+
93+
if cheat_dist <= max_cheats {
94+
// so the distance (or the time in picoseconds) is the sum of the distance
95+
// from the start to the cheat start, the cheat length and the distance from
96+
// the cheat end to the end of the track
97+
98+
let time = self.from_start[cheat_start] + cheat_dist + self.to_end[cheat_end];
8999

90-
if dist <= max_cheats {
91-
let time = self.from_start[cheat_start] + self.to_end[cheat_end] + dist;
100+
// if the gain is sufficient, we count it
92101
if time + min_gain <= self.boring {
93102
nb += 1;
94103
}

0 commit comments

Comments
 (0)