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