This commit is contained in:
Jack Bond-Preston 2018-12-08 06:19:29 +00:00
parent 4f68af7424
commit 560fe542e6
3 changed files with 66 additions and 0 deletions

2
.gitignore vendored
View File

@ -229,3 +229,5 @@ _Pvt_Extensions
*.o
*.hi
*.out
.vscode/

1
aoc-8/inputs.txt Normal file

File diff suppressed because one or more lines are too long

63
aoc-8/main.py Normal file
View File

@ -0,0 +1,63 @@
# node struct
class Node:
def __init__(self, num_children, num_metadata):
self.num_children = num_children
self.num_metadata = num_metadata
self.metadata = []
self.children = []
def full_children(self):
return len(self.children) == self.num_children
def full_metadata(self):
return len(self.metadata) == self.num_metadata
def total_metadata(self):
return sum(self.metadata) + sum([ c.total_metadata() for c in self.children ])
def value(self):
if self.num_children == 0:
return sum(self.metadata)
else:
val = 0
for index in self.metadata:
if index <= len(self.children):
val += self.children[index - 1].value()
return val
# build tree, list of ints -> root Node
def build_tree(inputs):
root = None
node_stack = list()
i = 0
while root == None:
top_node = node_stack[-1] if node_stack else None
if top_node == None or not top_node.full_children():
num_children = inputs[i]
num_metadata = inputs[i + 1]
node_stack.append(Node(num_children, num_metadata))
i += 2
else:
for j in range(top_node.num_metadata):
top_node.metadata.append(inputs[i])
i += 1
node = node_stack.pop()
if node_stack:
node_stack[-1].children.append(node)
else:
root = node
return root
root = build_tree(list(map(lambda x: int(x), open("inputs.txt").read().split(' '))))
print(f"[Part 1] Total metadata sum: { root.total_metadata() }")
print(f"[Part 2] Root node value: { root.value() } ")