Skip to content

Instantly share code, notes, and snippets.

@bartoszm
Created December 22, 2021 22:21
Show Gist options
  • Save bartoszm/98272ce520e7a910adc039a5ec99e04a to your computer and use it in GitHub Desktop.
Save bartoszm/98272ce520e7a910adc039a5ec99e04a to your computer and use it in GitHub Desktop.
Day21 Part1
package day21
interface Dice {
fun value(): Int
fun values(count: Int): Sequence<Int>
fun rollCount(): Int
}
class DeterministicDice(var value: Int = 1, val max: Int = 100): Dice {
private var rollCount = 0
override fun value(): Int {
val r = value
value = (value % max) + 1
rollCount += 1
return r
}
override fun values(count: Int): Sequence<Int> {
return (0 until count).asSequence().map { value() }
}
override fun rollCount() = rollCount
}
class Position(var start: Int, val max: Int = 10) {
var position = start - 1
fun goBy(v: Int): Int {
position = (position + v) % max
return position + 1
}
}
class Player(private val position: Position) {
var score = 0
var moves = 0
fun move(d: Dice) {
val distance = d.values(3).sum()
score += position.goBy(distance)
moves += 1
}
}
object Day21_Part1 {
fun solve(start1: Int, start2: Int): Long {
val stop = 1000
val dice = DeterministicDice()
val p1 = Player(Position(start1))
val p2 = Player(Position(start2))
while(p2.score < stop) {
p1.move(dice)
if(p1.score >= stop) {
break
}
p2.move(dice)
}
return dice.rollCount().toLong() * if(p1.score >= 1000) + p2.score
else p1.score
}
}
fun main(args: Array<String>) {
println(Day21_Part1.solve(3,7))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment