From 8d034b83f96b99c77e24e9e3eb53c9e6248c6c9e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 3 Dec 2023 13:48:50 -0500 Subject: [PATCH] Use address halos to calculate part halos --- 2023/src/day3.rs | 109 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/2023/src/day3.rs b/2023/src/day3.rs index 4fa2dca..88fa2c3 100644 --- a/2023/src/day3.rs +++ b/2023/src/day3.rs @@ -22,6 +22,32 @@ struct Addr { col: usize, } +impl Addr { + fn halo(&self) -> impl Iterator { + let row = self.row as i32; + let col = self.col as i32; + vec![ + (row - 1, col - 1), + (row, col - 1), + (row + 1, col - 1), + // + (row - 1, col), + (row, col), + (row + 1, col), + // + (row - 1, col + 1), + (row, col + 1), + (row + 1, col + 1), + ] + .into_iter() + .filter(|(row, col)| *row >= 0 && *col >= 0) + .map(|(row, col)| Addr { + row: row as usize, + col: col as usize, + }) + } +} + #[derive(Clone, Debug, PartialEq)] struct Part { value: u32, @@ -31,39 +57,38 @@ struct Part { } impl Part { - fn span(&self) -> Vec { - (self.start..self.end + 1) - .map(|addr| Addr { - row: self.row, - col: addr, - }) - .collect() + fn span<'a>(&'a self) -> impl Iterator + 'a { + (self.start..self.end + 1).map(|addr| Addr { + row: self.row, + col: addr, + }) } - 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 + fn halo<'a>(&'a self, max_row: usize, max_col: usize) -> impl IntoIterator { + println!("{:?}", self); + let addresses = self + .span() + .map(|addr| addr.halo()) + .flatten() + .filter(|Addr { row, col }| { + *row as i32 >= 0 + && *row as i32 <= max_row as i32 + && *col as i32 >= 0 + && *col as i32 <= max_col as i32 }) - .map(|(row, col)| Addr { - row: row as usize, - col: col as usize, - }) - .collect() + .fold(HashSet::new(), |mut acc, val| { + let _ = acc.insert(val); + acc + }); + + println!("{:?}", addresses); + println!("{:?}", self.span().collect::>()); + println!("{:?}", addresses.difference(&self.span().collect())); + + addresses + .difference(&self.span().collect()) + .map(|addr| addr.clone()) + .collect::>() } } @@ -112,8 +137,10 @@ fn find_gears(schematic: String) -> Vec { end: addr.col, } .halo(max_row - 1, max_col - 1) + .into_iter() + .collect::>() }) - .collect::>>(); + .collect::>>(); let mut gears = vec![]; for splat in splats { @@ -130,7 +157,7 @@ fn find_gears(schematic: String) -> Vec { gears } -fn part_within_halo(halo1: Vec, part: Part) -> bool { +fn part_within_halo(halo1: impl IntoIterator, part: Part) -> bool { let halo1: HashSet = halo1.into_iter().collect::>(); let halo2: HashSet = part.span().into_iter().collect::>(); @@ -352,7 +379,9 @@ mod test { end: 2, }; assert_eq!( - part.halo(schematic.len() - 1, schematic[0].len() - 1), + part.halo(schematic.len() - 1, schematic[0].len() - 1) + .into_iter() + .collect::>(), vec![ Addr { row: 1, col: 0 }, Addr { row: 1, col: 1 }, @@ -360,6 +389,8 @@ mod test { Addr { row: 0, col: 3 }, Addr { row: 1, col: 3 } ] + .into_iter() + .collect::>() ); assert_eq!( @@ -369,7 +400,9 @@ mod test { start: 1, end: 1 } - .halo(schematic.len() - 1, schematic[0].len() - 1), + .halo(schematic.len() - 1, schematic[0].len() - 1) + .into_iter() + .collect::>(), vec![ Addr { row: 0, col: 1 }, Addr { row: 2, col: 1 }, @@ -380,6 +413,8 @@ mod test { Addr { row: 1, col: 2 }, Addr { row: 2, col: 2 } ] + .into_iter() + .collect::>() ); assert_eq!( Part { @@ -388,7 +423,9 @@ mod test { start: 2, end: 4, } - .halo(schematic.len() - 1, schematic[0].len() - 1), + .halo(schematic.len() - 1, schematic[0].len() - 1) + .into_iter() + .collect::>(), vec![ Addr { row: 5, col: 2 }, Addr { row: 7, col: 2 }, @@ -403,6 +440,8 @@ mod test { Addr { row: 6, col: 5 }, Addr { row: 7, col: 5 }, ] + .into_iter() + .collect::>() ); }