Day 8 part 1 solution.
This commit is contained in:
25
2018/internal/day08/debug.go
Normal file
25
2018/internal/day08/debug.go
Normal file
@ -0,0 +1,25 @@
|
||||
package day08
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func DebugPrintTree(root *Node) {
|
||||
debugPrintTreeR(root, 0)
|
||||
}
|
||||
|
||||
func debugPrintTreeR(node *Node, indent int) {
|
||||
for i := 0; i < indent; i++ {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
fmt.Printf("Children: %d | Metadata: ", len(node.Children))
|
||||
|
||||
for _, md := range node.Metadata {
|
||||
fmt.Printf("%d ", md)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
for _, child := range node.Children {
|
||||
debugPrintTreeR(child, indent+1)
|
||||
}
|
||||
}
|
30
2018/internal/day08/tree.go
Normal file
30
2018/internal/day08/tree.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user