From 0074ccaf0f099cc2e4268d86a8b18edb53adb486 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sat, 4 Dec 2021 10:41:46 +0100 Subject: [PATCH] Make the way boards are parsed more functional --- src/day04.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/day04.rs b/src/day04.rs index d7099bc..28348ca 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -18,16 +18,13 @@ impl Day for Day04 { let nums = first.split(',').map(|x| x.parse::().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::().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::().expect("foo") + ).map(Option::Some).collect()).collect() + ).map(Board).collect(); Day04 { numbers: nums, @@ -58,11 +55,9 @@ impl Day for Day04 { 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); - } + 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