-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathd8.py
More file actions
43 lines (30 loc) · 1.06 KB
/
d8.py
File metadata and controls
43 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from collections import Counter
with open('input') as f:
nums = list(map(int, f.readline().strip().split(' ')))
class Node:
def __init__(self, children, metadata):
self.children = children
self.metadata = metadata
def build_tree(tree, start=0):
nchildren = tree[start+0]
nmetadata = tree[start+1]
offset = start + 2
children = []
for _ in range(nchildren):
child, offset = build_tree(tree, start=offset)
children.append(child)
return Node(children, tree[offset:offset+nmetadata]), offset+nmetadata
def calc_sum(tree):
return sum(tree.metadata) + sum(calc_sum(node) for node in tree.children)
def calc_value(tree):
nchildren = len(tree.children)
if nchildren == 0:
return sum(tree.metadata)
index_allowed_values = set(range(1, nchildren+1))
indexes = Counter([i for i in tree.metadata if i in index_allowed_values])
return sum(calc_value(tree.children[i-1])*times for i, times in indexes.items())
root = build_tree(nums)[0]
# p1
print(calc_sum(root))
# p2
print(calc_value(root))