Day 2 Part B passes
This commit is contained in:
29
adv02b.py
Normal file
29
adv02b.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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)
|
||||||
Reference in New Issue
Block a user