-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path20.kt
More file actions
17 lines (15 loc) · 720 Bytes
/
20.kt
File metadata and controls
17 lines (15 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Collections.swap
fun MutableList<IndexedValue<Long>>.simulate(times: Int, decryptKey: Long = 1L) {
for (index in 0 until size * times) {
var i = indexOfFirst { it.index == index % size }
var v = (this[i].value * decryptKey % (size-1)).toInt()
repeat(Math.abs(v)) { i = (i + v / Math.abs(v)).mod(size).also { swap(this, i, it) } }
}
val i0 = indexOfFirst { it.value == 0L }
(i0..i0+3000 step 1000).sumOf { this[it%size].value * decryptKey }.run(::println)
}
fun main() {
generateSequence(::readlnOrNull).map { it.toLong() }.withIndex().toList()
.also { it.toMutableList().simulate(1) }
.also { it.toMutableList().simulate(10, 811589153L) }
}