-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-2.py
34 lines (29 loc) · 906 Bytes
/
7-2.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
rawdata = open("7-1.input.txt").readlines()
data = []
for item in rawdata:
data.append((item))
rules={}
for rule in data:
outerbag=rule.split(' bags contain ')[0]
contents=rule.split(' bags contain ')[1].split(', ')
for innerbag in contents:
innerbag=innerbag.replace(' bags','').replace('.','').replace('\n','').replace(' bag','')
if innerbag!='no other':
num=int(innerbag[0])
innerbag=innerbag[2:]
else:
num=0
innerbag=''
if outerbag in rules:
rules[outerbag]=rules[outerbag]+[innerbag]*num
else:
rules[outerbag]=[innerbag]*num
def numinnerbags(outerbag):
if rules[outerbag]==[]:
return 0
else:
total=0
for innerbag in rules[outerbag]:
total=total+1+numinnerbags(innerbag)
return total
print(numinnerbags("shiny gold"))