Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created December 2, 2025 03:57
Show Gist options
  • Select an option

  • Save Sedose/bb1ca896be12d5e56ad0d3ddf683d344 to your computer and use it in GitHub Desktop.

Select an option

Save Sedose/bb1ca896be12d5e56ad0d3ddf683d344 to your computer and use it in GitHub Desktop.
fun main() {
input.let(::calculatePassword)
.let(::println)
}
fun calculatePassword(input: List<String>): Int =
input.map(::parseMoveDelta)
.scan(50) { position, delta ->
normalize(position + delta)
}
.count(0::equals)
fun parseMoveDelta(text: String): Int {
val amount = text.drop(1).toInt()
return when (text.first()) {
'L' -> -amount
'R' -> amount
else -> error("Unexpected move instruction")
}
}
const val start = 0
const val end = 100
// Forces position to stay within [`start`, `end`] range
// which represents closed dial
fun normalize(position: Int): Int {
var normalized = position
while (normalized < start) normalized += end
while (normalized >= end) normalized -= end
return normalized
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment