clippy for 2024 day-4

This commit is contained in:
Dylan Thies
2024-12-04 21:00:16 -05:00
parent fdb74e6439
commit d320036973
2 changed files with 27 additions and 10 deletions

View File

@@ -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: <https://adventofcode.com/2024/day/4>
///
/// # 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<String, Day4Part1Error> {
//read in grid
let grid = input
.lines()
.map(|line| Vec::from(line.as_bytes()))
.collect::<Vec<_>>();
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<String, Day4Part1Error> {
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'

View File

@@ -11,6 +11,14 @@ pub enum Day4Part2Error {
ParseError,
}
/// Day-4 Part 2 for 2024 advent of code
/// Problem can be found here: <https://adventofcode.com/2024/day/4#part2>
///
/// # 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<String, Day4Part2Error> {
//read in grid
let grid = input
@@ -39,7 +47,8 @@ pub fn part2(input: &str) -> Result<String, Day4Part2Error> {
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<String, Day4Part2Error> {
== 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
}