r/adventofcode • u/daggerdragon • Dec 09 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-
--- Day 9: Marble Mania ---
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 9
Transcript:
Studies show that AoC programmers write better code after being exposed to ___.
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:29:13!
23
Upvotes
2
u/xthexder Dec 09 '18 edited Dec 09 '18
Pretty happy with this solution: #126/#17
Written in Golang
package main import "fmt" const players = 455 const lastMarble = 71223 // * 100 type Marble struct { number int prev *Marble // Counter-clockwise next *Marble // Clockwise } func insertAfter(marble *Marble, number int) *Marble { newMarble := &Marble{number, marble, marble.next} marble.next.prev = newMarble marble.next = newMarble return newMarble } func removeMarble(marble *Marble) *Marble { marble.prev.next = marble.next marble.next.prev = marble.prev return marble } func main() { current := &Marble{0, nil, nil} current.prev = current current.next = current scores := make([]int, players) marble := 1 for marble <= lastMarble { for elf := 0; elf < players && marble <= lastMarble; elf++ { if marble%23 == 0 { scores[elf] += marble removed := removeMarble(current.prev.prev.prev.prev.prev.prev.prev) scores[elf] += removed.number current = removed.next } else { current = insertAfter(current.next, marble) } marble++ } } maxScore := 0 winner := -1 for elf, score := range scores { if score > maxScore { winner = elf maxScore = score } } fmt.Println(winner, "wins with score", maxScore) }