diff --git a/2024/day-4/src/part1.rs b/2024/day-4/src/part1.rs index 2123e6f..51d5f44 100644 --- a/2024/day-4/src/part1.rs +++ b/2024/day-4/src/part1.rs @@ -11,13 +11,25 @@ pub enum Day4Part1Error { ParseError, } +#[allow(clippy::cast_sign_loss)] +/// Day-4 Part 1 for 2024 advent of code +/// Problem can be found here: +/// +/// # Errors +/// - `ParseError` there was an issue with the parser +/// +/// # Panics +/// - If there is a catastropic error as it only panics in event that a lenght is negative pub fn part1(input: &str) -> Result { //read in grid let grid = input .lines() .map(|line| Vec::from(line.as_bytes())) .collect::>(); - let num_of_rows = grid.len().try_into().unwrap(); + let num_of_rows = grid + .len() + .try_into() + .expect("length cannot be negative ever"); let num_of_cols = grid[0].len().try_into().unwrap(); //because we know it will be rectangular //window over each letter (skip over not x's let total: usize = grid @@ -54,6 +66,9 @@ pub fn part1(input: &str) -> Result { let m = point + *dir; let a = point + 2 * *dir; let s = point + 3 * *dir; + grid.get(m.x as u32 as usize) + .map(|g| g.get(m.y as u32 as usize)) + .unwrap(); grid[m.x as u32 as usize][m.y as u32 as usize] == b'M' && grid[a.x as u32 as usize][a.y as u32 as usize] == b'A' && grid[s.x as u32 as usize][s.y as u32 as usize] == b'S' diff --git a/2024/day-4/src/part2.rs b/2024/day-4/src/part2.rs index ba4c7ef..6b72254 100644 --- a/2024/day-4/src/part2.rs +++ b/2024/day-4/src/part2.rs @@ -11,6 +11,14 @@ pub enum Day4Part2Error { ParseError, } +/// Day-4 Part 2 for 2024 advent of code +/// Problem can be found here: +/// +/// # Errors +/// - `ParseError` there was an issue with the parser +/// +/// # Panics +/// - If there is a catastropic error as it only panics in event that a lenght is negative pub fn part2(input: &str) -> Result { //read in grid let grid = input @@ -39,7 +47,8 @@ pub fn part2(input: &str) -> Result { let up_back = point + IVec2::NEG_ONE; let down_forward = point + IVec2::ONE; let down_back = point + IVec2::new(1, -1); - if ((grid[up_back.x as u32 as usize][up_back.y as u32 as usize] == b'M' + #[allow(clippy::cast_sign_loss)] + (((grid[up_back.x as u32 as usize][up_back.y as u32 as usize] == b'M' && grid[down_forward.x as u32 as usize] [down_forward.y as u32 as usize] == b'S') @@ -56,14 +65,7 @@ pub fn part2(input: &str) -> Result { == b'S' && grid[up_forward.x as u32 as usize] [up_forward.y as u32 as usize] - == b'M')) - { - //println!(" found at {}-{}", point.x, point.y); - 1 - } else { - 0 - } - // todo!("at pos {row_num} - {col_num}") + == b'M'))).into() } else { 0_usize }