/
DiracDice.kt
167 lines (130 loc) · 4.32 KB
/
DiracDice.kt
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package d21_DiracDice
import util.Input
import util.Output
import kotlin.math.max
val possibleDiracRolls = mapPossibleRolls()
fun main() {
Output.day(21, "Dirac Dice")
val startTime = Output.startTime()
// region Part 1
val startPositions = Input.parseLines(filename = "/input/d21_start_positions.txt")
.map { it.substringAfter(": ").toInt() }
val roll = mutableListOf(98, 99, 100)
val players = listOf(
PlayerDeterministic(score = 0, position = startPositions[0]),
PlayerDeterministic(score = 0, position = startPositions[1])
)
var turn = 0
var nextPlayer = 0
do {
turn += 3
roll.next(3)
players[nextPlayer].play((roll.sum()))
nextPlayer = (nextPlayer + 1) % 2
} while (players.all { it.score < 1000 })
val loserProduct = players.first { it.score < 1000 }.score * turn
Output.part(1, "Deterministic Loser Product", loserProduct)
// endregion
// region Part 2
val diracPlayers = listOf(
PlayerDirac(startPosition = startPositions[0]),
PlayerDirac(startPosition = startPositions[1])
)
diracPlayers[0].play()
diracPlayers[1].play()
println(diracPlayers[0].lossMap)
println(diracPlayers[1].winMap)
val p1Wins = diracPlayers[0].winMap.map { it.value * diracPlayers[1].lossMap[it.key - 1]!! }.sum()
val p2Wins = diracPlayers[1].winMap.map { it.value * (diracPlayers[0].lossMap[it.key] ?: 0) }.sum()
Output.part(2, "Most Possible Wins", max(p1Wins, p2Wins))
// endregion
Output.executionTime(startTime)
}
class PlayerDeterministic(
var score: Int,
var position: Int
) {
private fun move(amount: Int) {
position = (position + amount) % 10
if (position == 0) position = 10
}
private fun addScore(amount: Int) {
score += amount
}
fun play(amount: Int) {
move(amount)
addScore(position)
}
}
fun MutableList<Int>.next(increase: Int) {
for (i in indices)
this[i] = (this[i] + increase) % 100
when (val i = indexOf(0)) {
-1 -> return
else -> this[i] = 100
}
}
// key = roll sum, value = num matching rolls
fun mapPossibleRolls(): Map<Int, Int> {
val rolls = mutableListOf(mutableListOf(1, 1, 1))
while (rolls.last().sum() != 9) {
val next = rolls.last().toMutableList()
next[2] += 1
if (next[2] > 3) {
next[2] = 1
next[1] += 1
}
if (next[1] > 3) {
next[1] = 1
next[0] += 1
}
rolls.add(next)
}
return rolls.groupingBy { it.sum() }.eachCount()
}
class PlayerDirac(
val startPosition: Int,
val winMap: MutableMap<Int, Long> = mutableMapOf(), //key=turn, value = numWins
val lossMap: MutableMap<Int, Long> = mutableMapOf()
) {
private fun scoreRollPermutation(rolls: List<Int>): Int {
var position = startPosition
var score = 0
rolls.forEach { r ->
position += r
if (position > 10)
position -= 10
score += position
}
return score
}
fun play() {
var rollSets = possibleDiracRolls.keys.map { mutableListOf(it) }.toMutableSet()
var turn = 1
while (rollSets.isNotEmpty()) {
// increment turn
turn++
// to store winning roll sets for removal
val turnWinSets = mutableSetOf<MutableList<Int>>()
// next permutation
val newRolls = mutableSetOf<MutableList<Int>>()
rollSets.forEach { prev ->
possibleDiracRolls.keys.forEach { poss ->
val curRoll = (prev + poss) as MutableList
newRolls.add(curRoll)
if (scoreRollPermutation(curRoll) > 20) {
addWinLoss(winMap, turn, curRoll)
turnWinSets.add(curRoll)
} else {
addWinLoss(lossMap, turn, curRoll)
}
}
}
rollSets = (newRolls - turnWinSets) as MutableSet
}
}
private fun addWinLoss(which: MutableMap<Int, Long>, turn: Int, rolls: List<Int>) {
val n = rolls.map { possibleDiracRolls[it]!! }.reduce { acc, cur -> acc * cur }.toLong()
which.merge(turn, n) { a, b -> a + b }
}
}