Day 04 less mutable iterators
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0c28d31523
commit
8a3ec2f2f1
17
src/day04.rs
17
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::<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 len = boards.len();
|
||||
for board in &mut boards {
|
||||
let res = board.mark(number);
|
||||
if res {
|
||||
if some_count == 1 {
|
||||
if len == 1 {
|
||||
let s = board.unmarked_sum();
|
||||
return format!("{}", s * number);
|
||||
}
|
||||
*boardd = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue