24
submitted 7 months ago* (last edited 7 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 2: Cube Conundrum


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


πŸ”’This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

πŸ”“ Edit: Post has been unlocked after 6 minutes

you are viewing a single comment's thread
view the rest of the comments
[-] Cyno@programming.dev 3 points 7 months ago

Was pretty simple in Python with a regex to get the game number, and then the count of color. for part 2 instead of returning true/false whether the game is valid, you just max the count per color. No traps like in the first one, that I've seen, so it was surprisingly easy

def process_game(line: str):
    game_id = int(re.findall(r'game (\d+)*', line)[0])

    colon_idx = line.index(":")
    draws = line[colon_idx+1:].split(";")
    # print(draws)
    
    if is_game_valid(draws):
        # print("Game %d is possible"%game_id)
        return game_id
    return 0

            
def is_game_valid(draws: list):
    for draw in draws:
        red = get_nr_of_in_draw(draw, 'red')
        if red > MAX_RED:
            return False
        
        green = get_nr_of_in_draw(draw, 'green')
        if green > MAX_GREEN:
            return False
        
        blue = get_nr_of_in_draw(draw, 'blue')
        if blue > MAX_BLUE:
            return False    
    return True
        
            
def get_nr_of_in_draw(draw: str, color: str):
    if color in draw:
        nr = re.findall(r'(\d+) '+color, draw)
        return int(nr[0])
    return 0


# f = open("input.txt", "r")
f = open("input_real.txt", "r")
lines = f.readlines()
sum = 0
for line in lines:
    sum += process_game(line.strip().lower())
print("Answer: %d"%sum)
this post was submitted on 02 Dec 2023
24 points (96.2% liked)

Advent Of Code

736 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 11 months ago
MODERATORS