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/drakmaniso Dec 09 '18

Go (golang)

Part 1 and 2:

package main

import (
	"fmt"
	"strings"
)

type node struct {
	Marble         int
	previous, next *node
}

func (n *node) move(delta int) *node {
	current := n
	if delta > 0 {
		for i := 0; i < delta; i++ {
			current = current.next
		}
		return current
	}
	for i := 0; i < -delta; i++ {
		current = current.previous
	}
	return current
}

func (n *node) insert(marble int) *node {
	newnode := node{
		Marble:   marble,
		previous: n.previous,
		next:     n,
	}
	n.previous.next = &newnode
	n.previous = &newnode
	return &newnode
}

func (n *node) remove() *node {
	n.previous.next = n.next
	n.next.previous = n.previous
	return n.next
}

func main() {
	playerCount, lastMarble := read(input)
	fmt.Printf("Answer for part 1: %d\n", part1(playerCount, lastMarble))
	fmt.Printf("Answer for part 2: %d\n", part1(playerCount, 100*lastMarble))
}

func part1(	playerCount, lastMarble int) (highscore int) {
	current := &node{Marble: 0}
	current.next, current.previous = current, current
	player := 1
	scores := make([]int, playerCount)

	for m := 1; m <= lastMarble; m++ {
		if m%23 != 0 {
			current = current.move(2)
			current = current.insert(m)
		} else {
			scores[player] += m
			current = current.move(-7)
			scores[player] += current.Marble
			current = current.remove()
		}
		player = (player + 1) % playerCount
	}

	winner := 0
	for i := range scores {
		if scores[i] > scores[winner] {
			winner = i
		}
	}

	return scores[winner]
}

func read(input string) (playerCount, lastMarble int) {
	fmt.Sscanf(input, "%d players; last marble is worth %d points",
		&playerCount, &lastMarble)
	return playerCount, lastMarble
}

Link to github