-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_part1.jakt
66 lines (48 loc) · 1.32 KB
/
day8_part1.jakt
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Expect:
/// - output: "44838\n"
import utility
import reader
class Node {
public child_nodes: [Node]
public metadata: [i32]
}
function parse_node(anon node_values: [i32], mut node: Node) throws -> i32 {
let child_node_count = node_values[0]
let metadata_count = node_values[1]
mut child_nodes: [Node] = []
mut count = 0i32
mut i = 0i32
mut start = 2i32
while i < child_node_count {
mut new_node = Node(
child_nodes: [],
metadata: []
)
let add_count = parse_node(node_values[start..].to_array(), node: new_node)
node.child_nodes.push(new_node)
start += add_count
count += add_count
i++
}
node.metadata = node_values[start..start+metadata_count].to_array()
return metadata_count + count + 2
}
function count_metadata(node: Node) -> i32 {
mut count = 0i32
for child in node.child_nodes.iterator() {
count += count_metadata(node: child)
}
for data in node.metadata.iterator() {
count += data
}
return count
}
function main() {
let node_values = reader::lines_int(day: 8, separator: ' ')
mut node = Node(
child_nodes: [],
metadata: []
)
let count = parse_node(node_values, node)
println("{}", count_metadata(node))
}