2018/Day08/Day8B.py

19 lines
451 B
Python

def read_node(it):
child_count, meta_count = next(it), next(it)
if child_count == 0:
return sum(next(it) for _ in range(meta_count))
children = [read_node(it) for _ in range(child_count)]
value = 0
for _ in range(meta_count):
index = next(it)
if index > 0 and index <= len(children):
value += children[index - 1]
return value
it = (int(x) for x in input().split())
print(read_node(it))