Day 9
This commit is contained in:
119
src/main/kotlin/com/basdado/adventofcode/Day9.kt
Normal file
119
src/main/kotlin/com/basdado/adventofcode/Day9.kt
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package com.basdado.adventofcode
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val day = Day9()
|
||||||
|
|
||||||
|
// val testGame = Day9.Game(9, 25)
|
||||||
|
// testGame.play()
|
||||||
|
// // Correct last line: 0 16 8 17 4 18 19 2 24 20 25 10 21 5 22 11 1 12 6 13 3 14 7 15
|
||||||
|
// println(testGame.playerScores.joinToString())
|
||||||
|
//
|
||||||
|
day.puzzle1()
|
||||||
|
// Puzzle 1 answer = 385820
|
||||||
|
day.puzzle2()
|
||||||
|
}
|
||||||
|
|
||||||
|
class Day9 {
|
||||||
|
|
||||||
|
fun puzzle1() {
|
||||||
|
val input = lines("/day/9/input.txt")
|
||||||
|
.findFirst()
|
||||||
|
.get()
|
||||||
|
val regex = Regex("([0-9]+) players; last marble is worth ([0-9]+) points")
|
||||||
|
val match = regex.matchEntire(input)!!
|
||||||
|
val playerCount = match.groupValues[1].toInt()
|
||||||
|
val marbleCount = match.groupValues[2].toInt()
|
||||||
|
|
||||||
|
val game = Game(playerCount, marbleCount)
|
||||||
|
game.play()
|
||||||
|
println(game.playerScores.max())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun puzzle2() {
|
||||||
|
val input = lines("/day/9/input.txt")
|
||||||
|
.findFirst()
|
||||||
|
.get()
|
||||||
|
val regex = Regex("([0-9]+) players; last marble is worth ([0-9]+) points")
|
||||||
|
val match = regex.matchEntire(input)!!
|
||||||
|
val playerCount = match.groupValues[1].toInt()
|
||||||
|
val marbleCount = match.groupValues[2].toInt() * 100
|
||||||
|
|
||||||
|
val game = Game(playerCount, marbleCount)
|
||||||
|
game.play()
|
||||||
|
println(game.playerScores.max())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Game(val playerCount: Int, val marbleCount: Int) {
|
||||||
|
|
||||||
|
val playerScores = LongArray(playerCount)
|
||||||
|
val marbleCircle = DoubleLinkedList(0)
|
||||||
|
|
||||||
|
fun play() {
|
||||||
|
|
||||||
|
var marble = 1
|
||||||
|
var current = marbleCircle.first
|
||||||
|
var currentPlayer = 0
|
||||||
|
while(marble <= marbleCount) {
|
||||||
|
|
||||||
|
// println("$currentPlayer ${current.value} $marbleCircle")
|
||||||
|
|
||||||
|
if (marble % 23 == 0) {
|
||||||
|
|
||||||
|
playerScores[currentPlayer] = playerScores[currentPlayer] + marble
|
||||||
|
|
||||||
|
|
||||||
|
val toRemove = current.previous.previous.previous.previous.previous.previous.previous
|
||||||
|
val prev = toRemove.previous
|
||||||
|
val next = toRemove.next
|
||||||
|
prev.next = next
|
||||||
|
next.previous = prev
|
||||||
|
|
||||||
|
playerScores[currentPlayer] = playerScores[currentPlayer] + toRemove.value
|
||||||
|
current = next
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
val newNode = DoubleLinkedList.Node(marble)
|
||||||
|
newNode.previous = current.next
|
||||||
|
newNode.next = current.next.next
|
||||||
|
|
||||||
|
newNode.previous.next = newNode
|
||||||
|
newNode.next.previous = newNode
|
||||||
|
|
||||||
|
current = newNode
|
||||||
|
}
|
||||||
|
currentPlayer = (currentPlayer + 1) % playerCount
|
||||||
|
marble++
|
||||||
|
|
||||||
|
}
|
||||||
|
// println("$currentPlayer ${current.value} $marbleCircle")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DoubleLinkedList<T>(firstEntry: T) {
|
||||||
|
|
||||||
|
class Node<T>(val value: T) {
|
||||||
|
var next: Node<T> = this
|
||||||
|
var previous: Node<T> = this
|
||||||
|
}
|
||||||
|
|
||||||
|
var first: Node<T>
|
||||||
|
|
||||||
|
init {
|
||||||
|
first = Node(firstEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
|
||||||
|
var node = first
|
||||||
|
var res = StringBuilder()
|
||||||
|
do {
|
||||||
|
res.append(node.value).append(" ")
|
||||||
|
node = node.next
|
||||||
|
} while (node != first)
|
||||||
|
return res.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/main/resources/day/9/input.txt
Normal file
1
src/main/resources/day/9/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
468 players; last marble is worth 71843 points
|
||||||
Reference in New Issue
Block a user