-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11Reactor.kt
More file actions
41 lines (34 loc) · 1.32 KB
/
Copy pathDay11Reactor.kt
File metadata and controls
41 lines (34 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package adventofcode.year2025
import adventofcode.Puzzle
import adventofcode.PuzzleInput
private typealias Device = String
class Day11Reactor(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private fun parseInput() =
input
.lines()
.map { line -> line.split(": ", limit = 2) }
.associate { (fromDevice, toDevices) -> Pair(fromDevice, toDevices.split(" ").toSet()) }
override fun partOne() = parseInput().countPaths(YOU)
override fun partTwo() = parseInput().countPaths(SVR, mustVisit = setOf(DAC, FFT))
companion object {
private const val DAC = "dac"
private const val END = "out"
private const val FFT = "fft"
private const val SVR = "svr"
private const val YOU = "you"
private fun Map<Device, Set<Device>>.countPaths(
device: Device,
pathCounts: HashMap<Pair<Device, Set<Device>>, Long> = hashMapOf(),
mustVisit: Set<Device> = emptySet(),
): Long =
pathCounts.getOrPut(Pair(device, mustVisit)) {
when (device) {
END if mustVisit.isEmpty() -> 1
END -> 0
else -> getValue(device).sumOf { next -> countPaths(next, pathCounts, mustVisit - device) }
}
}
}
}