aoc/2023/02/02.py

50 lines
1.1 KiB
Python
Raw Normal View History

2024-03-05 09:32:18 +00:00
with open("input.txt") as f:
lines = f.readlines()
def parse_game(line: str) -> tuple[int, list[dict[str, int]]]:
prefix, turns = line.strip("\n").split(": ")
game = int(prefix[5:]) # remove "Game ", yielding just the number
draw_list = []
for turn in turns.split("; "):
draws = {}
for color in turn.split(", "):
v, k = color.split(" ") # count, then color
draws[k] = int(v) # map color -> count
draw_list.append(draws)
return (game, draw_list)
part1 = 0
R = 12
G = 13
B = 14
for line in lines:
game, draw_list = parse_game(line)
possible = True
for draw in draw_list:
red = draw.get("red", 0)
green = draw.get("green", 0)
blue = draw.get("blue", 0)
if red > R or green > G or blue > B:
possible = False
if possible:
part1 += game
print(part1)
part2 = 0
for line in lines:
game, draw_list = parse_game(line)
R = 0
G = 0
B = 0
for draw in draw_list:
red = draw.get("red", 0)
green = draw.get("green", 0)
blue = draw.get("blue", 0)
R = max(red, R)
G = max(green, G)
B = max(blue, B)
power = R*G*B
part2 += power
print(part2)