2. advent
This commit is contained in:
41
2/advent.py
Executable file
41
2/advent.py
Executable file
@@ -0,0 +1,41 @@
|
||||
#!/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)
|
||||
Reference in New Issue
Block a user