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)returnresultendfunction total_for_node(nums)num_children=parse(Int,popfirst!(nums))num_metadata=parse(Int,popfirst!(nums))total=0foriin1:num_childrentotal+=total_for_node(nums)endforiin1:num_metadatameta=parse(Int,popfirst!(nums))total+=metaendreturntotalend
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]returnresultendfunction total_for_node(nums)num_children=parse(Int,popfirst!(nums))num_metadata=parse(Int,popfirst!(nums))all_childs=[]foriin1:num_childrenappend!(all_childs,total_for_node(nums))endtotal=0iflength(all_childs)==0foriin1:num_metadatameta=parse(Int,popfirst!(nums))total+=metaendelseforiin1:num_metadatameta=parse(Int,popfirst!(nums))ifmeta<=length(all_childs)total+=all_childs[meta]endendendreturn[total]end
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
Part 2 looks almost identical to part 1, but with an extra branch in the middle.