This commit is contained in:
dave 2018-12-07 22:05:25 -08:00
parent 2b24356087
commit 40919a1e2d
3 changed files with 72 additions and 0 deletions

27
8/a.py Executable file
View File

@ -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()

44
8/b.py Executable file
View File

@ -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()

1
8/input.txt Normal file

File diff suppressed because one or more lines are too long