Advent of Code 2023: Day 2

This post is part of a number that I've written as I complete Advent of Code exercises. The purpose is to practice taking notes for other people as I solve a problem, and to get used to writing and publishing a post on a nearly daily basis.

Part 1

You have a bag and cubes that are either red, green, or blue. Each time you play the game, an elf will hide a secret number of cubes of each color in the bag. The goal is to figure out information about the cubes in the bag.

The Game

Once a bag has been loaded with cubes, the Elf will grab a random number of cubes, show them to you, then put them back in the bag. He does this a few times per game.

For the data file, each game takes up a line, and starts with its game ID. Each round is separated by a semicolon, where each round is a comma delimited list of what the Elf grabbed:

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

The first thing the Elf wants to know is what games would be possible if the bag only contained 12 red, 13 green, and 14 blue cubes. To calculate the final answer, we sum the IDs of the games where it's possible.

Solution

Since the number of cubes grabbed is random, it sounds like all we need to do is find impossible scenarios. An impossible scenario would be when the number of cubes grabbed is greater than our assumed counts.

It worked first try.

Part 2

Now the Elf wants to know: "In each game that we played, what is the fewest number of cubes of each color that could have been in the bag to make the game possible?"

To generate the answer, sum the power of the sets of minimum counts. The power is the multiplication of the values in the sets.

Solution

For this one, it seems that all we need to do is get the max number of each color in each round in order to find this. Then we'll have bounds on the minimum number needed. Then generate the power for the set.

This also worked first try.