Skip to content

Commit 00c658c

Browse files
committed
Prevent integer overflow in 2017 day 23
2017 day 23 crashed when ran with the debug profile / with overflow checks enabled due to mul_count overflowing when running part 2. This didn't affect the solution as mul_count isn't used in part 2.
1 parent c82b31d commit 00c658c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

crates/year2017/src/day23.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Day23 {
7878
}
7979

8080
#[must_use]
81-
pub fn part1(&self) -> u32 {
81+
pub fn part1(&self) -> u64 {
8282
self.run(&mut [0; 8])
8383
}
8484

@@ -89,7 +89,7 @@ impl Day23 {
8989
reg[Register::H as usize]
9090
}
9191

92-
fn run(&self, reg: &mut [i64; 8]) -> u32 {
92+
fn run(&self, reg: &mut [i64; 8]) -> u64 {
9393
let mut pc = 0;
9494
let mut mul_count = 0;
9595
while pc < self.instructions.len() {
@@ -140,7 +140,7 @@ impl Day23 {
140140
}
141141
reg[g as usize] = 0;
142142
pc += 15;
143-
mul_count += (reg[b as usize] - 2).pow(2) as u32;
143+
mul_count += (reg[b as usize] - 2).pow(2) as u64;
144144
continue;
145145
}
146146
_ => {},
@@ -178,4 +178,4 @@ impl Day23 {
178178
}
179179
}
180180

181-
examples!(Day23 -> (u32, i64) []);
181+
examples!(Day23 -> (u64, i64) []);

0 commit comments

Comments
 (0)