Skip to content

Part 1

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?

Solution

Draw dataclass

Dataclass for representing a draw during a game

Source code in aoc/day2/part1.py
44
45
46
47
48
49
50
51
52
53
54
55
56
@dataclass
class Draw:
    """Dataclass for representing a draw during a game"""

    red: int = 0
    green: int = 0
    blue: int = 0

    def __lt__(self, other: Self) -> bool:
        """Compare whether one Draw is less than another Draw"""
        return (
            self.red < other.red or self.green < other.green or self.blue < other.blue
        )

__lt__

__lt__(other: Self) -> bool

Compare whether one Draw is less than another Draw

Source code in aoc/day2/part1.py
52
53
54
55
56
def __lt__(self, other: Self) -> bool:
    """Compare whether one Draw is less than another Draw"""
    return (
        self.red < other.red or self.green < other.green or self.blue < other.blue
    )

game_is_valid

game_is_valid(draws: list[Draw]) -> bool

Test whether any draws are more than the maximum for each color

Parameters:

Name Type Description Default
draws list[Draw]

list of draws in the Game

required

Returns:

Type Description
bool

True if no draw has a larger color count then the max else False

Source code in aoc/day2/part1.py
62
63
64
65
66
67
68
69
70
71
def game_is_valid(draws: list[Draw]) -> bool:
    """Test whether any draws are more than the maximum for each color

    Args:
        draws: list of draws in the Game

    Returns:
        True if no draw has a larger color count then the max else False
    """
    return not any(max_draw < draw for draw in draws)

sum_valid_game_ids

sum_valid_game_ids(input_text: str) -> int

Parse each line into Game with id and draws. If game is valid add ID to cumulative sum.

Parameters:

Name Type Description Default
input_text str

Raw input text.

required

Returns:

Type Description
int

sum of all the valid Game IDs.

Source code in aoc/day2/part1.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def sum_valid_game_ids(input_text: str) -> int:
    """Parse each line into Game with id and draws.  If game is valid add ID to cumulative sum.

    Args:
        input_text: Raw input text.

    Returns:
        sum of all the valid Game IDs.
    """
    lines = input_text.splitlines()
    cum_sum = 0
    for line in lines:
        game_id = int(id_pattern.findall(line)[0])
        draw_strings = line.split(": ")[1].split("; ")
        draws = []
        for ds in draw_strings:
            draws.append(
                Draw(**{color: int(count) for count, color in draw_pattern.findall(ds)})
            )

        if game_is_valid(draws):
            cum_sum += game_id
    return cum_sum