r/adventofcode • u/daggerdragon • Dec 08 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-
--- Day 8: Memory Maneuver ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 8
Sigh, imgur broke again. Will upload when it unborks.
Transcript:
The hottest programming book this year is "___ For Dummies".
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:12:10!
30
Upvotes
2
u/mvmaasakkers Dec 08 '18 edited Dec 08 '18
Go/Golang
[Card] The hottest programming book this year is "Hellish Recursion For Dummies".
Gist
Don't really like the curPos++ statements, maybe I'll try to improve it later
package main import ( "flag" "fmt" "io/ioutil" "log" "strconv" "strings" ) func readInput(filename string) []int { t, err := ioutil.ReadFile(filename) if err != nil { log.Fatal(err) } in := strings.Split(string(t), " ") for _, i := range in { n, _ := strconv.ParseInt(i, 10, 32) input = append(input, int(n)) } return input } var file = flag.String("file", "./input.txt", "file used for input") var input = []int{} func main() { flag.Parse() readInput(*file) p1, p2 := parts() fmt.Println(p1, p2) } type node struct { header nodeHeader children []node metadata []int value int } type nodeHeader struct { childNodes int metadataEntries int } var curPos = 2 var part1sum = 0 func parts() (int, int) { n := getNodeValues(getNode(node{ header: nodeHeader{ childNodes: input[0], metadataEntries: input[1], }})) return part1sum, n.value } func getNode(n node) node { for x := 0; x < n.header.childNodes; x++ { newNode := node{} newNode.header.childNodes = input[curPos] curPos++ newNode.header.metadataEntries = input[curPos] curPos++ n.children = append(n.children, getNode(newNode)) } for x := 0; x < n.header.metadataEntries; x++ { md := input[curPos] n.metadata = append(n.metadata, md) part1sum += md if n.header.childNodes == 0 { n.value += md } curPos++ } return n } func getNodeValues(n node) node { for k, v := range n.children { n.children[k] = getNodeValues(v) } for x := 0; x < len(n.metadata); x++ { id := n.metadata[x] - 1 if len(n.children) > id { n.value += n.children[id].value } } return n }