🦌 - 2023 DAY 2 SOLUTIONS -🦌 - eviltoast

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

  • meant2live218@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    11 months ago

    My (awful) Python solves. Much easier than day 1’s, although I did run into an issue with trimming whitespace characters with my approach (Game 96 wouldn’t flag properly).

    Part 1
    with open('02A_input.txt', 'r') as file:
        data = file.readlines()
        
    possibleGames=[]
    
    for game in data:
        # Find Game number
        game = game.removeprefix("Game ")
        gameNumber = int(game[0:game.find(":")])
        # Break Game into rounds (split using semicolons)
        game=game[game.find(":")+1:]
        rounds=game.split(";")
        # For each round, determine the maximum number of Red, Blue, Green items shown at a time
        rgb=[0,0,0]
        for round in rounds:
            combos=round.split(",")
            for combo in combos:
                combo=combo.strip()
                number=int(combo[0:combo.find(" ")])
                if combo.endswith("red"):
                    if number>rgb[0]:
                        rgb[0]=number
                elif combo.endswith("green"):
                    if number>rgb[1]:
                        rgb[1]=number
                elif combo.endswith("blue"):
                    if number>rgb[2]:
                        rgb[2]=number
        # If Red>12, Green>13, Blue>14, append Game number to possibleGames
        if not (rgb[0]>12 or rgb[1]>13 or rgb[2]>14):
            possibleGames.append(gameNumber)
    
    print(sum(possibleGames))
    
    Part 2
    with open('02A_input.txt', 'r') as file:
        data = file.readlines()
        
    powers=[]
    
    for game in data:
        # Find Game number
        game = game.removeprefix("Game ")
        # Break Game into rounds (split using semicolons)
        game=game[game.find(":")+1:]
        rounds=game.split(";")
        # For each round, determine the maximum number of Red, Blue, Green items shown at a time
        # Note: This could be faster, since we don't need to worry about actual rounds
        rgb=[0,0,0]
        for round in rounds:
            combos=round.split(",")
            for combo in combos:
                combo=combo.strip()
                number=int(combo[0:combo.find(" ")])
                if combo.endswith("red"):
                    if number>rgb[0]:
                        rgb[0]=number
                elif combo.endswith("green"):
                    if number>rgb[1]:
                        rgb[1]=number
                elif combo.endswith("blue"):
                    if number>rgb[2]:
                        rgb[2]=number
        # Multiple R, G, B to find the "power" of the game
        # Append Power to the list
        powers.append(rgb[0]*rgb[1]*rgb[2])
        
    print(sum(powers))