From 234f53b8b523cabf42bdd0862dda8bf7acc21cae Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Dec 2023 13:08:38 -0500 Subject: [PATCH] Make Halo a part of the Part structure --- 2023/src/day3.rs | 107 ++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/2023/src/day3.rs b/2023/src/day3.rs index e2dc2d8..4fa2dca 100644 --- a/2023/src/day3.rs +++ b/2023/src/day3.rs @@ -39,6 +39,32 @@ impl Part { }) .collect() } + + fn halo(&self, max_row: usize, max_col: usize) -> Vec { + let mut halo: Vec<(i32, i32)> = vec![]; + + for r in self.start..self.end + 1 { + halo.push((self.row as i32 - 1, r as i32)); + halo.push((self.row as i32 + 1, r as i32)); + } + + halo.push((self.row as i32 - 1, self.start as i32 - 1)); + halo.push((self.row as i32, self.start as i32 - 1)); + halo.push((self.row as i32 + 1, self.start as i32 - 1)); + halo.push((self.row as i32 - 1, self.end as i32 + 1)); + halo.push((self.row as i32, self.end as i32 + 1)); + halo.push((self.row as i32 + 1, self.end as i32 + 1)); + + halo.into_iter() + .filter(|(row, col)| { + *row >= 0 && *row <= max_row as i32 && *col >= 0 && *col <= max_col as i32 + }) + .map(|(row, col)| Addr { + row: row as usize, + col: col as usize, + }) + .collect() + } } #[derive(Clone, Debug, PartialEq)] @@ -79,16 +105,13 @@ fn find_gears(schematic: String) -> Vec { let splats = find_splats(sc) .into_iter() .map(|addr| { - halo( - Part { - value: 0, - row: addr.row, - start: addr.col, - end: addr.col, - }, - max_row - 1, - max_col - 1, - ) + Part { + value: 0, + row: addr.row, + start: addr.col, + end: addr.col, + } + .halo(max_row - 1, max_col - 1) }) .collect::>>(); @@ -115,7 +138,7 @@ fn part_within_halo(halo1: Vec, part: Part) -> bool { } fn is_part(schematic: &Vec, part: Part) -> bool { - halo(part.clone(), schematic.len() - 1, schematic[0].len() - 1) + part.halo(schematic.len() - 1, schematic[0].len() - 1) .into_iter() .any(|addr| { let cell = schematic[addr.row].chars().nth(addr.col).unwrap(); @@ -123,32 +146,6 @@ fn is_part(schematic: &Vec, part: Part) -> bool { }) } -fn halo(addr: Part, max_row: usize, max_col: usize) -> Vec { - let mut halo: Vec<(i32, i32)> = vec![]; - - for r in addr.start..addr.end + 1 { - halo.push((addr.row as i32 - 1, r as i32)); - halo.push((addr.row as i32 + 1, r as i32)); - } - - halo.push((addr.row as i32 - 1, addr.start as i32 - 1)); - halo.push((addr.row as i32, addr.start as i32 - 1)); - halo.push((addr.row as i32 + 1, addr.start as i32 - 1)); - halo.push((addr.row as i32 - 1, addr.end as i32 + 1)); - halo.push((addr.row as i32, addr.end as i32 + 1)); - halo.push((addr.row as i32 + 1, addr.end as i32 + 1)); - - halo.into_iter() - .filter(|(row, col)| { - *row >= 0 && *row <= max_row as i32 && *col >= 0 && *col <= max_col as i32 - }) - .map(|(row, col)| Addr { - row: row as usize, - col: col as usize, - }) - .collect() -} - fn find_numbers(schematic: String) -> Vec { let mut result = vec![]; for (idx, line) in schematic.lines().enumerate() { @@ -355,7 +352,7 @@ mod test { end: 2, }; assert_eq!( - halo(part, schematic.len() - 1, schematic[0].len() - 1), + part.halo(schematic.len() - 1, schematic[0].len() - 1), vec![ Addr { row: 1, col: 0 }, Addr { row: 1, col: 1 }, @@ -366,16 +363,13 @@ mod test { ); assert_eq!( - halo( - Part { - value: 0, - row: 1, - start: 1, - end: 1 - }, - schematic.len() - 1, - schematic[0].len() - 1 - ), + Part { + value: 0, + row: 1, + start: 1, + end: 1 + } + .halo(schematic.len() - 1, schematic[0].len() - 1), vec![ Addr { row: 0, col: 1 }, Addr { row: 2, col: 1 }, @@ -388,16 +382,13 @@ mod test { ] ); assert_eq!( - halo( - Part { - value: 0, - row: 6, - start: 2, - end: 4, - }, - schematic.len() - 1, - schematic[0].len() - 1 - ), + Part { + value: 0, + row: 6, + start: 2, + end: 4, + } + .halo(schematic.len() - 1, schematic[0].len() - 1), vec![ Addr { row: 5, col: 2 }, Addr { row: 7, col: 2 },