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
25
src/day04.rs
25
src/day04.rs
|
@ -53,22 +53,19 @@ impl Day for Day04 {
|
||||||
|
|
||||||
fn part2(&self) -> String {
|
fn part2(&self) -> String {
|
||||||
let z = self.clone();
|
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 {
|
for number in z.numbers {
|
||||||
let some_count = boards.iter().map(|x| if x.is_some() { 1 } else { 0 }).sum::<u32>();
|
let len = boards.len();
|
||||||
#[allow(clippy::manual_flatten)]
|
for board in &mut boards {
|
||||||
for boardd in &mut boards {
|
let res = board.mark(number);
|
||||||
if let Some(board) = boardd {
|
if res {
|
||||||
let res = board.mark(number);
|
if len == 1 {
|
||||||
if res {
|
let s = board.unmarked_sum();
|
||||||
if some_count == 1 {
|
return format!("{}", s * number);
|
||||||
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?")
|
panic!("no board can win?")
|
||||||
|
@ -90,6 +87,10 @@ impl Board {
|
||||||
self.check_rows() || self.check_columns()
|
self.check_rows() || self.check_columns()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn complete(&self) -> bool {
|
||||||
|
self.check_rows() || self.check_columns()
|
||||||
|
}
|
||||||
|
|
||||||
fn check_rows(&self) -> bool {
|
fn check_rows(&self) -> bool {
|
||||||
for columns in &self.0 {
|
for columns in &self.0 {
|
||||||
if columns.iter().all(Option::is_none) {
|
if columns.iter().all(Option::is_none) {
|
||||||
|
|
Loading…
Reference in a new issue