42 lines
1.0 KiB
Python
Executable File
42 lines
1.0 KiB
Python
Executable File
#!/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]
|
|
|
|
gamesum = 0
|
|
|
|
for line in lines:
|
|
print(line)
|
|
matches = re.match("^Game (\\d+):(.*)", line)
|
|
if matches is not None:
|
|
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])
|
|
print(roundcolors)
|
|
roundstate = validateround(roundcolors)
|
|
if not roundstate:
|
|
break
|
|
if not roundstate:
|
|
print("Game not valid")
|
|
continue
|
|
gamesum += gameid
|
|
|
|
print(gamesum)
|