clippy for 2024 day-4
This commit is contained in:
@@ -11,13 +11,25 @@ pub enum Day4Part1Error {
|
|||||||
ParseError,
|
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> {
|
pub fn part1(input: &str) -> Result<String, Day4Part1Error> {
|
||||||
//read in grid
|
//read in grid
|
||||||
let grid = input
|
let grid = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| Vec::from(line.as_bytes()))
|
.map(|line| Vec::from(line.as_bytes()))
|
||||||
.collect::<Vec<_>>();
|
.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
|
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
|
//window over each letter (skip over not x's
|
||||||
let total: usize = grid
|
let total: usize = grid
|
||||||
@@ -54,6 +66,9 @@ pub fn part1(input: &str) -> Result<String, Day4Part1Error> {
|
|||||||
let m = point + *dir;
|
let m = point + *dir;
|
||||||
let a = point + 2 * *dir;
|
let a = point + 2 * *dir;
|
||||||
let s = point + 3 * *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[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[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'
|
&& grid[s.x as u32 as usize][s.y as u32 as usize] == b'S'
|
||||||
|
|||||||
@@ -11,6 +11,14 @@ pub enum Day4Part2Error {
|
|||||||
ParseError,
|
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> {
|
pub fn part2(input: &str) -> Result<String, Day4Part2Error> {
|
||||||
//read in grid
|
//read in grid
|
||||||
let grid = input
|
let grid = input
|
||||||
@@ -39,7 +47,8 @@ pub fn part2(input: &str) -> Result<String, Day4Part2Error> {
|
|||||||
let up_back = point + IVec2::NEG_ONE;
|
let up_back = point + IVec2::NEG_ONE;
|
||||||
let down_forward = point + IVec2::ONE;
|
let down_forward = point + IVec2::ONE;
|
||||||
let down_back = point + IVec2::new(1, -1);
|
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]
|
&& grid[down_forward.x as u32 as usize]
|
||||||
[down_forward.y as u32 as usize]
|
[down_forward.y as u32 as usize]
|
||||||
== b'S')
|
== b'S')
|
||||||
@@ -56,14 +65,7 @@ pub fn part2(input: &str) -> Result<String, Day4Part2Error> {
|
|||||||
== b'S'
|
== b'S'
|
||||||
&& grid[up_forward.x as u32 as usize]
|
&& grid[up_forward.x as u32 as usize]
|
||||||
[up_forward.y as u32 as usize]
|
[up_forward.y as u32 as usize]
|
||||||
== b'M'))
|
== b'M'))).into()
|
||||||
{
|
|
||||||
//println!(" found at {}-{}", point.x, point.y);
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
// todo!("at pos {row_num} - {col_num}")
|
|
||||||
} else {
|
} else {
|
||||||
0_usize
|
0_usize
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user