-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
89 lines (63 loc) · 1.64 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from common import *
def onlyDigits(string):
return "".join(c for c in string if c.isdigit())
def onlyAlpha(string):
return "".join(c for c in string if c.isalpha())
def part1():
lines = getDayInput(2, 1)
# amounts
RED = 12
GREEN = 13
BLUE = 14
possibleGameIDs = []
for game in lines:
splitLine = game.split(":")
gameID = onlyDigits(splitLine[0])
gameID = int(gameID)
possible = True
sets = splitLine[1]
plays = sets.split(";")
for play in plays:
bags = play.split(",")
for bag in bags:
colour = onlyAlpha(bag)
amount = int(onlyDigits(bag))
if colour == "red" and amount > RED:
possible = False
elif colour == "green" and amount > GREEN:
possible = False
elif colour == "blue" and amount > BLUE:
possible = False
else:
pass # possible!
if possible:
possibleGameIDs.append(gameID)
return sum(possibleGameIDs)
def part2():
lines = getDayInput(2, 2)
gamePowers = []
for game in lines:
splitLine = game.split(":")
gameID = onlyDigits(splitLine[0])
gameID = int(gameID)
required = {
"red": 0,
"green": 0,
"blue": 0
}
sets = splitLine[1]
plays = sets.split(";")
for play in plays:
bags = play.split(",")
for bag in bags:
colour = onlyAlpha(bag)
amount = int(onlyDigits(bag))
if required[colour] >= amount:
pass # we have enough of this colour for this play
else:
required[colour] = amount # we need more of colour for the play!
gamePower = required["red"] * required["green"] * required["blue"]
gamePowers.append(gamePower)
return sum(gamePowers)
print(f"Part 1: {part1()}")
print(f"Part 2: {part2()}")