62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
from utils import load_data
|
|
|
|
def question_1(data: list[str]):
|
|
|
|
color_limits = {"red": 12, "blue": 14, "green": 13}
|
|
|
|
sum = 0
|
|
for line in data:
|
|
|
|
colon_split = line.split(":")
|
|
game_id = colon_split[0].split(" ")[-1]
|
|
|
|
cubes = colon_split[1].replace(";", ",").split(",")
|
|
|
|
possible = True
|
|
for cube in cubes:
|
|
space_split = cube[1:].split(" ")
|
|
colour = space_split[1]
|
|
amount = space_split[0]
|
|
if int(amount) > color_limits[colour]:
|
|
possible = False
|
|
if possible:
|
|
sum += int(game_id)
|
|
|
|
|
|
print(f"Answer to question 1 is: {sum}")
|
|
|
|
def question_2(data: list[str]):
|
|
|
|
sum = 0
|
|
for line in data:
|
|
cubes = line.split(":")[1].replace(";", ",").split(",")
|
|
|
|
max_red = 0
|
|
max_green = 0
|
|
max_blue = 0
|
|
for cube in cubes:
|
|
space_split = cube[1:].split(" ")
|
|
colour = space_split[1]
|
|
amount = int(space_split[0])
|
|
|
|
match colour:
|
|
case "red":
|
|
if amount > max_red:
|
|
max_red = amount
|
|
case "blue":
|
|
if amount > max_blue:
|
|
max_blue = amount
|
|
case "green":
|
|
if amount > max_green:
|
|
max_green = amount
|
|
|
|
power = max_red * max_green * max_blue
|
|
sum += power
|
|
|
|
print(f"Answer to question 2 is: {sum}")
|
|
|
|
if __name__ == "__main__":
|
|
filepath = "Data/day2.txt"
|
|
data = load_data(filepath)
|
|
# question_1(data)
|
|
question_2(data) |