adventofcode-2023/02/part02.py
Peter Hudec 5d24cb67a1 day02
2023-12-04 22:06:10 +01:00

34 lines
567 B
Python

#!/usr/bin/env python
from sys import stdin
import re
def read_line():
for line in stdin:
yield line
sum = 0
for line in read_line():
t1 = line.split(":", 2)
num = int(t1[0].split(" ")[-1])
rounds = t1[1].split(";")
red = 0
green = 0
blue = 0
for round in rounds:
m = re.search("(\d+) blue", round)
if m:
blue = max(blue, int(m.group(1)))
m = re.search("(\d+) red", round)
if m:
red = max(red, int(m.group(1)))
m = re.search("(\d+) green", round)
if m:
green = max(green, int(m.group(1)))
sum += (red * green * blue)
print(sum)