DEV Community

Cover image for Advent of Code 2023 - December 2nd
Rob van der Leek
Rob van der Leek

Posted on

Advent of Code 2023 - December 2nd

In this series, I'll share my progress with the 2023 version of Advent of Code.

Check the first post for a short intro to this series.

You can also follow my progress on GitHub.

December 2nd

The puzzle of day 2 was not so hard. The one from yesterday made me a bit nervous for what's to come, but it seems the Elves gave me a break today πŸ˜…

My pitfall for this puzzle: Not really any, programming the solution was pretty straightforward.

Solution here, do not click if you want to solve the puzzle first yourself
#!/usr/bin/env python3

valid_games = []
game_powers = []

def is_valid_draw(marbles):
    for marble in marbles:
        [count, color] = marble.split(' ')
        if (color == 'red' and int(count) > 12) or \
           (color == 'green' and int(count) > 13) or \
           (color == 'blue' and int(count) > 14):
            return False
    return True 

def is_valid_game(games):
    for game in games:
        marbles = game.split(', ') 
        if not is_valid_draw(marbles):
            return False
    return True

def game_power(game_marbles):
    red = 0
    green = 0
    blue = 0 
    for marbles in game_marbles:
        for marble in marbles:
            [count, color] = marble.split(' ')
            if (color == 'red' and int(count) > red):
                red = int(count)
            if (color == 'green' and int(count) > green):
                green = int(count)
            if (color == 'blue' and int(count) > blue):
                blue = int(count)
    return red * green * blue

with open('input.txt') as infile:
    lines = infile.readlines()
for line in lines:
    [game_id, all_games] = line.strip().split(': ')
    game_number = int(game_id.split(' ')[1])
    games = all_games.split('; ')
    if is_valid_game(games):
        valid_games.append(game_number)
    game_marbles = [game.split(', ') for game in games]
    game_powers.append(game_power(game_marbles))

print(sum(valid_games))
print(sum(game_powers))
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay