/
day08.jl
67 lines (48 loc) · 1.29 KB
/
day08.jl
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
67
using Test
data = map(x->parse(Int16, x), split(readline("day08.txt"), " "))
struct Node
metadata::Array{Int16}
children::Array{Node}
length::Int32
end
function parseNode(data)
childno = data[1]
metalen = data[2]
rdata = data[3: end]
childlen = 0
children = []
for i in 1:childno
child = parseNode(rdata[childlen+1:end])
push!(children, child)
childlen += child.length
end
metadata = rdata[childlen+1: childlen+metalen]
return Node(metadata, children, metalen+childlen+2)
end
function solution1(node)
res = 0
for child in node.children
res+= solution1(child)
end
res+=sum(node.metadata)
end
function solution2(node)
res = 0
if isempty(node.children)
res+= sum(node.metadata)
else
for childIdx in node.metadata
if childIdx in 1:length(node.children)
res+= solution2(node.children[childIdx])
else
res+=0
end
end
end
res
end
@test solution1(parseNode([2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2])) == 138
@test solution2(parseNode([2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2])) == 66
nodeTree = parseNode(data)
println("solution 1 ", @time solution1(nodeTree))
println("solution 2 ", @time solution2(nodeTree))