Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc951c4

Browse files
committed
Day eight done. Recursion is rewarding when you figure it out.
1 parent 2f9287e commit dc951c4

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

‎2018/day08/eight.go‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type Node struct {
11+
id int
12+
children []*Node
13+
metadata []int
14+
value int
15+
}
16+
17+
func main() {
18+
f, _ := os.ReadFile("input.txt")
19+
numbers := toInt(strings.Fields(strings.TrimSpace(string(f))))
20+
root := Node{0, []*Node{}, []int{}, 0}
21+
createTree(&root, 0, 0, numbers)
22+
// printTree(&root) // Debug purposes.
23+
fmt.Printf("Part 1 | Sum of the metadata: %d\n", addMetadata(&root))
24+
fmt.Printf("Part 2 | Value of the root node: %d\n", addMetadataWithChildren(&root))
25+
}
26+
27+
func addMetadata(root *Node) int {
28+
s := 0
29+
if root != nil {
30+
s += root.value
31+
for _, children := range root.children {
32+
s += addMetadata(children)
33+
}
34+
}
35+
return s
36+
}
37+
38+
func addMetadataWithChildren(root *Node) int {
39+
s := 0
40+
if root != nil {
41+
if len(root.children) > 0 {
42+
for _, childNumber := range root.metadata {
43+
if childNumber >= 1 && childNumber <= len(root.children) {
44+
s += addMetadataWithChildren(root.children[childNumber-1])
45+
}
46+
}
47+
} else {
48+
s = root.value
49+
}
50+
}
51+
return s
52+
}
53+
54+
func createTree(root *Node, index int, id int, numbers []int) (*Node, int) {
55+
if index >= len(numbers) {
56+
return nil, 0
57+
}
58+
root.id = id
59+
children := numbers[index]
60+
metadata := numbers[index+1]
61+
index += 2
62+
// Index will always point to the number of children.
63+
for i := range children {
64+
child, j := createTree(&Node{}, index, id+1+i, numbers)
65+
root.children = append(root.children, child)
66+
index = j
67+
}
68+
sum := 0
69+
for range metadata {
70+
sum += numbers[index]
71+
root.metadata = append(root.metadata, numbers[index])
72+
index++
73+
}
74+
root.value = sum // This helps in readability to sum the metadata.
75+
return root, index
76+
}
77+
78+
func printTree(root *Node) {
79+
if root != nil {
80+
fmt.Printf("Node: +%v\n", root)
81+
for _, node := range root.children {
82+
printTree(node)
83+
}
84+
}
85+
}
86+
87+
func toInt(numbers []string) []int {
88+
n := make([]int, len(numbers))
89+
for i, number := range numbers {
90+
val, _ := strconv.Atoi(number)
91+
n[i] = val
92+
}
93+
return n
94+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /