-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·52 lines (45 loc) · 1.39 KB
/
part_b.py
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
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import utils
from year_2018.day_08 import part_a
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
20839
"""
return NodeExtended.from_nodes_text(_input).get_value()
class NodeExtended(part_a.Node):
def get_value(self):
"""
>>> NodeExtended\\
... .from_nodes_text("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2")\\
... .get_value()
66
>>> NodeExtended((), ()).get_value()
0
>>> NodeExtended((), (1, 2, 3)).get_value()
6
>>> NodeExtended((NodeExtended((), (1, 2, 3)),), (1,)).get_value()
6
>>> NodeExtended((NodeExtended((), (1, 2, 3)),), (1, 1)).get_value()
12
>>> NodeExtended((NodeExtended((), (1, 2, 3)),), (2, 1, 2)).get_value()
6
"""
if self.children:
children_values = {
index: (
self.children[index - 1].get_value()
if index - 1 < len(self.children) else
0
)
for index in set(self.metadata)
}
return sum(
children_values[index]
for index in self.metadata
)
else:
return self.metadata_sum()
Challenge.main()
challenge = Challenge()