diff --git a/python/Day 2/main.py b/python/Day 2/main.py new file mode 100644 index 0000000..960684c --- /dev/null +++ b/python/Day 2/main.py @@ -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) \ No newline at end of file