Files
Advent2023/adv02b.py
2023-12-02 04:59:54 -06:00

30 lines
686 B
Python

import re
def numpart(s):
return int(''.join(c for c in s if c in '0123456789'))
def game(line):
id_part, cube_part = line.split(':')
id = numpart(id_part)
rounds = cube_part.split(';')
bp = 0
rp = 0
gp = 0
for round in rounds:
cubes = round.split(',')
for cube in cubes:
count = numpart(cube)
if 'blue' in cube:
bp = max(bp, count)
elif 'red' in cube:
rp = max(rp, count)
elif 'green' in cube:
gp = max(gp, count)
return bp * rp * gp
with open('adv02a.txt') as f:
txt = f.readlines()
s = sum(game(line) for line in txt)
print(s)