-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.kt
83 lines (67 loc) · 2.98 KB
/
day20.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
package day20
import assertEquals
import benchmark
import readInputLines
import repeat
import java.math.BigInteger
import java.util.*
fun main() {
val day = "20"
val demoInputLines = readInputLines("day$day-demo")
assertEquals(solvePart1(demoInputLines), 3)
val inputLines = readInputLines("day$day")
benchmark { solvePart1(inputLines).also { println("Solution part one: $it") } }
assertEquals(solvePart2(demoInputLines), BigInteger("1623178306"))
benchmark { solvePart2(inputLines).also { println("Solution part two: $it") } }
}
data class EncryptedNumber(
val initialIndex: Int,
val value: Int,
)
data class EncryptedNumberBig(
val initialIndex: Int,
val value: BigInteger,
)
private fun solvePart1(input: List<String>): Int {
val encryptedNums = input.mapIndexed { idx, num -> EncryptedNumber(initialIndex = idx, value = num.toInt()) }
val decryptedNums = LinkedList(encryptedNums.toMutableList())
for (encryptedNum in encryptedNums) {
val currentIndex: Int = decryptedNums.indexOfFirst { it.initialIndex == encryptedNum.initialIndex }
// calculate new index
val index = (currentIndex + (encryptedNum.value % (encryptedNums.size - 1))) % (encryptedNums.size - 1)
val finalIndex = if (index > 0) index else encryptedNums.size - 1 + index
// remove and insert at new loc
decryptedNums.removeAt(currentIndex)
decryptedNums.add(finalIndex, encryptedNum)
}
val decryptedSeq = decryptedNums.map { it.value }.asSequence().repeat()
val zeroIndex = decryptedSeq.indexOfFirst { it == 0 }
return decryptedSeq.elementAt(zeroIndex + 1000) + decryptedSeq.elementAt(zeroIndex + 2000) + decryptedSeq.elementAt(
zeroIndex + 3000
)
}
private fun solvePart2(input: List<String>): BigInteger {
val encryptedNums = input.mapIndexed { idx, num ->
EncryptedNumberBig(
initialIndex = idx,
value = BigInteger.valueOf(num.toLong()).times(BigInteger("811589153"))
)
}
val decryptedNums = LinkedList(encryptedNums.toMutableList())
repeat(10) {
for (encryptedNum in encryptedNums) {
val currentIndex: Int = decryptedNums.indexOfFirst { it.initialIndex == encryptedNum.initialIndex }
// calculate new index
val index =
(currentIndex + (encryptedNum.value.mod(BigInteger.valueOf((encryptedNums.size - 1).toLong()))).toInt()) % (encryptedNums.size - 1)
val finalIndex = if (index > 0) index else encryptedNums.size - 1 + index
// remove and insert at new loc
decryptedNums.removeAt(currentIndex)
decryptedNums.add(finalIndex, encryptedNum)
}
}
val decryptedSeq = decryptedNums.map { it.value }.asSequence().repeat()
val zeroIndex = decryptedSeq.indexOfFirst { it == BigInteger.ZERO }
return decryptedSeq.elementAt(zeroIndex + 1000).add(decryptedSeq.elementAt(zeroIndex + 2000))
.add(decryptedSeq.elementAt(zeroIndex + 3000))
}