adventofcode2023/day02/sol.py

35 lines
849 B
Python
Raw Normal View History

2023-12-02 06:39:12 +00:00
import sys
2023-12-03 05:01:39 +00:00
def product(items):
t = 1
for x in items:
t *= x
return t
2023-12-02 06:39:12 +00:00
input = sys.stdin
total = 0
2023-12-03 05:01:39 +00:00
power = 0
2023-12-02 06:39:12 +00:00
game_num = 0
for line in input:
game, _, data = line.partition(": ")
game_num += 1
takes = data.split(";")
possible = True
2023-12-03 05:01:39 +00:00
minset = {'green':0, 'blue':0, 'red':0}
2023-12-02 06:39:12 +00:00
for t in takes:
cubes = t.split(",")
cubes = [x.split() for x in t.split(",")]
cubes = {color: int(n) for n, color in cubes}
possible = possible and \
cubes.get('green', 0) <= 13 and \
cubes.get('blue', 0) <= 14 and \
cubes.get("red", 0) <= 12
2023-12-03 05:01:39 +00:00
for color, n in cubes.items():
minset[color] = max(minset[color], n)
2023-12-02 06:39:12 +00:00
if possible:
total += game_num
2023-12-03 05:01:39 +00:00
power += product(minset.values())
2023-12-02 06:39:12 +00:00
print(total)
2023-12-03 05:01:39 +00:00
print(power)