### re: AoC Day 8: Memory Maneuver VIEW POST

I liked this challenge a lot! It felt a lot more straightforward than the other problems.

The code came out extremely succinct, too, which is always nice. IMO it's quite readable:

Part 1:

``````function sum_metadata(input)
nums = split(input, " ")
result = total_for_node(nums)

return result
end

function total_for_node(nums)
num_children = parse(Int, popfirst!(nums))
num_metadata = parse(Int, popfirst!(nums))
total = 0
for i in 1:num_children
total += total_for_node(nums)
end
for i in 1:num_metadata
meta = parse(Int, popfirst!(nums))
total += meta
end
end
``````

Part 2 looks almost identical to part 1, but with an extra branch in the middle.

``````function sum_child(input)
nums = split(input, " ")
result = total_for_node(nums)[1]

return result
end

function total_for_node(nums)
num_children = parse(Int, popfirst!(nums))
num_metadata = parse(Int, popfirst!(nums))
all_childs = []
for i in 1:num_children
append!(all_childs, total_for_node(nums))
end
total = 0
if length(all_childs) == 0
for i in 1:num_metadata
meta = parse(Int, popfirst!(nums))
total += meta
end
else
for i in 1:num_metadata
meta = parse(Int, popfirst!(nums))
if meta <= length(all_childs)
total += all_childs[meta]
end
end
end
return [total]
end
``````
code of conduct - report abuse