-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
56 lines (41 loc) · 1.04 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import inspect
import math
import os
cwd = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
games = []
with open(os.path.join(cwd, "example.txt"), "r") as f:
for line in f.read().splitlines():
game = line.split(": ")[1]
moves = [
[pair.split(" ") for pair in move.split(", ")] for move in game.split("; ")
]
games.append(moves)
# part 1
res = 0
for game_id, game in enumerate(games):
for moves in game:
cube_count = {
"red": 12,
"green": 13,
"blue": 14,
}
for amount, cube in moves:
cube_count[cube] -= int(amount)
if any(count < 0 for count in cube_count.values()):
break
else:
res += game_id
print(res)
# part 2
res = 0
for game in games:
cube_max = {
"red": 0,
"green": 0,
"blue": 0,
}
for moves in game:
for amount, cube in moves:
cube_max[cube] = max(cube_max[cube], int(amount))
res += math.prod(cube_max.values())
print(res)