Guest User

AOC - 2025 day 11

a guest
Dec 11th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.85 KB | None | 0 0
  1.  
  2. class Reactor(val instructions: List<String>) {
  3.     fun numberOfPaths(): Long {
  4.         return recurseThroughInstruction("you", emptyList<String>())
  5.     }
  6.  
  7.     fun numberOfSvrPaths(): Long {
  8.         return recurseThroughInstruction("svr", listOf("fft", "dac"))
  9.     }
  10.  
  11.     private val knownPaths = mutableMapOf<String, Long>()
  12.  
  13.     private fun recurseThroughInstruction(node: String, mustVisit: List<String>): Long {
  14.         if (node == "out") return if (mustVisit.isEmpty()) 1 else 0
  15.         val next = instructions.first { it.startsWith("$node: ") }.substring(5).split(" ")
  16.         if (knownPaths[node + mustVisit] != null) return knownPaths[node + mustVisit]!!
  17.         val pathLength = next.fold(0L) { acc, s -> acc + recurseThroughInstruction(s, mustVisit - s) }
  18.         knownPaths[node + mustVisit] = pathLength
  19.         return pathLength
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment