68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
#The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
|
|
|
|
import re
|
|
|
|
red_cubes = 12
|
|
green_cubes = 13
|
|
blue_cubes = 14
|
|
|
|
possible_game_IDs = []
|
|
|
|
def isGamePossible(reveals ) -> bool:
|
|
for reveal in reveals :
|
|
n_dice = int(reveal[0])
|
|
colour = reveal[1]
|
|
#print(f"showed {n_dice} of the {colour} dice" )
|
|
|
|
if colour == "blue":
|
|
if n_dice <= blue_cubes:
|
|
continue
|
|
else:
|
|
return False
|
|
if colour == "green":
|
|
if n_dice <= green_cubes:
|
|
continue
|
|
else:
|
|
return False
|
|
if colour == "red":
|
|
if n_dice <= red_cubes:
|
|
continue
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
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 )
|
|
|
|
|
|
with open("input.txt") as file:
|
|
line = file.readline()
|
|
while line :
|
|
print(line)
|
|
game_id, reveals = ParseLine(line)
|
|
if isGamePossible(reveals):
|
|
possible_game_IDs.append(game_id)
|
|
else:
|
|
print("Game not possible")
|
|
line = file.readline()
|
|
|
|
print(possible_game_IDs)
|
|
|
|
sum = 0
|
|
# sum up id's to get the answer
|
|
for gameID in possible_game_IDs:
|
|
sum += gameID
|
|
print (f"Answer: {sum}")
|
|
|
|
print("DONE!") |