mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
25 lines
696 B
Python
25 lines
696 B
Python
lines = open('input').read().replace(' bags','').replace(' bag','').replace('.','').splitlines()
|
|
|
|
empties = ['base']
|
|
dictionary = {}
|
|
for line in lines:
|
|
r = line.split(' contain ')
|
|
if 'no other' in line:
|
|
dictionary[r[0]] = {}
|
|
empties.append(r[0])
|
|
else:
|
|
dictionary[r[0]] = {y[1]: int(y[0]) for x in r[1].split(', ') if (y := x.split(' ',1))}
|
|
|
|
def f(li,count):
|
|
for u in li:
|
|
k = li[u]
|
|
if u not in empties:
|
|
tmp = {x:y*k for x in dictionary[u] if (y := dictionary[u][x])}
|
|
tmp.update({'base':k})
|
|
count = f(tmp,count)
|
|
else:
|
|
count += k
|
|
return count
|
|
|
|
print( f(dictionary['shiny gold'],0) )
|