Advent of code 2023 - Day 2: Cube Conundrum

Advent of code 2023 - Day 2: Cube Conundrum

2023-12-11
python

This year I try to record my attempt at solving the Advent of Code 2023 riddles. This is Day 2 - see https:adventofcode.com/2023/day/2

Part 1 #

Lets first read the task:

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?

Okay, let’s load our python kernel in emacs-jupyter and get coding! First of all, let’s load the input and split the riddle code by colon : to extract the game id and the rest of the code by semicolon ; to get the number of sets played in each game.

import pandas as pd
import re

txt = pd.read_table('data/2023-12-02-1-aoc.txt', names=['code'])
txt['id'] = txt.loc[:, 'code'].str.split(':').apply(
    lambda x: int(x[0].strip('Game ')))
txt['code'] = txt.loc[:, 'code'].str.split(':').apply(lambda x: x[1])
# txt['code'] = txt.loc[:, 'code'].str.split(';')
# txt['nsets'] = txt.loc[:, 'code'].apply(lambda x: len(x))
txt
                                                 code   id
0    1 green, 1 blue, 1 red; 1 green, 8 red, 7 blu...    1
1    9 red, 7 green, 3 blue; 15 green, 2 blue, 5 r...    2
2    3 red, 1 blue, 4 green; 6 red, 3 green, 2 blu...    3
3    2 blue, 2 green, 19 red; 3 blue, 11 red, 16 g...    4
4    8 green, 1 red, 12 blue; 10 green, 6 red, 13 ...    5
..                                                ...  ...
95    2 red, 2 green, 1 blue; 1 red, 4 green; 1 green   96
96   4 red, 5 green; 5 blue, 3 red; 8 blue, 2 gree...   97
97   1 blue; 2 green, 1 red; 5 red, 2 green; 4 red...   98
98   6 blue, 5 red, 2 green; 9 red, 1 blue; 2 gree...   99
99   1 blue, 13 green, 14 red; 11 green, 11 blue, ...  100

[100 rows x 2 columns]

Now, let’s extract the three colors in different columns with regex. We use the lookahead assertion ?= to find the respective colours and only exctract the digits \d+ coming before. Then we just keep the max imum drawn number of cubes per color, since this is the only information that matters at the moment.

txt['green'] = txt.loc[:, 'code'].apply(
    lambda code: re.findall(r'\d+(?=.green)', code)).apply(
        lambda list: max([int(i) for i in list]))
txt['red'] = txt.loc[:, 'code'].apply(
    lambda code: re.findall(r'\d+(?=.red)', code)).apply(
        lambda list: max([int(i) for i in list]))
txt['blue'] = txt.loc[:, 'code'].apply(
    lambda code: re.findall(r'\d+(?=.blue)', code)).apply(
        lambda list: max([int(i) for i in list]))
txt
                                                 code   id  green  red  blue
0    1 green, 1 blue, 1 red; 1 green, 8 red, 7 blu...    1      2   10    10
1    9 red, 7 green, 3 blue; 15 green, 2 blue, 5 r...    2     15   10     3
2    3 red, 1 blue, 4 green; 6 red, 3 green, 2 blu...    3      4    6    16
3    2 blue, 2 green, 19 red; 3 blue, 11 red, 16 g...    4     16   20    18
4    8 green, 1 red, 12 blue; 10 green, 6 red, 13 ...    5     10    6    14
..                                                ...  ...    ...  ...   ...
95    2 red, 2 green, 1 blue; 1 red, 4 green; 1 green   96      4    2     1
96   4 red, 5 green; 5 blue, 3 red; 8 blue, 2 gree...   97      5    4     8
97   1 blue; 2 green, 1 red; 5 red, 2 green; 4 red...   98      2    5     2
98   6 blue, 5 red, 2 green; 9 red, 1 blue; 2 gree...   99      2    9    11
99   1 blue, 13 green, 14 red; 11 green, 11 blue, ...  100     13   15    11

[100 rows x 5 columns]

Lastly, we just filter the DataFrame to only include games where all drawn cubes were below or equal the number of cubes in the game and sum the result!

txt['id'][(txt['green'] < 14) & (txt['red'] < 13) & (txt['blue'] < 15)].sum()
3035

Part 2 #

First, let’s get the instruction from the second part:

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?

Luckily, this task is made trivial by the approach we have taken before. We just have to multiply the green, red and blue columns:

txt['power'] = txt.loc[:, 'green'] * txt.loc[:, 'blue'] * txt.loc[:, 'red']
txt
                                                 code   id  green  red  blue  \
0    1 green, 1 blue, 1 red; 1 green, 8 red, 7 blu...    1      2   10    10
1    9 red, 7 green, 3 blue; 15 green, 2 blue, 5 r...    2     15   10     3
2    3 red, 1 blue, 4 green; 6 red, 3 green, 2 blu...    3      4    6    16
3    2 blue, 2 green, 19 red; 3 blue, 11 red, 16 g...    4     16   20    18
4    8 green, 1 red, 12 blue; 10 green, 6 red, 13 ...    5     10    6    14
..                                                ...  ...    ...  ...   ...
95    2 red, 2 green, 1 blue; 1 red, 4 green; 1 green   96      4    2     1
96   4 red, 5 green; 5 blue, 3 red; 8 blue, 2 gree...   97      5    4     8
97   1 blue; 2 green, 1 red; 5 red, 2 green; 4 red...   98      2    5     2
98   6 blue, 5 red, 2 green; 9 red, 1 blue; 2 gree...   99      2    9    11
99   1 blue, 13 green, 14 red; 11 green, 11 blue, ...  100     13   15    11

    power
0     200
1     450
2     384
3    5760
4     840
..    ...
95      8
96    160
97     20
98    198
99   2145

[100 rows x 6 columns]

And for this one, the sum is:

txt['power'].sum()
66027

comments powered by Disqus