-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.py
54 lines (43 loc) · 1.39 KB
/
day02.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
def get_games(task_input):
lines = task_input.split('\n')
shown = []
for line in lines:
info = line.split(': ')
game_no = int(info[0][info[0].index(' ')+1:])
games = []
game_info = info[1].split('; ')
for g in game_info:
dice = {'r': 0, 'g': 0, 'b': 0}
colors = g.split(', ')
for c in colors:
dice_info = c.split(' ')
dice_cnt = int(dice_info[0])
dice_color = dice_info[1][0]
dice[dice_color] = dice_cnt
games.append(dice)
shown.append((game_no, games))
return shown
def part1(task_input):
games = get_games(task_input)
R, G, B = 12, 13, 14
possible_sum = 0
for g in games:
game_no = g[0]
rolls = g[1]
for roll in rolls:
if roll['r'] > R or roll['g'] > G or roll['b'] > B:
break
else: # We never hit break, so all rolls in this game were possible
possible_sum += game_no
return possible_sum
def part2(task_input):
games = get_games(task_input)
power_sum = 0
for g in games:
min_r, min_g, min_b = 0, 0, 0
for roll in g[1]:
min_r = max(min_r, roll['r'])
min_g = max(min_g, roll['g'])
min_b = max(min_b, roll['b'])
power_sum += min_r * min_g * min_b
return power_sum