Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2023 Day 1
- Advent of Code 2023 Day 2
- Advent of Code 2023 Day 3
- Advent of Code 2023 Day 4
- Advent of Code 2023 Day 5
- Advent of Code 2023 Day 6
- Advent of Code 2023 Day 7
- Advent of Code 2023 Day 8
- Advent of Code 2023 Day 9
- Advent of Code 2023 Day 10
- Advent of Code 2023 Day 11
- Advent of Code 2023 Day 12
- Advent of Code 2023 Day 13
- Advent of Code 2023 Day 14
- Advent of Code 2023 Day 15
- Advent of Code 2023 Day 16
- Advent of Code 2023 Day 17
- Advent of Code 2023 Day 18
- Advent of Code 2023 Day 19
- Advent of Code 2023 Day 20
- Advent of Code 2023 Day 21
- Advent of Code 2023 Day 22
- Advent of Code 2023 Day 23
- Advent of Code 2023 Day 25
-
Older post
-
Newer post
Advent of Code 2023 Day 2
Day 2: Cube Conundrum
https://adventofcode.com/2023/day/2
You arrive at a sky island and an elf comes to greet you. They want to play a game while you walk to your destination.
In a small bag, there are an unknown amount of coloured cubes. The cubes are all either red, green, or blue.
Each round, the elf draws some cubes from the bag and shows them to you.
The goal of the game is to figure out which cubes are in the bag.
The input today is a list of game-reports.
- Each line describes one game.
- Each round (that lists the cubes the elf drew from the bag) is separated by a semicolon.
An example input looks like this:
That means in game 1 there were 3 draws:
- 3 blue cubes, and 4 red cubes
- 1 red cube, 2 green cubes, and 6 blue cubes
- 2 green cubes
After a draw is shown, all cubes are placed back into to bag before drawing from the bag again.
Part 1
The elf wants to know which games in your input would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes.
The question asks for the sum of each possible game ID.
In the example, game 1, 2, and 5 would have been possible, summing those IDs gives 8.
Option 1: Nested for loops
I decided to build up the sum while iterating through the lines of my input.
Some skeleton/pseudocode:
Starting to fill out that skeleton:
For each draw, several number/color pairs can be shown (ie: 3 blue, 4 red). I decided to implement a check to see if a certain pair was possible with the given cubes.
If it’s not, I skip to the next game. If all pairs in the current game are possible however, I increment the sum.
Code
Option 2: An iterator chain
A more organized, but slower solution.
This involves parsing each game first, then filtering out impossible games, and finally summing up the IDs of every remaining game.
In skeleton/pseudocode:
Helpers
Each line represents a Game
.
Within a game, several draws happen, represented by a list of Draw
.
Each draw has a number of red, green, and blue cubes. If no cubes of a color were drawn, the value of the field is 0.
I created a way to turn strings like “3 blue, 4 red”, or “1 red, 2 green, 6 blue” into a Draw
struct.
That way, turning each line into a Game
looks like this:
All that is left then, is to filter out invalid games, and sum the remaining games’ IDs.
Code
Part 2
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?
The question asks for sum of the power levels of all games.
The power level can be found by multiplying the minimum amount of red, blue, and green cubes to make a game valid.
The setup looks very similar to part one.
This time, per game, we keep track of the minimum amount of cubes of a certain color.
If we come across a draw with a larger amount of cubes for that color, that minimum is increased.
Per game, the power level is then calculated and added to a sum.