-
-
Save omo/f8d155e5923adcd926283d41a55c0ef8 to your computer and use it in GitHub Desktop.
This file contains 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
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