Skip to content

Instantly share code, notes, and snippets.

@cdiamon
Last active April 15, 2021 18:09
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 cdiamon/ba49ead9497242ff70599cd443dea8cb to your computer and use it in GitHub Desktop.
Save cdiamon/ba49ead9497242ff70599cd443dea8cb to your computer and use it in GitHub Desktop.
import java.util.*
import kotlin.collections.HashMap
fun main() {
println(findRoute(4, 57).contentToString())
}
fun findRoute(start: Int, end: Int): IntArray {
val allSolutions: HashMap<Int, List<Int>> = hashMapOf()
for (multiplier in 9 downTo 2) {
var currentNumber = end
val path = arrayListOf(currentNumber)
while (currentNumber > start) {
if (currentNumber % multiplier == 0 && (currentNumber / multiplier) >= start) {
currentNumber /= multiplier
path.add(currentNumber)
} else {
currentNumber--
path.add(currentNumber)
}
}
allSolutions[path.size] = path
}
if (allSolutions.isEmpty()) throw EmptyStackException()
var pathCounter = 2
while (true) {
allSolutions[pathCounter]?.also { return it.reversed().toIntArray() }
pathCounter++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment