From c655326e5b11784a48ba6a9ef0ebee9a0cfb1905 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 5 Dec 2023 14:23:04 -0500 Subject: [PATCH] Complete part on of 2023 day 2 --- 2023/day2/src/main.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/2023/day2/src/main.rs b/2023/day2/src/main.rs index cf1cdf5..e106574 100644 --- a/2023/day2/src/main.rs +++ b/2023/day2/src/main.rs @@ -13,10 +13,8 @@ fn parse_colors(raw_colors: Vec<&str>) -> Cubes { let mut green = 0usize; let mut blue = 0usize; - println!("{:#?}", raw_colors); - for raw_color in raw_colors { - let color_split: Vec<&str> = raw_color.split(' ').collect(); + let color_split: Vec<&str> = raw_color.split(' ').map(|s| s.trim()).collect(); let color_split: Vec<&str> = color_split.into_iter().filter(|s| s.len() > 0).collect(); let [num_str, color] = color_split[..] else { panic!("Bad color row") @@ -67,15 +65,28 @@ fn is_valid(reference: &Cubes, comparison: &Cubes) -> bool { } fn validate_games(reference: &Cubes, games: CubeMap) -> Vec { - + games + .into_iter() + .filter(|(_, cubes)| cubes.iter().all(|c| is_valid(reference, c))) + .map(|(id, _)| id) + .collect() } fn main() { let file_str = include_str!("input.txt"); let mut map: CubeMap = HashMap::new(); + let reference = Cubes { + red: 12, + green: 13, + blue: 14, + }; file_str .split('\n') .for_each(|line| parse_game(line, &mut map)); - println!("{:#?}", map); + + let valid = validate_games(&reference, map); + let sum: usize = valid.into_iter().sum(); + + println!("Part 1 Sum of valid games: {}", sum); }