From 0d46c79e14966b4109c7ed436733400d95af90ce Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 6 Dec 2023 08:25:17 -0500 Subject: [PATCH] Start on part 2 of 2023 day 2 --- 2023/day2/README.md | 30 ++++++++++++++++++++++++++++++ 2023/day2/src/main.rs | 37 +++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/2023/day2/README.md b/2023/day2/README.md index eca4b61..727316b 100644 --- a/2023/day2/README.md +++ b/2023/day2/README.md @@ -44,3 +44,33 @@ IDs of the games that would have been possible, you get **8**. Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. **What is the sum of the IDs of those games?** + +## Part Two + +The Elf says they've stopped producing snow because they aren't getting any **water**! He isn't sure why the water stopped; +however, he can show you how to get to the water source to check it out for yourself. It's just up ahead! + +As you continue your walk, the Elf poses a second question: in each game you played, **what is the fewest number of cubes +of each color** that could have been in the bag to make the game possible? + +Again consider the example games from earlier: + +``` +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green +``` + +- In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible. +- Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes. +- Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes. +- Game 4 required at least 14 red, 3 green, and 15 blue cubes. +- Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag. + +The **power** of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. +The power of the minimum set of cubes in game 1 is `48`. In games 2-5 it was `12`, `1560`, `630`, and `36`, respectively. +Adding up these five powers produces the sum `**2286**`. + +For each game, find the minimum set of cubes that must have been present. **What is the sum of the power of these sets?** diff --git a/2023/day2/src/main.rs b/2023/day2/src/main.rs index e106574..8db22e8 100644 --- a/2023/day2/src/main.rs +++ b/2023/day2/src/main.rs @@ -1,21 +1,24 @@ use std::collections::HashMap; #[derive(Debug, PartialOrd, PartialEq)] -struct Cubes { +struct CubeSet { red: usize, blue: usize, green: usize, } -type CubeMap = HashMap>; +type CubeMap = HashMap>; -fn parse_colors(raw_colors: Vec<&str>) -> Cubes { +fn parse_colors(raw_colors: Vec<&str>) -> CubeSet { let mut red = 0usize; let mut green = 0usize; let mut blue = 0usize; for raw_color in raw_colors { - 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 color_split: Vec<&str> = raw_color + .split(' ') + .map(|s| s.trim()) + .filter(|s| s.len() > 0) + .collect(); let [num_str, color] = color_split[..] else { panic!("Bad color row") }; @@ -35,11 +38,11 @@ fn parse_colors(raw_colors: Vec<&str>) -> Cubes { } } - Cubes { red, blue, green } + CubeSet { red, blue, green } } fn parse_game(line: &str, map: &mut CubeMap) { - let mut draws: Vec = Vec::new(); + let mut draws: Vec = Vec::new(); let raw: Vec<&str> = line.split(": ").map(|s| s.trim()).collect(); if raw.len() < 2 { return; @@ -58,24 +61,35 @@ fn parse_game(line: &str, map: &mut CubeMap) { map.insert(id, draws); } -fn is_valid(reference: &Cubes, comparison: &Cubes) -> bool { +fn is_valid(reference: &CubeSet, comparison: &CubeSet) -> bool { reference.green >= comparison.green && reference.blue >= comparison.blue && reference.red >= comparison.red } -fn validate_games(reference: &Cubes, games: CubeMap) -> Vec { +fn filter_valid_games(reference: &CubeSet, games: CubeMap) -> CubeMap { games .into_iter() .filter(|(_, cubes)| cubes.iter().all(|c| is_valid(reference, c))) + .collect() +} + +fn validate_games(reference: &CubeSet, games: CubeMap) -> Vec { + filter_valid_games(reference, games) + .into_iter() .map(|(id, _)| id) .collect() } +fn find_min_cubes(valid_games: &CubeMap) -> CubeMap { + let games = valid_games.clone().to_owned(); + todo!(); +} + fn main() { let file_str = include_str!("input.txt"); let mut map: CubeMap = HashMap::new(); - let reference = Cubes { + let reference = CubeSet { red: 12, green: 13, blue: 14, @@ -90,3 +104,6 @@ fn main() { println!("Part 1 Sum of valid games: {}", sum); } + +#[cfg(test)] +mod tests {}