Day 08
parent
dc0d6a9490
commit
0838270e6d
@ -0,0 +1,48 @@
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
|
||||
# sys.setrecursionlimit(6500)
|
||||
|
||||
with open('input.txt', 'r') as f:
|
||||
numbers = [int(x) for x in f.read().split()]
|
||||
|
||||
# print(numbers)
|
||||
|
||||
class Node:
|
||||
def __init__(self, header):
|
||||
self.header = header
|
||||
self.children = []
|
||||
self.meta = []
|
||||
|
||||
def add_child(self, child):
|
||||
self.children.append(child)
|
||||
|
||||
def add_meta(self, data):
|
||||
self.meta.append(data)
|
||||
|
||||
def meta_sum(self):
|
||||
return sum([x.meta_sum() for x in self.children]) + sum(self.meta)
|
||||
|
||||
def get_node(i):
|
||||
num_childs = numbers[i]
|
||||
num_meta = numbers[i + 1]
|
||||
header = (num_childs, num_meta)
|
||||
|
||||
node = Node(header)
|
||||
size = 2
|
||||
for _ in range(num_childs):
|
||||
child, ch_size = get_node(i + size)
|
||||
node.add_child(child)
|
||||
size += ch_size
|
||||
|
||||
for _ in range(num_meta):
|
||||
node.add_meta(numbers[i + size])
|
||||
size += 1
|
||||
|
||||
return node, size
|
||||
|
||||
# print(len(numbers))
|
||||
root = get_node(0)[0]
|
||||
|
||||
print(root.meta_sum())
|
||||
|
@ -0,0 +1,56 @@
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
|
||||
# sys.setrecursionlimit(6500)
|
||||
|
||||
with open('input.txt', 'r') as f:
|
||||
numbers = [int(x) for x in f.read().split()]
|
||||
|
||||
# print(numbers)
|
||||
|
||||
class Node:
|
||||
def __init__(self, header):
|
||||
self.header = header
|
||||
self.children = []
|
||||
self.meta = []
|
||||
|
||||
def add_child(self, child):
|
||||
self.children.append(child)
|
||||
|
||||
def add_meta(self, data):
|
||||
self.meta.append(data)
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
if len(self.children) == 0:
|
||||
return sum(self.meta)
|
||||
|
||||
val = 0
|
||||
for ref in self.meta:
|
||||
if ref > 0 and ref <= len(self.children):
|
||||
val += self.children[ref - 1].value
|
||||
return val
|
||||
|
||||
def get_node(i):
|
||||
num_childs = numbers[i]
|
||||
num_meta = numbers[i + 1]
|
||||
header = (num_childs, num_meta)
|
||||
|
||||
node = Node(header)
|
||||
size = 2
|
||||
for _ in range(num_childs):
|
||||
child, ch_size = get_node(i + size)
|
||||
node.add_child(child)
|
||||
size += ch_size
|
||||
|
||||
for _ in range(num_meta):
|
||||
node.add_meta(numbers[i + size])
|
||||
size += 1
|
||||
|
||||
return node, size
|
||||
|
||||
# print(len(numbers))
|
||||
root = get_node(0)[0]
|
||||
|
||||
print(root.value)
|
||||
|
@ -0,0 +1 @@
|
||||
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,2 @@
|
||||
01: 40309
|
||||
02: 28779
|
Loading…
Reference in New Issue