This commit is contained in:
Jonas Neugebauer
2020-12-13 10:59:48 +01:00
parent ea798b7313
commit 9e35a9bb7c
3 changed files with 672 additions and 0 deletions

35
2020/day7_2.py Normal file
View File

@@ -0,0 +1,35 @@
instr = '''
A contain 1 C.
B contain 1 D.
C contain no other bags.
D contain 1 E.
E contain no other bags.
F contain 1 D.
'''
input = dict()
with open('day7_input.txt', 'r') as f:
for line in f.readlines():
if len(line.strip()) > 0:
btype, content = line.strip()[:-1].split(' contain ')
btype= btype[:-1]
input[btype] = content.split(', ')
for i,b in enumerate(input[btype]):
if b == 'no other bags':
input[btype] = []
else:
num,name = b.split(' ',1)
if int(num) > 1:
name = name[0:-1]
input[btype][i] = (int(num), name)
def walk_bags(name, bags):
count = 1
if name in bags:
for child in bags[name]:
count += child[0] * walk_bags(child[1], bags)
return count
search = 'shiny gold bag'
print(walk_bags(search, input)-1)