Skip to content

Instantly share code, notes, and snippets.

@agp8x
Last active December 17, 2018 21:17
Show Gist options
  • Save agp8x/08dca03bd827add9a63f3d070305bb28 to your computer and use it in GitHub Desktop.
Save agp8x/08dca03bd827add9a63f3d070305bb28 to your computer and use it in GitHub Desktop.
aoc08
# V1
class Node:
nodes = []
def __init__(self, numbers):
#print(numbers)
if not numbers:
self.num_childs=0
self.num_meta=0
self.metadata=[]
self.children=[]
return
self.num_childs = numbers[0]
self.num_meta = numbers[1]
self.children = []
start = 2
Node.nodes.append(self)
for i in range(self.num_childs):
c = Node(numbers[start:-self.num_meta])
self.children.append(c)
start += c.len()
self.metadata = numbers[start:start + self.num_meta]
if not len(self.metadata) == self.num_meta:
print("ALERT")
def len(self):
return 2 + self.num_meta + sum([c.len() for c in self.children])
def meta_sum(self):
return sum(self.metadata) + sum([c.meta_sum() for c in self.children])
def __repr__(self):
return f"({self.num_childs}, {self.num_meta}, ({self.metadata}))"
n = Node(numbers)
print(n.meta_sum())
# V2
meta = []
def node(n):
if n[0] == 0:
end = 2 + n[1]
meta.append(n[2:end])
return end
end = 2
for i in range(n[0]):
end_i = node(n[end:])
#meta.append(n[end_i-n[3]:end_i])
end += end_i
end += n[1]
meta.append(n[end-n[1]:end])
return end
print(sum([sum(x) for x in meta]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment