Files
Advent2023/2/advent_2.py

48 lines
1.4 KiB
Python
Raw Normal View History

2023-12-02 20:52:14 +02:00
#!/usr/bin/env python3
import re
LIMITS = {"red": 12, "green": 13, "blue": 14}
def validateround(round):
for k in round:
if LIMITS[k] < round[k]:
return False
return True
with open("data.txt") as file:
lines = [line.rstrip() for line in file]
gametotal = 0
for line in lines:
print(line)
matches = re.match("^Game (\\d+):(.*)", line)
if matches is not None:
gamemax = {"red": 0, "green": 0, "blue": 0}
gameid = int(matches.groups()[0])
rounds = matches.groups()[1].split(";")
for round in rounds:
colors = round.split(",")
roundcolors = {}
for color in colors:
colormatch = re.match("\\s*(\\d+)\\s+(red|green|blue)\\s*+", color)
roundcolors[colormatch.groups()[1]] = int(colormatch.groups()[0])
c = colormatch.groups()[1]
ccount = int(colormatch.groups()[0])
if ccount > gamemax[c]:
gamemax[c] = ccount
print(roundcolors)
print("#####")
print(gamemax)
gamesum = 0
for i in gamemax:
if gamemax[i] > 0 and gamesum != 0:
gamesum = gamesum * gamemax[i]
if gamemax[i] > 0 and gamesum == 0:
gamesum = gamemax[i]
print(gamesum)
gametotal += gamesum
print(gametotal)