54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
|
import math
|
||
|
|
||
|
games = dict()
|
||
|
|
||
|
with open("input.txt", "r") as f:
|
||
|
for line in f:
|
||
|
game = list()
|
||
|
x, y = line.strip().split(":")
|
||
|
game_number = int(x.split(" ")[1])
|
||
|
sets = y.strip().split(";")
|
||
|
for s in sets:
|
||
|
ss = {}
|
||
|
grabs = s.split(", ")
|
||
|
for grab in grabs:
|
||
|
cube_count, colour = grab.strip().split(" ")
|
||
|
ss[colour] = int(cube_count)
|
||
|
game.append(ss)
|
||
|
games[game_number] = game
|
||
|
|
||
|
print(games)
|
||
|
|
||
|
query = {"red": 12, "green": 13, "blue": 14}
|
||
|
|
||
|
possible_games = []
|
||
|
for id, game in games.items():
|
||
|
possible = True
|
||
|
for s in game:
|
||
|
for colour, count in s.items():
|
||
|
max = query[colour]
|
||
|
if count > max:
|
||
|
possible = False
|
||
|
if possible:
|
||
|
possible_games.append(id)
|
||
|
|
||
|
part1 = 0
|
||
|
for id in possible_games:
|
||
|
part1 += id
|
||
|
print("part1", part1)
|
||
|
|
||
|
part2 = 0
|
||
|
for id, game in games.items():
|
||
|
min = {"red": 0, "green": 0, "blue": 0}
|
||
|
|
||
|
for s in game:
|
||
|
for colour, count in s.items():
|
||
|
if count > min[colour]:
|
||
|
min[colour] = count
|
||
|
|
||
|
print(min)
|
||
|
power = min["red"] * min["green"] * min["blue"]
|
||
|
print(power)
|
||
|
part2 += power
|
||
|
print(part2)
|