-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 2 - Cubes.py
64 lines (44 loc) · 1.36 KB
/
Day 2 - Cubes.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
def getGameIDs():
total = 0
f = open("input.txt", "r")
Lines = f.readlines()
for line in Lines:
id = int(line.split()[1][:-1]) # removes the colon from game ID
tosses = [game.strip().replace(", ", ",") for game in line.split(":")[1].split(";")]
red, blue, green = 0, 0, 0
success = True
for toss in tosses:
cubes = toss.split(",")
for cube in cubes:
count, color = int(cube.split()[0]), cube.split()[1]
if color == "red":
red = count
elif color == "blue":
blue = count
elif color == "green":
green = count
if red > 12 or blue > 14 or green > 13:
success = False
break
total += id if success else 0
return total
def getPowers():
total = 0
f = open("input.txt", "r")
Lines = f.readlines()
for line in Lines:
tosses = [game.strip().replace(", ", ",") for game in line.split(":")[1].split(";")]
red, blue, green = 0, 0, 0
for toss in tosses:
cubes = toss.split(",")
for cube in cubes:
count, color = int(cube.split()[0]), cube.split()[1]
if color == "red":
red = max(red, count)
elif color == "blue":
blue = max(blue, count)
elif color == "green":
green = max(green, count)
total += red * blue * green
return total
print(getPowers())