From 05222c2e6a5fce3cce8a6f03ae970b414d96156a Mon Sep 17 00:00:00 2001 From: 3rfaan <28968837+3rfaan@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:35:45 +0100 Subject: [PATCH] Fixed overflow error --- src/bin/04.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bin/04.rs b/src/bin/04.rs index fe98ff6..7e89fe2 100644 --- a/src/bin/04.rs +++ b/src/bin/04.rs @@ -32,11 +32,11 @@ struct Grid { } impl Grid { - fn get(&self, r: usize, c: usize) -> u8 { + fn get(&self, row: isize, col: isize) -> u8 { *self .bytes - .get(r) - .and_then(|row| row.get(c)) + .get(row as usize) + .and_then(|row| row.get(col as usize)) .unwrap_or(&b'.') } @@ -54,7 +54,9 @@ impl Grid { .iter() .filter(|(off_x, off_y)| { (0..4).all(|i| { - let (new_row, new_col) = (row + (off_x * i) as usize, col + (off_y * i) as usize); + let new_row = row as isize + (off_x * i); + let new_col = col as isize + (off_y * i); + self.get(new_row, new_col) == b"XMAS"[i as usize] }) }) @@ -62,6 +64,8 @@ impl Grid { } fn crossmas_count(&self, row: usize, col: usize) -> bool { + let (row, col) = (row as isize, col as isize); + let diag1 = [self.get(row - 1, col - 1), self.get(row + 1, col + 1)]; let diag2 = [self.get(row - 1, col + 1), self.get(row + 1, col - 1)]; @@ -71,14 +75,10 @@ impl Grid { impl From<&str> for Grid { fn from(input: &str) -> Self { - let grid: Vec> = input.lines().map(|row| row.bytes().collect()).collect(); - let (rows, cols) = (grid.len(), grid.first().map_or(0, |row| row.len())); - - Self { - bytes: grid, - rows, - cols, - } + let bytes: Vec> = input.lines().map(|row| row.bytes().collect()).collect(); + let (rows, cols) = (bytes.len(), bytes.first().map_or(0, |row| row.len())); + + Self { bytes, rows, cols } } }