-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.py
37 lines (28 loc) · 981 Bytes
/
07.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
import collections
with open ('input7.txt','r' ) as reader:
bagInfo = [l.replace('contain ', ':') for l in reader.read().split('\n') ]
setChildBag = collections.defaultdict(set)
setMasterBag= collections.defaultdict(list)
contains_gold = set()
for i in bagInfo:
masterBag = i[0:i.find(":")-6]
childBag = i[i.find(":")+1:]
childBag = childBag.split(", ")
setMasterBag[masterBag]=[]
for bags in childBag:
if bags[0:1].isnumeric():
setMasterBag[masterBag].append((int(bags[0:1]),bags[2:bags.find("bag")-1]))
setChildBag[bags[2:bags.find(" bag")]].add(masterBag)
def getGoldBag(gold):
for gold in setChildBag[gold]:
contains_gold.add(gold)
getGoldBag(gold)
sg = "shiny gold"
getGoldBag(sg)
print(len(contains_gold))
def bagCount(color):
total=0
for value,inner in setMasterBag[color]:
total += value * (1+bagCount(inner))
return total
print(bagCount("shiny gold"))