30 lines
686 B
Python
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)
|