Skip to content

Commit

Permalink
Solve Day21
Browse files Browse the repository at this point in the history
  • Loading branch information
ArpitShukIa committed Dec 21, 2021
1 parent 3c95268 commit d582f7b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Welcome to my Advent of Code[^aoc] Kotlin project. Here I will be sharing my sol
| 18 | [Snailfish](src/Day18.kt) | 🌟 | 🌟 |
| 19 | [Beacon Scanner](src/Day19.kt) | 🌟 | 🌟 |
| 20 | [Trench Map](src/Day20.kt) | 🌟 | 🌟 |
| 21 | [Dirac Dice](src/Day21.kt) | 🌟 | 🌟 |


[^aoc]: [Advent of Code](https://adventofcode.com) – An annual event of Christmas-oriented programming challenges started December 2015.
Expand Down
44 changes: 44 additions & 0 deletions src/Day21.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
fun main() {

fun part1(input: List<String>): Int {
val pos = Array(2) { input[it].substringAfterLast(" ").toInt() }
val score = Array(2) { 0 }
var (rolls, dice, i) = Triple(0, 1, 0)
fun roll() = dice.also { dice = dice % 100 + 1; rolls++ }
while (true) {
pos[i] = (pos[i] + roll() + roll() + roll() - 1) % 10 + 1
score[i] += pos[i]
if (score[i] >= 1000) return score[1 - i] * rolls
i = 1 - i
}
}

fun part2(input: List<String>): Long {
data class Universe(val p1: Int, val p2: Int, val s1: Int, val s2: Int)

val pos = Array(2) { input[it].substringAfterLast(" ").toInt() }
val dp = mutableMapOf<Universe, Pair<Long, Long>>()
fun solve(u: Universe): Pair<Long, Long> {
dp[u]?.let { return it }
if (u.s1 >= 21) return 1L to 0L
if (u.s2 >= 21) return 0L to 1L
var ans = 0L to 0L
for (d1 in 1..3) for (d2 in 1..3) for (d3 in 1..3) {
val newP1 = (u.p1 + d1 + d2 + d3 - 1) % 10 + 1
val newS1 = u.s1 + newP1
val (x, y) = solve(Universe(u.p2, newP1, u.s2, newS1))
ans = ans.first + y to ans.second + x
}
return ans.also { dp[u] = it }
}
return solve(Universe(pos[0], pos[1], 0, 0)).let { maxOf(it.first, it.second) }
}

val testInput = readInput("Day21_test")
checkEquals(part1(testInput), 739785)
checkEquals(part2(testInput), 444356092776315)

val input = readInput("Day21")
println(part1(input))
println(part2(input))
}

0 comments on commit d582f7b

Please sign in to comment.