From 4d567425a7011aa6cea86b04882934bdd8e8ea60 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Mon, 10 Dec 2018 20:23:18 -0500 Subject: [PATCH] Day 8 part 1 solution. --- 2018/day08-1.go | 27 +++++++++++++++++++++++++++ 2018/internal/day08/debug.go | 25 +++++++++++++++++++++++++ 2018/internal/day08/tree.go | 30 ++++++++++++++++++++++++++++++ 2018/internal/util/input.go | 27 ++++++++++++++++++++++++++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 2018/day08-1.go create mode 100644 2018/internal/day08/debug.go create mode 100644 2018/internal/day08/tree.go diff --git a/2018/day08-1.go b/2018/day08-1.go new file mode 100644 index 0000000..082a1ce --- /dev/null +++ b/2018/day08-1.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + + "internal/day08" + "internal/util" +) + +func main() { + data := util.ReadInputInts() + root := day08.BuildTree(data) + fmt.Println(sumMetadata(root)) +} + +func sumMetadata(node *day08.Node) int { + sum := 0 + for _, v := range node.Metadata { + sum += v + } + + for _, child := range node.Children { + sum += sumMetadata(child) + } + + return sum +} diff --git a/2018/internal/day08/debug.go b/2018/internal/day08/debug.go new file mode 100644 index 0000000..8c54891 --- /dev/null +++ b/2018/internal/day08/debug.go @@ -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) + } +} diff --git a/2018/internal/day08/tree.go b/2018/internal/day08/tree.go new file mode 100644 index 0000000..47da31a --- /dev/null +++ b/2018/internal/day08/tree.go @@ -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 +} diff --git a/2018/internal/util/input.go b/2018/internal/util/input.go index 530f6a2..d0fb570 100644 --- a/2018/internal/util/input.go +++ b/2018/internal/util/input.go @@ -5,6 +5,8 @@ import ( "bytes" "io/ioutil" "os" + "strconv" + "strings" ) // ReadInput isn't here to make friends. It is highly specific to this domain. @@ -25,7 +27,7 @@ func ReadInput() []string { return lines } -// ReadInputS is like ReadInput, but returns a byte array. +// ReadInputBytes is like ReadInput, but returns a byte array. func ReadInputBytes() []byte { rawData, err := ioutil.ReadFile(os.Args[1]) @@ -35,3 +37,26 @@ func ReadInputBytes() []byte { return bytes.TrimRight(rawData, "\n") } + +// ReadInputInts is like ReadInput, but returns an array of ints. (space-separated in the input) +func ReadInputInts() []int { + rawData, err := ioutil.ReadFile(os.Args[1]) + + if err != nil { + panic(err) + } + + strData := strings.Split(strings.TrimSpace(string(rawData)), " ") + data := []int{} + for _, newIntStr := range strData { + newInt, err := strconv.Atoi(newIntStr) + + if err != nil { + panic(err) + } + + data = append(data, newInt) + } + + return data +}