Skip to content

Commit 337502a

Browse files
committed
Replace REM with MOD.
1 parent e58452a commit 337502a

File tree

1 file changed

+3
-14
lines changed
  • src/main/kotlin/io/dmitrijs/aoc2022

1 file changed

+3
-14
lines changed

src/main/kotlin/io/dmitrijs/aoc2022/Day20.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.dmitrijs.aoc2022
22

3-
import kotlin.math.absoluteValue
4-
import kotlin.math.sign
5-
63
class Day20(private val input: List<Long>) {
74
fun puzzle1() = decipher()
85

@@ -25,17 +22,9 @@ class Day20(private val input: List<Long>) {
2522
private fun List<Number>.zeroIndex() = indexOfFirst { it.value == 0L }
2623

2724
private fun MutableList<Number>.mix(order: Int) {
28-
val index = indexOfFirst { it.order == order }
29-
val value = this[index].value
30-
31-
val rawIndex = (index + value.sign * (value.absoluteValue % lastIndex)).toInt()
32-
val newIndex = when {
33-
rawIndex <= 0 -> size - (rawIndex.absoluteValue + 1)
34-
rawIndex >= size -> rawIndex % size + 1
35-
else -> rawIndex
36-
}
37-
38-
add(newIndex, removeAt(index))
25+
val oldIndex = indexOfFirst { it.order == order }
26+
val newIndex = (oldIndex + this[oldIndex].value).mod(size - 1)
27+
add(newIndex, removeAt(oldIndex))
3928
}
4029

4130
private fun <T> List<T>.nth(num: Int) = this[num % size]

0 commit comments

Comments
 (0)