Skip to content

Instantly share code, notes, and snippets.

@omo

omo/Move.kt Secret

Created August 15, 2022 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omo/f8d155e5923adcd926283d41a55c0ef8 to your computer and use it in GitHub Desktop.
Save omo/f8d155e5923adcd926283d41a55c0ef8 to your computer and use it in GitHub Desktop.
import kotlin.math.*
fun debugln(str: String) = println(str)
//fun debugln(@Suppress("UNUSED_PARAMETER") unused: String) = {}
fun main() {
fun search(distance: Int): Int = when (distance) {
1 -> 2
2 -> 1
3 -> 1
4 -> 2
else -> {
val threes = distance / 3
when (distance % 3) {
0 -> threes
2 -> threes + search(2)
1 -> threes - 1 + search(1 + 3) // Step back and make the remaining 4.
else -> throw RuntimeException("Should never be here!")
}
}
}
val ntests = readLine()!!.toInt()
repeat(ntests) {
val distance = readLine()!!.toInt()
val steps = search(distance)
println("${steps}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment