day 8
This commit is contained in:
parent
2b24356087
commit
40919a1e2d
|
@ -0,0 +1,27 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("input2.txt") as f:
|
||||||
|
numbers = [int(i) for i in f.read().split()]
|
||||||
|
mdtotal = 0
|
||||||
|
|
||||||
|
def parseit(data):
|
||||||
|
nonlocal mdtotal
|
||||||
|
children = data.pop(0)
|
||||||
|
metas = data.pop(0)
|
||||||
|
|
||||||
|
while children > 0:
|
||||||
|
children -= 1
|
||||||
|
parseit(data)
|
||||||
|
|
||||||
|
while metas > 0:
|
||||||
|
metas -= 1
|
||||||
|
mdtotal += data.pop(0)
|
||||||
|
|
||||||
|
parseit(numbers)
|
||||||
|
print(mdtotal)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,44 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
|
def getnodevalue(data):
|
||||||
|
"""
|
||||||
|
Get the current node value
|
||||||
|
- parse number of child nodes
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
children_count = data.pop(0)
|
||||||
|
meta_count = data.pop(0)
|
||||||
|
|
||||||
|
if children_count == 0:
|
||||||
|
value = 0
|
||||||
|
while meta_count > 0:
|
||||||
|
meta_count -= 1
|
||||||
|
value += data.pop(0)
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
child_values = []
|
||||||
|
while children_count > 0:
|
||||||
|
children_count -= 1
|
||||||
|
child_values.append(getnodevalue(data))
|
||||||
|
value = 0
|
||||||
|
while meta_count > 0:
|
||||||
|
meta_count -= 1
|
||||||
|
entry = data.pop(0)
|
||||||
|
if entry <= len(child_values):
|
||||||
|
value += child_values[entry - 1]
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("input.txt") as f:
|
||||||
|
numbers = [int(i) for i in f.read().split()]
|
||||||
|
|
||||||
|
mdtotal = getnodevalue(numbers)
|
||||||
|
|
||||||
|
print(mdtotal)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue