Skip to content

Commit af95ace

Browse files
committed
Fix possible overflow in 2024 day 16
1 parent 5cf1631 commit af95ace

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

crates/year2024/src/day16.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ impl Day16 {
149149
}
150150
on_best[index] |= 1 << dir;
151151

152+
if score < 1000 {
153+
// Must not have turned, score is not high enough.
154+
// This branch prevents `score - 1000` overflowing below.
155+
self.reverse(
156+
index.wrapping_add_signed(-self.offsets[dir]),
157+
dir,
158+
score - 1,
159+
on_best,
160+
);
161+
return;
162+
}
163+
152164
let mut count = 0;
153165
let mut next_index = 0;
154166
let mut next_dir = 0;

0 commit comments

Comments
 (0)