-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha8.tt
46 lines (33 loc) · 821 Bytes
/
a8.tt
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
composer nodes
<node>
rule node: { (def childCount: <INT>;) (<WS>) (def metadataCount: <INT>;) (<WS>) children:[ <node>=$childCount ], metadata:[ <metadata>=$metadataCount ] } (<WS>?)
rule metadata: <INT> (<WS>?)
end nodes
def root: $IN::lines -> nodes;
//part 1
templates sum
@: 0;
$... -> @sum: $@sum + $;
$@ !
end sum
templates getMetadata
[ $ -> # ] !
<{}>
$.children... -> #
$.metadata... -> #
<>
$ !
end getMetadata
$root -> getMetadata -> sum -> !OUT::write
'
' -> !OUT::write
//part 2
templates nodeValue
<{children:<[](0)>}>
$.metadata -> sum !
<{children:<[](1..)>}>
def max: $.children::length;
def childSelector: [$.metadata... -> \(<1..$max> $!\)];
[ $.children($childSelector)... -> # ] -> sum !
end nodeValue
$root -> nodeValue -> !OUT::write