day 2 python part 1
parent
cd9d30fb33
commit
55014d961a
|
|
@ -0,0 +1,52 @@
|
|||
#Import the module that give us access to work with files. Allows us to manage the file-related input.
|
||||
from io import open
|
||||
|
||||
# We open the txt document containing the games
|
||||
input=open('input.txt','r')
|
||||
|
||||
# We read the text inside distinguishing by lines
|
||||
text=input.readlines()
|
||||
|
||||
# Close the txt file to be able to work on the text without having the file open.
|
||||
input.close()
|
||||
del(input)
|
||||
|
||||
bag={'red':12,'green':13,'blue':14}
|
||||
data=[]
|
||||
|
||||
# Separamos el ID del juego con las combinaciones posibles
|
||||
for line in text:
|
||||
secuencias=line.strip().replace("Game ","").split(":")
|
||||
id=int(secuencias[0])
|
||||
game=secuencias[1]
|
||||
plays=game.split(";")
|
||||
rounds=[]
|
||||
for intento in plays:
|
||||
cubes=intento.split(",")
|
||||
colores={'red':0,'green':0,'blue':0}
|
||||
for elemento in cubes:
|
||||
split_element=elemento.split(" ")
|
||||
del(split_element[0])
|
||||
numero=int(split_element[0])
|
||||
color=split_element[1]
|
||||
colores[color]=numero
|
||||
rounds.append(colores)
|
||||
|
||||
data.append([id,rounds])
|
||||
|
||||
|
||||
count=0
|
||||
|
||||
for game in data:
|
||||
id=game[0]
|
||||
rounds=game[1]
|
||||
is_valid=True
|
||||
for round in rounds:
|
||||
if round['red']<=bag['red'] and round['green']<=bag['green'] and round['blue']<=bag['blue']:
|
||||
is_valid=True
|
||||
else:
|
||||
is_valid=False
|
||||
break
|
||||
if is_valid: count+=id
|
||||
|
||||
print(count)
|
||||
Loading…
Reference in New Issue