@@ -3,6 +3,8 @@ package adventofcode.year2025
33import adventofcode.Puzzle
44import adventofcode.PuzzleInput
55
6+ private typealias Device = String
7+
68class Day11Reactor (
79 customInput : PuzzleInput ? = null ,
810) : Puzzle(customInput) {
@@ -12,16 +14,28 @@ class Day11Reactor(
1214 .map { line -> line.split(" : " , limit = 2 ) }
1315 .associate { (fromDevice, toDevices) -> Pair (fromDevice, toDevices.split(" " ).toSet()) }
1416
15- override fun partOne () = parseInput().countPaths(START )
17+ override fun partOne () = parseInput().countPaths(YOU )
18+
19+ override fun partTwo () = parseInput().countPaths(SVR , mustVisit = setOf (DAC , FFT ))
1620
1721 companion object {
18- private const val START = " you "
22+ private const val DAC = " dac "
1923 private const val END = " out"
24+ private const val FFT = " fft"
25+ private const val SVR = " svr"
26+ private const val YOU = " you"
2027
21- private fun Map <String , Set <String >>.countPaths (device : String ): Long =
22- when (device) {
23- END -> 1
24- else -> this .getValue(device).sumOf { next -> countPaths(next) }
28+ private fun Map <Device , Set <Device >>.countPaths (
29+ device : Device ,
30+ pathCounts : HashMap <Pair <Device , Set <Device >>, Long > = hashMapOf(),
31+ mustVisit : Set <Device > = emptySet(),
32+ ): Long =
33+ pathCounts.getOrPut(Pair (device, mustVisit)) {
34+ when (device) {
35+ END if mustVisit.isEmpty() -> 1
36+ END -> 0
37+ else -> getValue(device).sumOf { next -> countPaths(next, pathCounts, mustVisit - device) }
38+ }
2539 }
2640 }
2741}
0 commit comments