54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
#For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?
|
|
import re
|
|
def ParseLine( line :str) -> ( int , [int] ):
|
|
|
|
line = line[5:] # skip the first 5 characters
|
|
|
|
id = re.match(r'(\d{1,3})\:', line).groups()[0]
|
|
print(f"found game id: {id}")
|
|
|
|
revealed_cubes = re.findall(r'(?:(\d{1,2}) (blue|red|green)(\,|\;|)){1,3}', line)
|
|
|
|
print(revealed_cubes)
|
|
|
|
|
|
return (int(id), revealed_cubes )
|
|
|
|
minimum_dice = []
|
|
|
|
with open("input.txt") as file:
|
|
line = file.readline()
|
|
while line :
|
|
print(line)
|
|
game_id, reveals = ParseLine(line)
|
|
min_red = 0
|
|
min_green = 0
|
|
min_blue = 0
|
|
|
|
for reveal in reveals:
|
|
n_dice = int(reveal[0])
|
|
colour = reveal[1]
|
|
if colour == "red":
|
|
if n_dice > min_red:
|
|
min_red = n_dice
|
|
elif colour == "green":
|
|
if n_dice > min_green:
|
|
min_green = n_dice
|
|
elif colour == "blue":
|
|
if n_dice > min_blue:
|
|
min_blue = n_dice
|
|
|
|
minimum_dice.append((min_red, min_green, min_blue))
|
|
|
|
|
|
|
|
line = file.readline()
|
|
|
|
|
|
sum = 0
|
|
for set_cubes in minimum_dice:
|
|
sum += set_cubes[0] * set_cubes[1] * set_cubes[2]
|
|
|
|
print (f"Answer: {sum}")
|
|
|
|
print("DONE!") |