Skip to content
advent of code 2023

day 02

cube conundrum

approach

each game is parsed into its id and a list of Counters, one per revealed set, mapping colour to count.

part 1 sums the ids of games where no set shows more of a colour than the limits (12 red, 13 green, 14 blue). part 2 takes, per game, the maximum seen of each colour and adds up the product of those maxima.

the trick

the smallest bag that fits a game is just the per-colour maximum over its sets.

python
def part2(games):
    res = 0
    for gid, gconfig in games:
        mxs = Counter()
        for s in gconfig:
            for c, num in s.items():
                mxs[c] = max(mxs[c], num)
        res += prod(mxs.values())
    return res