-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.scala
38 lines (29 loc) · 1.17 KB
/
9.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
val numPlayers = 458
val highestMarble = 7201900
case class Turn(currentPlayer: Int, game: Seq[Int], scores: Map[Int, Long])
def shiftLeft[T](sequence: Seq[T], shift: Int): Seq[T] =
if (shift < 0) shiftRight(sequence, Math.abs(shift))
else shiftRight(sequence, sequence.size - shift)
def shiftRight[T](sequence: Seq[T], shift: Int): Seq[T] =
if (shift == 0 || sequence.isEmpty) {
sequence
} else {
val split = (sequence.size - shift) % sequence.size
sequence.drop(split) ++ sequence.take(split)
}
def printGame(turn: Turn): Unit = {
println(turn.game.mkString(", "))
}
val lastTurn = (0 to highestMarble).foldLeft(Turn(0, Vector.empty, Map().withDefaultValue(0))) {
case (Turn(currentPlayer, game, scores), marble) =>
val (newGame, newScores) = if (marble % 23 == 0 && marble > 0) {
val backSeven = shiftRight(game, 7)
val newScores = scores + (currentPlayer -> (scores(currentPlayer) + marble + backSeven.head))
(backSeven.drop(1), newScores)
} else {
val shifted = shiftLeft(game, 2)
(marble +: shifted, scores)
}
Turn((currentPlayer + 1) % numPlayers, newGame, newScores)
}
println(lastTurn.scores.values.max)