diff --git a/src/day04.rs b/src/day04.rs index 8c54df9..d7099bc 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -53,22 +53,19 @@ impl Day for Day04 { fn part2(&self) -> String { let z = self.clone(); - let mut boards = z.boards.iter().cloned().map(Option::Some).collect::>>(); + let mut boards = z.boards; for number in z.numbers { - let some_count = boards.iter().map(|x| if x.is_some() { 1 } else { 0 }).sum::(); - #[allow(clippy::manual_flatten)] - for boardd in &mut boards { - if let Some(board) = boardd { - let res = board.mark(number); - if res { - if some_count == 1 { - let s = board.unmarked_sum(); - return format!("{}", s * number); - } - *boardd = None; + let len = boards.len(); + for board in &mut boards { + let res = board.mark(number); + if res { + if len == 1 { + let s = board.unmarked_sum(); + return format!("{}", s * number); } } } + boards = boards.iter().filter(|x| !x.complete()).cloned().collect(); } panic!("no board can win?") @@ -90,6 +87,10 @@ impl Board { self.check_rows() || self.check_columns() } + fn complete(&self) -> bool { + self.check_rows() || self.check_columns() + } + fn check_rows(&self) -> bool { for columns in &self.0 { if columns.iter().all(Option::is_none) {