this post was submitted on 02 Dec 2023
24 points (96.2% liked)

Advent Of Code

763 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 1 year ago
MODERATORS
24
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

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
[–] [email protected] 2 points 11 months ago

My solution in python

input="""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"""

def parse(line):
    data={}
    data['game']=int(line[5:line.index(':')])
    data['hands']=[]
    for str in line[line.index(':')+1:].split(';'):
        h={'red':0,'green':0,'blue':0}
        for str2 in str.split(','):
            tmp=str2.strip(' ').split(' ')
            h[tmp[1]]=int(tmp[0])
        data['hands'].append(h)

    data['max_red']=max([x['red'] for x in data['hands']])
    data['max_green']=max([x['green'] for x in data['hands']])
    data['max_blue']=max([x['blue'] for x in data['hands']])
    data['power']=data['max_red']*data['max_green']*data['max_blue']
    data['possible'] = True
    if data['max_red'] > 12:
        data['possible'] = False
    if data['max_green'] > 13:
        data['possible'] = False
    if data['max_blue'] > 14:
        data['possible'] = False


    return data
def loadFile(path):
    with open(path,'r') as f:
        return f.read()

if __name__ == '__main__':
    input=loadFile('day2_input')
    res=[]
    total=0
    power_sum=0
    for row in input.split('\n'):
        data=parse(row)
        if data['possible']:
            total=total+data['game']
        power_sum=power_sum+data['power']
    print('total: %s, power: %s ' % (total,power_sum,))