Make the way boards are parsed more functional
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
8a3ec2f2f1
commit
0074ccaf0f
21
src/day04.rs
21
src/day04.rs
|
@ -18,16 +18,13 @@ impl Day for Day04 {
|
|||
let nums = first.split(',').map(|x| x.parse::<u32>().expect("wat?")).collect();
|
||||
|
||||
let split = l.split(|x| x.is_empty());
|
||||
let mut boards = vec![];
|
||||
for vecs in split {
|
||||
let mut rows = vec![];
|
||||
for vec in vecs {
|
||||
let v = vec.split_ascii_whitespace().map(|x| x.parse::<u32>().expect("foo")).map(Option::Some).collect();
|
||||
rows.push(v);
|
||||
}
|
||||
boards.push(Board(rows));
|
||||
}
|
||||
|
||||
let boards = split.map(|vecs|
|
||||
vecs.iter().map(
|
||||
|vec| vec.split_ascii_whitespace().map(
|
||||
|x| x.parse::<u32>().expect("foo")
|
||||
).map(Option::Some).collect()).collect()
|
||||
).map(Board).collect();
|
||||
|
||||
Day04 {
|
||||
numbers: nums,
|
||||
|
@ -58,13 +55,11 @@ impl Day for Day04 {
|
|||
let len = boards.len();
|
||||
for board in &mut boards {
|
||||
let res = board.mark(number);
|
||||
if res {
|
||||
if len == 1 {
|
||||
if res && len == 1 {
|
||||
let s = board.unmarked_sum();
|
||||
return format!("{}", s * number);
|
||||
}
|
||||
}
|
||||
}
|
||||
boards = boards.iter().filter(|x| !x.complete()).cloned().collect();
|
||||
}
|
||||
|
||||
|
@ -132,11 +127,11 @@ impl Board {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use crate::Day04;
|
||||
use crate::day04::Board;
|
||||
use crate::day::Day;
|
||||
|
||||
|
||||
const INPUT: &str = r"7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||
|
||||
22 13 17 11 0
|
||||
|
|
Loading…
Reference in a new issue