-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaySevenB.py
38 lines (29 loc) · 1.03 KB
/
daySevenB.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
from collections import defaultdict
graph = defaultdict(list)
with open("daySeven.txt") as file:
for line in file:
line = line.strip()
line = line.replace(".", "")
line = line.split(" contain ")
for bag in line[1].split(", "):
key = line[0]
key = key.replace(" bags", "")
key = key.replace(" bag", "")
if "no" in line[1]:
graph[key].append((-1, 0))
else:
to_append = bag[2:]
num = int(bag[:1])
to_append = to_append.replace(" bags", "")
to_append = to_append.replace(" bag", "")
graph[key].append((to_append, num))
def getTotalBags(node, graph):
if node == -1:
return 0
res = 0
for child in graph[node]:
temp = child[1] + child[1]*getTotalBags(child[0], graph)
res += temp
return res
totalBags = getTotalBags("shiny gold", graph)
print(f"A single shiny gold bag must contain {totalBags} other bags")