Day25
This commit is contained in:
parent
0ad5dac997
commit
522618d106
50
days/src/main/kotlin/Day25.kt
Normal file
50
days/src/main/kotlin/Day25.kt
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
|
import be.vandewalleh.aoc.utils.input.Day
|
||||||
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
|
import be.vandewalleh.aoc.utils.input.Lines
|
||||||
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
|
|
||||||
|
@Day(25)
|
||||||
|
class Day25(@Lines val input: Input<IntArray>) {
|
||||||
|
private val doorPublicKey = input.value[0]
|
||||||
|
private val cardPublicKey = input.value[1]
|
||||||
|
|
||||||
|
private fun encryptionKey(loopSize: Int, publicKey: Int) = transformSubjectNumber(loopSize, publicKey)
|
||||||
|
|
||||||
|
private fun transformSubjectNumber(loopSize: Int, subjectNumber: Int): Int {
|
||||||
|
var number = 1L
|
||||||
|
repeat(loopSize) {
|
||||||
|
number *= subjectNumber
|
||||||
|
number %= 20201227
|
||||||
|
}
|
||||||
|
return number.toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun transformSubjectNumberStartingWith(currentNumber: Long, subjectNumber: Int): Long {
|
||||||
|
var number = currentNumber
|
||||||
|
number *= subjectNumber
|
||||||
|
number %= 20201227
|
||||||
|
return number
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findLoopSize(expectedPublicKey: Long): Int {
|
||||||
|
var computedPublicKey = 1L
|
||||||
|
var loopSize = 0
|
||||||
|
do {
|
||||||
|
computedPublicKey = transformSubjectNumberStartingWith(computedPublicKey, 7)
|
||||||
|
loopSize++
|
||||||
|
} while (computedPublicKey != expectedPublicKey)
|
||||||
|
return loopSize
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1(): Int {
|
||||||
|
val cardLoopSize = findLoopSize(cardPublicKey.toLong())
|
||||||
|
return encryptionKey(cardLoopSize, doorPublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() = with(createDay<Day25>()) {
|
||||||
|
println(part1())
|
||||||
|
}
|
||||||
2
days/src/main/resources/day25.txt
Normal file
2
days/src/main/resources/day25.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
1327981
|
||||||
|
2822615
|
||||||
Loading…
x
Reference in New Issue
Block a user