From 000000701f9333a4f0cbde73622d6f62dc84e2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sat, 2 Dec 2023 14:23:33 +0100 Subject: [PATCH] solution: day 2 clean --- src/bin/02.rs | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/bin/02.rs b/src/bin/02.rs index e1706c1..ac4ce7c 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -26,35 +26,19 @@ impl FromStr for Game { pub fn part_one(input: &str) -> Option { let games: Vec = to_vec(input, '\n'); + let rules = [(12, "red"), (13, "green"), (14, "blue")]; - let mut id_sum = 0; - - 'games: for g in games { - for (n, c) in g.balls { - match c.as_str() { - "blue" => { - if n > 14 { - continue 'games; - } - } - "red" => { - if n > 12 { - continue 'games; - } - } - "green" => { - if n > 13 { - continue 'games; - } - } - _ => continue 'games, - } - } - - id_sum += g.id; - } - - Some(id_sum) + Some( + games + .iter() + .filter(|g| { + !g.balls + .iter() + .any(|(n, c)| rules.iter().any(|(rn, rc)| c == rc && n > rn)) + }) + .map(|g| g.id) + .sum(), + ) } pub fn part_two(input: &str) -> Option {