AdventOfCode2023/day2-2.py

49 lines
1.4 KiB
Python
Raw Normal View History

2023-12-02 16:50:31 -06:00
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)
min_red = 0
min_green = 0
min_blue = 0
for thing in game:
if thing.red > min_red:
min_red = thing.red
if thing.green > min_green:
min_green = thing.green
if thing.blue > min_blue:
min_blue = thing.blue
power = min_red*min_green*min_blue
sum+=power
game_id+=1
print(sum)