Day 8 part 1 solution.

This commit is contained in:
Anna Rose Wiggins 2018-12-10 20:23:18 -05:00
parent 31f60eca0e
commit 4d567425a7
No known key found for this signature in database
GPG key ID: 8D9ACA841015C59A
4 changed files with 108 additions and 1 deletions

View file

@ -0,0 +1,30 @@
package day08
type Node struct {
Children []*Node
Metadata []int
}
func BuildTree(data []int) *Node {
root, _ := buildTreeR(data, 0)
return root
}
func buildTreeR(data []int, index int) (*Node, int) {
node := &Node{}
numChildren := data[index]
numMetadata := data[index+1]
index += 2
for i := 0; i < numChildren; i++ {
var child *Node
child, index = buildTreeR(data, index)
node.Children = append(node.Children, child)
}
for i := 0; i < numMetadata; i++ {
node.Metadata = append(node.Metadata, data[index+i])
}
return node, index + numMetadata
}