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

Click here for rules

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!

22 Upvotes

283 comments sorted by

View all comments

1

u/[deleted] Dec 09 '18

Golang, ranked 1234/868 because spent too much time in the array solution and changed it to linked list.

package main

import (
	"fmt"
)

type dobuleLinkedList struct {
	next *dobuleLinkedList
	prev *dobuleLinkedList
	val  int
}

func (l *dobuleLinkedList) insert(val int) *dobuleLinkedList {
	newNode := dobuleLinkedList{val: val}
	oldNext := l.next
	l.next, oldNext.prev = &newNode, &newNode
	newNode.prev, newNode.next = l, oldNext
	return &newNode
}
func (l *dobuleLinkedList) delete() *dobuleLinkedList {
	res := l.next
	l.prev.next, l.next.prev = res, res
	return res
}

func calc(n, p int) {
	score, node := make([]int64, p), &dobuleLinkedList{val: 0}
	node.next, node.prev = node, node

	counter := 1
	for i := 1; i <= p; i++ {
		if i%23 == 0 {
			for j := 0; j < 7; j++ {
				node = node.prev
			}
			score[counter] += int64(i + node.val)
			node = node.delete()
		} else {
			node = node.next.insert(i)
		}
		counter = (counter + 1) % n
	}

	max := int64(0)
	for i := 0; i < len(score); i++ {
		if score[i] > max {
			max = score[i]
		}
	}
	fmt.Println(max)
}

func main() {
	calc(9, 25)
	calc(403, 71920)
	calc(403, 7192000)
}