Day 04 less mutable iterators
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julius 2021-12-04 10:12:56 +01:00
parent 0c28d31523
commit 8a3ec2f2f1
Signed by: j00lz
GPG key ID: AF241B0AA237BBA2

View file

@ -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::<Vec<Option<Board>>>();
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::<u32>();
#[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) {