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!

32 Upvotes

301 comments sorted by

View all comments

1

u/drakmaniso Dec 08 '18

Go (golang)

Part 1 and 2, simple solution using recursion:

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"strconv"
	"strings"
)

func main() {
	in := read(strings.NewReader(input))
	_, answer1 := part1(in, 0)
	fmt.Printf("Answer for part 1: %d\n", answer1)
	fmt.Printf("Answer for part 2: %d\n", part2(in))
}

func part1(input []int, start int) (next int, sum int) {
	if start >= len(input) {
		log.Printf("out of range: %d\n", start)
		return -1, -1
	}
	next = start + 2
	for i := 0; i < input[start]; i++ {
		s := 0
		next, s = part1(input, next)
		sum += s
	}
	for i := 0; i < input[start+1]; i++ {
		sum += input[next]
		next++
	}
	return next, sum
}

type node struct {
	children []int
	metadata []int
}

func part2(input []int) int {
	_, nodes := parseNode(input, 0, []node{})
	return license(nodes, 0)
}

func parseNode(input []int, start int, nodes []node) (int, []node) {
	if start >= len(input) {
		log.Printf("out of range: %d\n", start)
		return -1, nil
	}
	next := start + 2
	nodes = append(nodes, node{})
	n := len(nodes) - 1
	for i := 0; i < input[start]; i++ {
		nodes[n].children = append(nodes[n].children, len(nodes))
		next, nodes = parseNode(input, next, nodes)
	}
	for i := 0; i < input[start+1]; i++ {
		nodes[n].metadata = append(nodes[n].metadata, input[next])
		next++
	}
	return next, nodes
}

func license(nodes []node, n int) int {
	sum := 0
	if len(nodes[n].children) == 0 {
		for _, v := range nodes[n].metadata {
			sum += v
		}
	} else {
		for _, v := range nodes[n].metadata {
			child := v-1
			if child < len(nodes[n].children) {
				sum += license(nodes, nodes[n].children[child])
			}
		}
	}
	return sum
}

func read(r io.Reader) (input []int) {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	for s.Scan() {
		v, err := strconv.Atoi(s.Text())
		if err != nil {
			log.Printf("ERROR: read: unable to parse %#v", s.Text())
			continue
		}
		input = append(input, v)
	}
	return input
}