Skip to content

Change u32 to i32 to prevent subtraction overflow #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/year2024/day16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::util::grid::*;
use crate::util::point::*;
use std::collections::VecDeque;

type Input = (u32, usize);
type Input = (i32, usize);

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

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

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

pub fn part1(input: &Input) -> u32 {
pub fn part1(input: &Input) -> i32 {
input.0
}

Expand Down
Loading