-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_07_a.py
49 lines (41 loc) · 970 Bytes
/
puzzle_07_a.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
import sys
from collections import deque
target = 'shinygold'
E = {}
def compress( text ):
text = text.strip()
words = text.split(' ')
out = ''
for word in words:
while any([word.startswith(d) for d in '0123456789']):
word = word[1:]
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 bag not in E:
E[bag] = []
print(container,bag)
E[bag].append(container)
SEEN = set()
Q = deque([target])
while Q:
print Q
x = Q.popleft()
print(x)
if x in SEEN:
continue
SEEN.add(x)
print "Seen", SEEN
for y in E.get(x,[]):
Q.append(y)
print(len(SEEN)-1)