From 8a3ec2f2f1b5d8402d296014848d9626e44904ee Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sat, 4 Dec 2021 10:12:56 +0100 Subject: [PATCH] Day 04 less mutable iterators --- src/day04.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) 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) {