44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
|
import time
|
||
|
|
||
|
with open("input_day2.txt") as file:
|
||
|
lines = [line.rstrip() for line in file]
|
||
|
|
||
|
class colour_thing:
|
||
|
def __init__(self,red=0,green=0,blue=0):
|
||
|
self.red = int(red)
|
||
|
self.green = int(green)
|
||
|
self.blue = int(blue)
|
||
|
def add_rgb(self,red=0,green=0,blue=0):
|
||
|
self.red += int(red)
|
||
|
self.green += int(green)
|
||
|
self.blue += int(blue)
|
||
|
sum = 0
|
||
|
game_id = 1
|
||
|
for line in lines:
|
||
|
id_valid = True
|
||
|
string = line.split(": ")[1]
|
||
|
draws = string.split("; ")
|
||
|
game = []
|
||
|
for draw in draws:
|
||
|
temp_colour_thing = colour_thing()
|
||
|
colours = draw.split(", ")
|
||
|
for colour in colours:
|
||
|
count,split_colour = colour.split(" ")
|
||
|
match split_colour:
|
||
|
case "red":
|
||
|
temp_colour_thing.add_rgb(red=count)
|
||
|
case "green":
|
||
|
temp_colour_thing.add_rgb(green=count)
|
||
|
case "blue":
|
||
|
temp_colour_thing.add_rgb(blue=count)
|
||
|
game.append(temp_colour_thing)
|
||
|
for thing in game:
|
||
|
if thing.red > 12 or thing.green > 13 or thing.blue > 14:
|
||
|
id_valid = False
|
||
|
if id_valid:
|
||
|
print(f"{game_id} is valid")
|
||
|
sum+=game_id
|
||
|
else:
|
||
|
print(f"{game_id} is not valid")
|
||
|
game_id+=1
|
||
|
print(sum)
|