Start on part 2 of 2023 day 2

This commit is contained in:
Timothy Warren 2023-12-06 08:25:17 -05:00
parent c655326e5b
commit 0d46c79e14
2 changed files with 57 additions and 10 deletions

View File

@ -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 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?** 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?**

View File

@ -1,21 +1,24 @@
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, PartialOrd, PartialEq)] #[derive(Debug, PartialOrd, PartialEq)]
struct Cubes { struct CubeSet {
red: usize, red: usize,
blue: usize, blue: usize,
green: usize, green: usize,
} }
type CubeMap = HashMap<usize, Vec<Cubes>>; type CubeMap = HashMap<usize, Vec<CubeSet>>;
fn parse_colors(raw_colors: Vec<&str>) -> Cubes { fn parse_colors(raw_colors: Vec<&str>) -> CubeSet {
let mut red = 0usize; let mut red = 0usize;
let mut green = 0usize; let mut green = 0usize;
let mut blue = 0usize; let mut blue = 0usize;
for raw_color in raw_colors { for raw_color in raw_colors {
let color_split: Vec<&str> = raw_color.split(' ').map(|s| s.trim()).collect(); let color_split: Vec<&str> = raw_color
let color_split: Vec<&str> = color_split.into_iter().filter(|s| s.len() > 0).collect(); .split(' ')
.map(|s| s.trim())
.filter(|s| s.len() > 0)
.collect();
let [num_str, color] = color_split[..] else { let [num_str, color] = color_split[..] else {
panic!("Bad color row") 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) { fn parse_game(line: &str, map: &mut CubeMap) {
let mut draws: Vec<Cubes> = Vec::new(); let mut draws: Vec<CubeSet> = Vec::new();
let raw: Vec<&str> = line.split(": ").map(|s| s.trim()).collect(); let raw: Vec<&str> = line.split(": ").map(|s| s.trim()).collect();
if raw.len() < 2 { if raw.len() < 2 {
return; return;
@ -58,24 +61,35 @@ fn parse_game(line: &str, map: &mut CubeMap) {
map.insert(id, draws); 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.green >= comparison.green
&& reference.blue >= comparison.blue && reference.blue >= comparison.blue
&& reference.red >= comparison.red && reference.red >= comparison.red
} }
fn validate_games(reference: &Cubes, games: CubeMap) -> Vec<usize> { fn filter_valid_games(reference: &CubeSet, games: CubeMap) -> CubeMap {
games games
.into_iter() .into_iter()
.filter(|(_, cubes)| cubes.iter().all(|c| is_valid(reference, c))) .filter(|(_, cubes)| cubes.iter().all(|c| is_valid(reference, c)))
.collect()
}
fn validate_games(reference: &CubeSet, games: CubeMap) -> Vec<usize> {
filter_valid_games(reference, games)
.into_iter()
.map(|(id, _)| id) .map(|(id, _)| id)
.collect() .collect()
} }
fn find_min_cubes(valid_games: &CubeMap) -> CubeMap {
let games = valid_games.clone().to_owned();
todo!();
}
fn main() { fn main() {
let file_str = include_str!("input.txt"); let file_str = include_str!("input.txt");
let mut map: CubeMap = HashMap::new(); let mut map: CubeMap = HashMap::new();
let reference = Cubes { let reference = CubeSet {
red: 12, red: 12,
green: 13, green: 13,
blue: 14, blue: 14,
@ -90,3 +104,6 @@ fn main() {
println!("Part 1 Sum of valid games: {}", sum); println!("Part 1 Sum of valid games: {}", sum);
} }
#[cfg(test)]
mod tests {}