This commit is contained in:
Thorn Avery 2023-12-03 08:38:52 +11:00
parent cfe64a1daf
commit 40596ae83b
9 changed files with 306 additions and 1 deletions

View file

@ -8,5 +8,11 @@ fn main() -> Result<(), String> {
println!("solution: {a}, {b}");
Ok(())
})?;
println!("== day 02 ==");
timeit(|| -> Result<(), String> {
let (a, b) = day02::solution(day02::inputs::INPUT)?;
println!("solution: {a}, {b}");
Ok(())
})?;
Ok(())
}

View file

@ -1,5 +1,5 @@
#[cfg(test)]
use crate::problems::day01::{inputs::*, solution};
use super::{inputs::*, solution};
#[cfg(test)]
use std::assert_eq;

View file

@ -0,0 +1,56 @@
--- Day 2: Cube Conundrum ---
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you.
The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime?
As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.
To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.
You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).
For example, the record of a few games might look like this:
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, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.
The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the 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?
Your puzzle answer was 2085.
--- 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?
Your puzzle answer was 79315.
Both parts of this puzzle are complete! They provide two gold stars: **

View file

@ -0,0 +1,5 @@
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

View file

@ -0,0 +1,100 @@
Game 1: 8 green, 4 red, 4 blue; 1 green, 6 red, 4 blue; 7 red, 4 green, 1 blue; 2 blue, 8 red, 8 green
Game 2: 11 green, 7 blue, 15 red; 13 red, 6 green, 2 blue; 7 green, 9 red, 10 blue; 5 blue, 1 red, 11 green; 4 blue, 18 green; 4 red, 8 blue, 18 green
Game 3: 7 red, 10 green, 4 blue; 2 blue, 3 green, 5 red; 9 red, 7 green, 3 blue; 3 blue, 6 green, 18 red
Game 4: 1 blue, 2 green, 5 red; 10 red, 1 blue, 3 green; 14 red
Game 5: 16 red, 4 green, 19 blue; 13 blue, 9 red; 7 green, 14 red, 2 blue; 5 green, 18 blue, 4 red; 7 red, 2 blue, 3 green; 8 blue, 12 green
Game 6: 3 blue, 8 green; 4 red, 5 green; 5 red, 8 blue, 5 green; 8 blue, 4 green; 3 red, 7 green, 2 blue
Game 7: 7 red, 2 blue; 5 red, 6 green; 10 red; 2 blue, 12 red, 2 green; 4 red, 1 blue, 1 green; 9 green, 2 blue, 10 red
Game 8: 3 red, 7 green, 3 blue; 2 green, 3 blue, 3 red; 3 blue, 4 green, 1 red; 3 blue, 3 red; 2 blue, 6 green; 2 red, 7 green, 1 blue
Game 9: 5 red, 6 green; 6 red; 2 blue, 3 green, 9 red; 6 green, 2 blue
Game 10: 5 red, 8 blue, 5 green; 15 red, 6 green, 7 blue; 8 blue, 6 red, 5 green; 5 green, 2 blue; 12 red, 6 blue; 6 green, 16 red, 6 blue
Game 11: 3 red, 7 blue; 2 red, 3 blue, 6 green; 4 red, 5 green, 2 blue
Game 12: 15 green, 6 red, 2 blue; 3 red, 8 green, 9 blue; 8 blue, 2 red, 7 green; 5 red, 11 green, 6 blue
Game 13: 1 blue; 1 green, 6 red; 8 red, 3 blue; 5 blue, 8 red, 2 green
Game 14: 4 green, 1 blue; 8 blue, 5 green, 1 red; 2 green, 8 blue, 3 red
Game 15: 7 blue, 9 red, 8 green; 9 green, 12 blue, 5 red; 8 green, 10 blue, 2 red
Game 16: 1 blue, 1 red, 14 green; 6 green, 4 blue, 1 red; 16 blue, 14 green; 6 green, 2 red, 9 blue; 1 red, 1 blue; 2 red, 13 green, 7 blue
Game 17: 1 red, 9 green; 9 red, 5 blue, 2 green; 2 red, 8 green, 5 blue; 5 blue, 1 green, 3 red; 3 green, 5 blue; 3 red, 5 blue, 9 green
Game 18: 3 blue, 5 red, 3 green; 8 blue, 14 green; 3 blue, 11 red; 2 blue, 10 red, 9 green
Game 19: 7 green, 8 blue, 10 red; 1 green, 10 red, 2 blue; 6 blue, 4 red, 11 green; 7 red, 4 green; 6 blue, 4 red; 4 green, 2 red, 3 blue
Game 20: 5 red; 16 blue, 4 green, 7 red; 1 red, 1 green, 4 blue; 3 blue, 4 green, 7 red; 5 blue, 2 red, 3 green; 17 blue, 5 red
Game 21: 11 red, 4 green, 4 blue; 11 red, 5 blue, 2 green; 11 red, 13 green, 2 blue; 13 green, 6 red, 5 blue; 7 green, 4 blue, 8 red; 6 red, 8 blue, 14 green
Game 22: 2 green, 1 red, 6 blue; 3 blue, 2 green; 7 green, 10 red; 10 red, 7 blue, 5 green
Game 23: 14 green, 8 blue; 2 red, 5 green; 8 red, 16 green, 10 blue; 1 green, 7 red, 8 blue; 15 green, 5 red; 15 green, 2 red, 5 blue
Game 24: 4 blue, 1 green, 1 red; 1 blue, 8 green, 3 red; 4 red, 9 blue, 4 green
Game 25: 2 blue, 1 green, 3 red; 2 green, 4 red; 2 green, 1 blue
Game 26: 3 red, 5 green, 4 blue; 5 green, 8 blue; 1 blue, 3 green, 3 red; 7 blue, 2 red
Game 27: 13 red, 9 green, 6 blue; 19 blue, 9 red, 4 green; 14 red, 4 green, 11 blue; 5 green, 12 blue, 7 red
Game 28: 9 green, 4 blue, 15 red; 18 blue, 14 red, 12 green; 14 red, 12 blue, 3 green; 6 blue, 7 green, 15 red; 17 blue, 6 green, 9 red; 7 red, 12 green
Game 29: 17 red, 2 blue, 9 green; 5 blue, 15 red, 11 green; 13 red, 3 green, 5 blue; 4 green, 11 red, 1 blue; 5 green, 1 blue
Game 30: 3 green, 12 blue; 15 red, 2 blue, 6 green; 12 red, 11 blue, 10 green; 9 blue, 7 green, 14 red; 1 green, 7 red, 5 blue
Game 31: 6 green, 7 red, 18 blue; 2 green, 3 blue, 10 red; 9 blue, 8 red; 18 blue, 10 green, 4 red; 14 blue, 11 red, 10 green
Game 32: 7 green, 1 red, 5 blue; 5 green, 3 blue; 6 blue, 5 green; 5 red, 11 green, 6 blue
Game 33: 9 blue, 5 red; 2 blue; 6 red, 8 blue; 10 blue, 4 green, 6 red; 2 red, 4 green; 1 red, 6 blue, 2 green
Game 34: 1 red, 6 green; 3 green, 6 red, 2 blue; 2 red, 2 blue, 1 green; 5 green, 1 red, 2 blue
Game 35: 7 red, 2 green, 4 blue; 4 red, 2 green, 3 blue; 14 green, 2 blue, 5 red; 14 green, 2 red, 3 blue; 5 green, 2 red, 1 blue; 2 green, 8 red, 3 blue
Game 36: 7 green, 1 red, 1 blue; 3 blue, 6 green, 9 red; 2 blue, 17 red; 18 red, 2 green
Game 37: 6 red, 14 blue; 3 green, 2 blue; 18 blue, 6 red; 3 red, 18 blue, 3 green
Game 38: 6 blue, 8 red; 9 blue, 1 green, 1 red; 2 green, 10 blue, 1 red
Game 39: 9 red, 2 green; 7 red, 2 blue, 2 green; 2 blue, 1 red, 8 green
Game 40: 15 green, 1 blue, 1 red; 3 blue, 1 red, 6 green; 2 blue, 11 green; 2 blue, 11 green; 3 green, 4 blue, 1 red; 2 blue, 6 green
Game 41: 4 blue, 9 green; 11 green, 1 blue, 7 red; 1 red, 3 blue, 17 green; 17 green, 2 red; 2 blue, 16 green, 8 red; 1 blue, 8 green, 3 red
Game 42: 7 green, 15 blue, 2 red; 1 green, 5 blue, 5 red; 4 green, 6 red; 4 red, 4 blue, 1 green; 1 blue, 4 red
Game 43: 10 green, 6 blue, 8 red; 9 green, 9 red, 4 blue; 6 blue, 3 green, 6 red; 8 blue, 16 green, 7 red
Game 44: 14 red, 14 blue; 11 red, 1 blue; 1 green, 9 red, 15 blue; 1 green, 10 red
Game 45: 10 blue, 5 red; 1 green, 2 red, 11 blue; 2 blue, 10 red, 1 green
Game 46: 10 red, 9 green, 5 blue; 9 green, 2 red, 5 blue; 3 blue, 14 red, 4 green; 2 red, 1 green, 6 blue; 2 red, 9 blue, 4 green; 5 green, 2 blue, 11 red
Game 47: 9 red, 5 green, 1 blue; 3 red, 1 green; 8 red, 2 blue, 10 green; 9 green, 9 red, 2 blue; 13 green, 3 blue, 6 red
Game 48: 15 blue, 3 red, 8 green; 2 blue; 12 green, 7 blue, 1 red
Game 49: 9 blue, 3 red, 2 green; 12 green, 17 blue, 2 red; 3 green, 1 red, 8 blue; 16 blue, 3 green, 3 red
Game 50: 17 blue, 2 red, 14 green; 18 blue, 12 green; 13 blue, 6 red, 8 green; 4 blue, 6 red, 4 green
Game 51: 3 blue, 8 green, 4 red; 5 blue, 5 green, 4 red; 2 red, 8 blue, 2 green; 8 green, 3 blue; 9 green, 5 blue, 1 red
Game 52: 9 red, 5 green, 6 blue; 3 red, 8 green; 2 red, 3 blue, 6 green; 8 red, 4 blue, 2 green
Game 53: 6 green, 13 blue; 4 red, 7 green; 2 green, 7 red; 11 red, 2 blue; 12 blue, 9 green, 8 red
Game 54: 3 red, 3 blue, 3 green; 3 green, 2 red, 1 blue; 13 blue, 3 green; 5 red, 4 green, 13 blue; 4 green, 7 blue, 9 red
Game 55: 15 blue, 3 green, 2 red; 1 red, 8 green; 19 blue, 7 green; 4 green, 19 blue; 9 blue, 7 green, 2 red
Game 56: 1 blue, 9 green, 11 red; 3 blue, 12 green, 2 red; 1 blue, 12 green, 4 red; 16 green, 3 blue, 3 red; 18 red, 9 green
Game 57: 7 blue, 6 red, 3 green; 11 red, 5 blue, 4 green; 9 blue, 1 green, 5 red
Game 58: 17 green, 17 red, 6 blue; 10 red, 13 blue, 1 green; 7 red, 14 green; 6 red, 2 blue, 8 green; 13 red, 13 blue, 4 green
Game 59: 1 green, 4 red, 1 blue; 1 red, 1 green; 1 red; 4 blue; 6 red, 6 blue, 1 green
Game 60: 7 blue, 17 red, 8 green; 12 green, 6 red, 8 blue; 1 red, 9 blue, 12 green
Game 61: 2 green, 15 blue, 2 red; 1 green, 9 blue, 12 red; 12 red, 3 green, 19 blue
Game 62: 17 red, 1 blue; 5 green, 16 blue, 14 red; 3 red, 7 blue; 8 blue, 3 red, 1 green
Game 63: 9 red, 9 blue; 10 blue, 6 red, 9 green; 11 green, 1 blue, 13 red; 6 green, 13 blue, 9 red
Game 64: 16 blue, 2 red; 9 blue; 10 red, 1 blue, 4 green
Game 65: 1 blue, 18 green; 19 green, 1 red; 10 green, 1 blue, 1 red
Game 66: 12 blue, 5 green, 13 red; 3 green, 3 blue; 1 green, 1 blue, 11 red
Game 67: 5 red, 2 green; 7 red, 3 blue; 1 green, 8 red, 6 blue; 2 red, 5 blue; 8 red, 6 blue
Game 68: 8 green, 6 red, 18 blue; 8 green, 6 red, 3 blue; 4 red, 14 blue, 11 green
Game 69: 6 blue, 3 red, 1 green; 4 green, 6 red, 6 blue; 2 green, 13 red; 9 red, 5 blue, 6 green; 2 green, 11 red; 6 green, 2 red, 1 blue
Game 70: 7 blue, 18 green, 12 red; 17 green, 2 red, 8 blue; 17 red, 13 green, 2 blue; 1 red, 2 green, 7 blue
Game 71: 3 blue, 1 red, 15 green; 13 green, 2 red, 8 blue; 7 green, 12 blue; 7 blue, 12 green; 7 blue, 5 green
Game 72: 6 blue, 18 red; 1 blue, 4 green, 3 red; 11 red, 3 green; 6 blue, 5 red, 13 green; 11 green, 16 red, 5 blue; 3 green, 5 blue
Game 73: 18 red, 1 blue, 3 green; 4 blue, 4 red, 4 green; 1 blue, 3 red; 2 red, 1 green
Game 74: 5 red; 12 red, 4 green; 4 green, 5 red; 2 red, 1 blue, 4 green
Game 75: 2 red, 2 blue, 6 green; 2 blue, 6 green; 3 green, 1 red
Game 76: 6 green, 1 blue, 12 red; 2 green, 2 red, 3 blue; 3 green, 10 red, 3 blue; 3 blue, 16 red, 11 green; 15 red, 5 blue, 7 green; 4 green, 4 red, 3 blue
Game 77: 14 green, 4 blue, 11 red; 12 blue, 9 red, 19 green; 10 green, 4 blue
Game 78: 1 blue, 11 red; 1 blue, 14 green, 4 red; 7 green, 3 blue, 5 red
Game 79: 3 red, 13 blue; 14 blue, 5 red, 5 green; 1 green, 7 blue, 2 red; 5 green, 13 blue, 3 red; 7 blue, 5 red, 2 green
Game 80: 2 green, 11 blue; 2 red, 12 blue, 1 green; 11 blue, 11 green; 2 green, 12 blue, 1 red
Game 81: 8 blue, 5 green, 1 red; 1 red, 6 blue, 4 green; 6 green, 10 blue; 2 red, 9 blue, 2 green; 6 blue, 2 green; 6 green, 9 blue, 2 red
Game 82: 13 red, 12 green, 3 blue; 4 blue, 4 red, 1 green; 4 green, 8 red, 2 blue; 7 red, 13 green
Game 83: 2 green, 8 red, 1 blue; 2 blue, 6 red, 8 green; 5 green, 1 blue
Game 84: 6 red, 18 blue, 5 green; 3 green, 15 blue, 2 red; 6 red, 6 blue, 8 green; 1 red, 4 blue, 2 green; 2 blue, 5 green
Game 85: 3 red, 11 green, 2 blue; 3 blue, 6 green; 2 red, 4 green, 4 blue; 1 blue, 3 red, 10 green; 4 blue, 7 green, 4 red
Game 86: 7 green, 16 blue; 4 blue, 1 green, 7 red; 6 red, 15 green, 9 blue; 7 green, 9 red, 2 blue; 14 green, 2 blue
Game 87: 8 red, 3 blue, 8 green; 3 red, 1 blue, 8 green; 6 red, 5 green; 2 red, 6 green, 2 blue; 6 green, 2 red
Game 88: 1 blue, 4 red, 12 green; 4 red, 1 blue, 14 green; 1 blue, 10 green, 4 red; 1 blue, 10 red, 9 green
Game 89: 7 green, 6 red, 10 blue; 4 red, 5 green; 2 green, 13 blue, 2 red
Game 90: 16 blue, 2 green, 10 red; 4 green, 7 red, 14 blue; 4 blue, 11 green, 3 red; 3 red, 10 blue, 3 green
Game 91: 7 green, 7 red, 4 blue; 14 red, 11 blue; 16 red, 8 green, 15 blue
Game 92: 5 blue, 12 red, 3 green; 2 blue, 8 green, 5 red; 5 blue, 10 green; 11 green, 6 red, 4 blue; 5 red, 4 green, 4 blue
Game 93: 4 blue, 3 green, 5 red; 7 red, 17 blue; 8 blue, 7 green; 17 blue, 1 green; 2 red, 6 blue, 2 green; 15 blue, 3 red, 4 green
Game 94: 9 green, 3 red, 2 blue; 3 green, 6 red; 13 green, 4 red, 2 blue; 7 green; 4 green, 7 red; 2 red, 9 green, 2 blue
Game 95: 3 green, 11 red, 5 blue; 6 blue, 8 green; 9 green, 6 blue; 6 red, 1 green
Game 96: 3 blue, 3 green, 10 red; 2 blue, 12 red; 4 red, 3 blue; 2 green, 3 red, 1 blue; 2 green, 6 blue
Game 97: 5 blue, 3 green, 2 red; 2 blue, 3 green, 5 red; 12 red, 3 blue
Game 98: 2 blue, 2 red, 9 green; 4 green, 5 blue, 1 red; 15 green, 3 red, 9 blue
Game 99: 15 red, 7 green, 11 blue; 2 blue, 12 green, 17 red; 6 red, 3 blue, 11 green; 14 red, 13 green, 5 blue
Game 100: 5 green, 17 blue, 5 red; 15 blue; 13 green, 8 red, 3 blue; 16 blue, 15 green, 8 red; 16 green, 2 blue, 3 red

