Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Reactor(val instructions: List<String>) {
- fun numberOfPaths(): Long {
- return recurseThroughInstruction("you", emptyList<String>())
- }
- fun numberOfSvrPaths(): Long {
- return recurseThroughInstruction("svr", listOf("fft", "dac"))
- }
- private val knownPaths = mutableMapOf<String, Long>()
- private fun recurseThroughInstruction(node: String, mustVisit: List<String>): Long {
- if (node == "out") return if (mustVisit.isEmpty()) 1 else 0
- val next = instructions.first { it.startsWith("$node: ") }.substring(5).split(" ")
- if (knownPaths[node + mustVisit] != null) return knownPaths[node + mustVisit]!!
- val pathLength = next.fold(0L) { acc, s -> acc + recurseThroughInstruction(s, mustVisit - s) }
- knownPaths[node + mustVisit] = pathLength
- return pathLength
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment