re: AoC Day 8: Memory Maneuver VIEW POST

Here is my Golang solution for today's problem. It was more easy for me to solve today, but made me learn to use pointers in Golang to consume the input string. I don't think this is necessarily the best way to use pointers, but I liked the simplicity in this case.

package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

// A simple structure of a node.
type node struct {
children []node
}

// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}

// function to consume elements from the input string.
func consume(parseNodes *[]string) int {
i, _ := strconv.Atoi((*parseNodes)[0])
*parseNodes = (*parseNodes)[1:]
return i
}

// function to parse a node from the input string.
numChildren := consume(parseNodes)
var children []node
for c := 0; c < numChildren; c++ {
}
for m := 0; m < numMetaData; m++ {
}
// Create and return node:
}

// function to get the sum of meta data from a given node.
func getMeta(node node) int {
// Read meta data of this node:
// Run through each child and get meta data:
for _, c := range node.children {
meta = append(meta, getMeta(c))
}
var sum int
// Sum the meta data:
for _, m := range meta {
sum += m
}
return sum
}

// function to get the value of a given node.
func getValue(node node) int {
numChildren := len(node.children)
var value int
if numChildren == 0 {
// If the node has no children return just the sum of
// the meta data for thid node:
value = getMeta(node)
} else {
// If this node has children return the sum of the
// values for each child:
for _, m := range node.metaData {
m-- // for correct indexing
if m >= 0 && m < numChildren {
// Only get value if index is not out of bound
value += getValue(node.children[m])
}
}
}
return value
}

func main() {
if err != nil {
panic(err)
}
nodeString := strings.Split(data[0], " ")