View file

@ -0,0 +1,7 @@
pub const EXAMPLE_INPUT: &str = include_str!("example.txt");
pub const EXAMPLE_OUTPUT_A: u32 = 8;
pub const EXAMPLE_OUTPUT_B: u32 = 2286;
pub const INPUT: &str = include_str!("input.txt");
pub const OUTPUT_A: u32 = 2085;
pub const OUTPUT_B: u32 = 79315;

113
src/problems/day02/mod.rs Normal file
View file

@ -0,0 +1,113 @@
use std::collections::BTreeMap;
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{digit1, newline, space0},
combinator::{map, map_res, opt},
multi::{many1, separated_list1},
sequence::{pair, preceded, terminated},
IResult,
};
pub mod inputs;
pub mod test;
type Int = u32;
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone)]
enum Colour {
Blue,
Red,
Green,
}
impl Colour {
fn limit(&self) -> Int {
match self {
Colour::Blue => 14,
Colour::Red => 12,
Colour::Green => 13,
}
}
}
#[derive(Debug)]
struct Pick {
colour: Colour,
quantity: Int,
}
type Hand = Vec<Pick>;
#[derive(Debug)]
struct Game {
id: Int,
hands: Vec<Hand>,
}
fn parse_colour(input: &str) -> IResult<&str, Colour> {
alt((
map(tag("blue"), |_| Colour::Blue),
map(tag("red"), |_| Colour::Red),
map(tag("green"), |_| Colour::Green),
))(input)
}
fn parse_number(input: &str) -> IResult<&str, Int> {
map_res(digit1, |d: &str| d.parse::<Int>())(input)
}
fn parse_pick(input: &str) -> IResult<&str, Pick> {
let (input, quantity) = parse_number(input)?;
let (input, _) = space0(input)?;
let (input, colour) = parse_colour(input)?;
let pick = Pick { colour, quantity };
Ok((input, pick))
}
fn parse_hand(input: &str) -> IResult<&str, Hand> {
separated_list1(pair(tag(","), space0), parse_pick)(input)
}
fn parse_game(input: &str) -> IResult<&str, Game> {
let (input, id) = preceded(pair(tag("Game"), space0), parse_number)(input)?;
let (input, _) = pair(tag(":"), space0)(input)?;
let (input, hands) = separated_list1(pair(tag(";"), space0), parse_hand)(input)?;
let game = Game { id, hands };
Ok((input, game))
}
fn parse_input(input: &str) -> IResult<&str, Vec<Game>> {
terminated(many1(terminated(parse_game, newline)), opt(newline))(input)
}
pub fn solution(input: &str) -> Result<(Int, Int), String> {
let (_, output) = parse_input(input).map_err(|e| e.to_string())?;
let (a, b) = output
.into_iter()
.map(|g| {
let mut limits: BTreeMap<Colour, Int> =
vec![(Colour::Red, 0), (Colour::Green, 0), (Colour::Blue, 0)]
.into_iter()
.collect();
for h in &g.hands {
for p in h {
let xp = limits.entry(p.colour.clone()).or_insert(0);
*xp = p.quantity.max(*xp);
}
}
let valid = g
.hands
.into_iter()
.all(|h| h.into_iter().all(|p| p.quantity <= p.colour.limit()));
let power: Int = limits.into_values().product();
if valid {
(g.id, power)
} else {
(0, power)
}
})
.fold((0, 0), |(a, b), (c, d)| (a + c, b + d));
Ok((a, b))
}

View file

@ -0,0 +1,17 @@
#[cfg(test)]
use super::{inputs::*, solution};
#[cfg(test)]
use std::assert_eq;
#[test]
pub fn example_input() {
assert_eq!(
solution(EXAMPLE_INPUT),
Ok((EXAMPLE_OUTPUT_A, EXAMPLE_OUTPUT_B))
)
}
#[test]
pub fn input() {
assert_eq!(solution(INPUT), Ok((OUTPUT_A, OUTPUT_B)))
}

View file

@ -1 +1,2 @@
pub mod day01;
pub mod day02;