ephemient
12/25/2020, 1:45 PMtodd.ginsberg
12/25/2020, 2:09 PMclass Day25(input: List<String>) {
private val cardPk = input.first().toLong()
private val doorPk = input.last().toLong()
fun solvePart1(): Long =
transform(findLoopSize(cardPk), doorPk)
private fun transform(loopSize: Int, subject: Long): Long =
generateSequence(1L) { (it * subject) % MAGIC_NUMBER }.drop(loopSize).first()
private fun findLoopSize(target: Long): Int =
generateSequence(1L) { (it * MAGIC_SUBJECT) % MAGIC_NUMBER }.indexOf(target)
companion object {
const val MAGIC_NUMBER = 20201227
const val MAGIC_SUBJECT = 7
}
}
It was faster with while loops, but I like this better 🙂Nir
12/25/2020, 2:32 PMephemient
12/25/2020, 2:47 PMtodd.ginsberg
12/25/2020, 2:48 PMJakub Gwóźdź
12/25/2020, 3:00 PMtodd.ginsberg
12/25/2020, 3:21 PMephemient
12/25/2020, 3:21 PMJakub Gwóźdź
12/25/2020, 3:25 PMephemient
12/25/2020, 3:31 PMNir
12/25/2020, 4:49 PMephemient
12/25/2020, 5:09 PMbjonnh
12/28/2020, 8:55 PMconst val COLOR = 20201227
const val SUBJECT = 7
fun encrypt(turns: Int, subject: Long) = generateSequence(1L) { (it * subject) % COLOR }.elementAtOrNull(turns) ?: -1L
fun findLoopsForValue(value: Long): Int = generateSequence(1L) { (it * SUBJECT) % COLOR }.indexOf(value)
fun main() {
val cardPubKey = 2959251L //5764801L
val doorPubKey = 4542595L //17807724L
val cardTurns = findLoopsForValue(cardPubKey)
println(encrypt(cardTurns, doorPubKey))
}