-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday02.py
44 lines (33 loc) · 951 Bytes
/
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
from benchy import minibench
def maxballs(line):
(_, rest) = line.split(": ")
games = rest.split("; ")
mdict = {}
mdict["red"] = mdict["green"] = mdict["blue"] = 0
for game in games:
game = game.replace(",", "")
toks = game.split(" ")
for i in range(len(toks) // 2):
cnt = int(toks[i * 2])
col = toks[i * 2 + 1]
mdict[col] = max(mdict[col], cnt)
return mdict
lines = open("day02.txt").read().split("\n")
def part1():
id = 1
sum = 0
for line in lines:
mdict = maxballs(line)
if mdict["red"] <= 12 and mdict["green"] <= 13 and mdict["blue"] <= 14:
sum += id
id += 1
return sum
def part2():
sum = 0
for line in lines:
mdict = maxballs(line)
sum += mdict["red"] * mdict["green"] * mdict["blue"]
return sum
print(part1())
print(part2())
minibench({"part1": part1, "part2": part2})