-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.kt
56 lines (44 loc) · 1.84 KB
/
Day11.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
package aoc.years.year2024
import aoc.Day
import java.math.BigInteger
@Year2024
class Day11 : Day() {
override fun solvePart1(input: List<String>): Any {
val memoization = HashMap<String, BigInteger>()
return getStones(input).sumOf { it.getNrOfStonesAfter(25, memoization) }
}
override fun solvePart2(input: List<String>): Any {
val memoization = HashMap<String, BigInteger>()
return getStones(input).sumOf { it.getNrOfStonesAfter(75, memoization) }
}
private fun getStones(input: List<String>) = input.first().split(" ").map { it.toBigInteger() }
}
private fun BigInteger.getNrOfStonesAfter(blinks: Int, memoization: MutableMap<String, BigInteger>): BigInteger {
if (blinks == 0) {
return BigInteger.ONE
}
val memoizationKey = "$this $blinks"
if (memoization.containsKey(memoizationKey)) {
return memoization[memoizationKey]!!
}
val nrOfStones: BigInteger = getNrOfStones(blinks, memoization)
memoization[memoizationKey] = nrOfStones
return nrOfStones
}
private fun BigInteger.getNrOfStones(blinks: Int, memoization: MutableMap<String, BigInteger>): BigInteger {
val nrOfStones: BigInteger
if (this == BigInteger.ZERO) {
nrOfStones = BigInteger.ONE.getNrOfStonesAfter(blinks - 1, memoization)
} else {
val digit = this.toString()
if (digit.length % 2 == 0) {
val firstStone = digit.substring(0, digit.length / 2).toBigInteger()
val secondStone = digit.substring(digit.length / 2).toBigInteger()
nrOfStones = firstStone.getNrOfStonesAfter(blinks - 1, memoization)
.plus(secondStone.getNrOfStonesAfter(blinks - 1, memoization))
} else {
nrOfStones = (this * BigInteger.valueOf(2024)).getNrOfStonesAfter(blinks - 1, memoization)
}
}
return nrOfStones
}