-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-2.py
executable file
·47 lines (36 loc) · 1.05 KB
/
02-2.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
#!/usr/bin/env python
import re
from collections import deque
lines = [row.strip() for row in open('02.input').readlines()]
line_format = re.compile("^Game (?P<num>[0-9]+): (?P<reveals>.*)$")
games = {}
for line in lines:
m = re.match(line_format, line)
games[int(m['num'])] = []
reveals = m['reveals'].split(';')
for reveal in reveals:
colours = {
'red': 0,
'green': 0,
'blue': 0
}
tokens = deque(reveal.replace(',', '').split())
while len(tokens) > 0:
num = int(tokens.popleft())
colour = tokens.popleft()
colours[colour] = num
games[int(m['num'])].append(colours)
def power(game):
required = {
'red': 0,
'green': 0,
'blue': 0
}
for reveal in game:
for colour in ['red', 'green', 'blue']:
required[colour] = max(required[colour], reveal[colour])
return required['red'] * required['green'] * required['blue']
sum = 0
for game in games.values():
sum += power(game)
print(sum)