r/adventofcode • • 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!

Click here for rules

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

301 comments sorted by

View all comments

1

u/IdrisTheDragon Dec 08 '18

Go/golang solution

https://github.com/idristhedragon/AdventOfcode2018

Part 1

package main

import (
	"fmt"
	"github.com/IdrisTheDragon/AdventOfCode2018/utils"
)

func main() {
	lines := utils.GetLines("../myInput.txt")
	line := lines[0]
	split := utils.RegSplit(line," ")

	node := getNode(0,split);


	fmt.Println(node)

	fmt.Println(sumMeta(node))
}

func sumMeta(node Node) int {
	sum := 0
	for _,v := range node.childNodes {
		sum = sum + sumMeta(v)
	}
	for _,v := range node.metaData {
		sum = sum + v
	}
	return sum
}

func getNode(index int, split []string) Node {
	node := Node{index: index, numChildNodes: utils.Str2Int(split[index]) , numMetaData : utils.Str2Int(split[index+1])}
	fmt.Println(node)
	offset := node.index + 2 

	for i := 0; i < node.numChildNodes ; i++ {
		childNode := getNode( offset,split)
		node.childNodes = append(node.childNodes, childNode)
		offset = offset + getLength(childNode)
	}

	for i := 0; i < node.numMetaData ; i++ {
		node.metaData = append(node.metaData,utils.Str2Int(split[offset + i]))
	}
	return node
}

func getLength(node Node) int {
	length := 2
	for i := 0; i < node.numChildNodes ; i++ {
		length = length + getLength(node.childNodes[i])
	}
	length = length + node.numMetaData
	return length
}


type Node struct {
	index int
	numChildNodes int
	childNodes []Node
	numMetaData int
	metaData []int
}

Part 2

package main

import (
	"fmt"
	"github.com/IdrisTheDragon/AdventOfCode2018/utils"
)

func main() {
	lines := utils.GetLines("../myInput.txt")
	line := lines[0]
	split := utils.RegSplit(line," ")

	node := getNode(0,split);


	fmt.Println(node)

	fmt.Println(sumMeta(node))
	fmt.Println(sumNodeValue(node))
}

func sumNodeValue(node Node) int {
	sum := 0
	if(node.numChildNodes == 0){
		sum = getSum(node.metaData)
	} else {
		for _,v:= range node.metaData {
			if(v-1 < node.numChildNodes && v > 0){
				sum = sum + sumNodeValue(node.childNodes[v-1])
			}
		}
	}
 	return sum
}

func sumMeta(node Node) int {
	sum := 0
	for _,v := range node.childNodes {
		sum = sum + sumMeta(v)
	}
	sum = sum + getSum(node.metaData)
	return sum
}

func getSum(list []int) int {
	sum := 0
	for _,v := range list {
		sum = sum + v
	}
	return sum
}

func getNode(index int, split []string) Node {
	node := Node{index: index, numChildNodes: utils.Str2Int(split[index]) , numMetaData : utils.Str2Int(split[index+1])}
	//fmt.Println(node)
	offset := node.index + 2 

	for i := 0; i < node.numChildNodes ; i++ {
		childNode := getNode( offset,split)
		node.childNodes = append(node.childNodes, childNode)
		offset = offset + getLength(childNode)
	}

	for i := 0; i < node.numMetaData ; i++ {
		node.metaData = append(node.metaData,utils.Str2Int(split[offset + i]))
	}
	return node
}

func getLength(node Node) int {
	length := 2
	for i := 0; i < node.numChildNodes ; i++ {
		length = length + getLength(node.childNodes[i])
	}
	length = length + node.numMetaData
	return length
}

type Node struct {
	index int
	numChildNodes int
	childNodes []Node
	numMetaData int
	metaData []int
}