-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_07_b.py
46 lines (37 loc) · 905 Bytes
/
puzzle_07_b.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
import sys
from collections import deque
target = 'shinygold'
p1 = 0
p2 = 0
E = {}
def compress( text ):
text = text.strip()
words = text.split(' ')
out = ''
for word in words:
if 'bag' in word:
continue
out += word
return out
for line in sys.stdin:
line = line.strip()
(container,contents) = line.split(' contain ')
container = compress(container)
bags = contents.split(',')
for bag in bags:
bag = compress(bag)
if bag == 'noother':
break
if container not in E:
E[container] = []
print(container,bag)
E[container].append((int(bag[0:1]),bag[1:]))
print E
def num_bags(bag):
num = 1
for (n,x) in E.get(bag,[]):
print bag,n,x,num
num += (n * num_bags(x))
print bag,n,x,num, "=="
return num
print(num_bags(target)-1)