Created
December 2, 2025 03:57
-
-
Save Sedose/bb1ca896be12d5e56ad0d3ddf683d344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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