Last active
April 15, 2021 18:09
-
-
Save cdiamon/ba49ead9497242ff70599cd443dea8cb 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 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