Skip to content

Commit 9c3b8ef

Browse files
authored
Change u32 to i32 to prevent subtraction overflow (#5)
1 parent 58bfd5a commit 9c3b8ef

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/year2024/day16.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::util::grid::*;
1212
use crate::util::point::*;
1313
use std::collections::VecDeque;
1414

15-
type Input = (u32, usize);
15+
type Input = (i32, usize);
1616

1717
/// Clockwise order starting with facing right.
1818
const DIRECTIONS: [Point; 4] = [RIGHT, DOWN, LEFT, UP];
@@ -28,8 +28,8 @@ pub fn parse(input: &str) -> Input {
2828
let mut todo_first = VecDeque::new();
2929
let mut todo_second = VecDeque::new();
3030
// State is `(position, direction)`.
31-
let mut seen = grid.same_size_with([u32::MAX; 4]);
32-
let mut lowest = u32::MAX;
31+
let mut seen = grid.same_size_with([i32::MAX; 4]);
32+
let mut lowest = i32::MAX;
3333

3434
todo_first.push_back((start, 0, 0));
3535
seen[start][0] = 0;
@@ -99,16 +99,16 @@ pub fn parse(input: &str) -> Input {
9999
// Trace our cost step by step so it will exactly match possible paths.
100100
if next_cost == seen[next_position][next_direction] {
101101
todo.push_back((next_position, next_direction, next_cost));
102-
// Set cost back to `u32::MAX` to prevent redundant path explorations.
103-
seen[next_position][next_direction] = u32::MAX;
102+
// Set cost back to `i32::MAX` to prevent redundant path explorations.
103+
seen[next_position][next_direction] = i32::MAX;
104104
}
105105
}
106106
}
107107

108108
(lowest, path.bytes.iter().filter(|&&b| b).count())
109109
}
110110

111-
pub fn part1(input: &Input) -> u32 {
111+
pub fn part1(input: &Input) -> i32 {
112112
input.0
113113
}
114114

0 commit comments

Comments
 (0)