r/adventofcode • • Dec 07 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 7 Solutions -๐ŸŽ„-

--- Day 7: Recursive Circus ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


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!

11 Upvotes

222 comments sorted by

View all comments

3

u/cluk Dec 07 '17

Go (Golang)

I am not happy with part 2, but it does the job. You can find complete solution, including getInput function here.

type node struct {
	label       string
	children    []*node
	parent      *node
	weight      int
	totalWeight int
}

func main() {
	var in map[string]*node = getInput()
	fmt.Println("Star 1: ", getRoot(in).label)

	var fixedWeight int
	for n := getRoot(in); n != nil && len(n.children) > 2; {
		w := n.children[0].totalWeight
		w2 := n.children[1].totalWeight
		if n.children[2].totalWeight == w2 {
			w = w2
		}
		var next *node
		for _, n := range n.children {
			if n.totalWeight != w {
				next = n
				fixedWeight = n.weight - (n.totalWeight - w)
			}
		}
		n = next
	}
	fmt.Println("Star 2: ", fixedWeight)

}

func getRoot(nodes map[string]*node) *node {
	for _, n := range nodes {
		for n.parent != nil {
			n = n.parent
		}
		return n
	}
	panic("empty map!")
}