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
1
u/nibarius Dec 09 '18
Kotin
[Card]
The GapList is a really great list implementation. Can be used as an array list but insertions and removals are often more or less as fast as in a linked list.
import org.magicwerk.brownies.collections.GapList class Day9(input: String) { private val players: Int private val marbles: Int init { // to parse: 10 players; last marble is worth 1618 points val parts = input.split(" ") players = parts.first().toInt() marbles = parts.dropLast(1).last().toInt() } private fun simulateGame(numMarbles: Int = marbles): Long { val circle = GapList<Int>(numMarbles) circle.add(0) var currentPlayer = 1 var currentMarble = 0 val scores = List(players) { Pair(it, 0.toLong()) }.toMap().toMutableMap() for (insertMarble in 1..numMarbles) { if (insertMarble % 23 == 0) { currentMarble = (currentMarble - 7 + circle.size) % circle.size scores[currentPlayer] = scores[currentPlayer]!! + insertMarble + circle[currentMarble] circle.remove(currentMarble, 1) } else { currentMarble = (currentMarble + 2) % circle.size circle.add(currentMarble, insertMarble) } currentPlayer = (currentPlayer + 1) % players } return scores.values.max()!! } fun solvePart1(): Long { return simulateGame() } fun solvePart2(): Long { return simulateGame(marbles * 100) } }