package main import ( "fmt" "internal/day08" "internal/util" ) func main() { data := util.ReadInputInts() root := day08.BuildTree(data) fmt.Println(generateChecksum(root)) } func generateChecksum(node *day08.Node) int { // If a node has no child nodes, its value is the sum of its metadata entries. if len(node.Children) == 0 { return node.SumMetadata() } // if a node does have child nodes, the metadata entries become (1-indexed) indexes // which refer to those child nodes. // The value of the node is the sum of the values of its children. value := 0 for _, md := range node.Metadata { // Skip invalid metadata entries if md == 0 || md > len(node.Children) { continue } value += generateChecksum(node.Children[md-1]) } return value }