No need to have two different recursion functions for Day 11. Tidied up.
This commit is contained in:
parent
0192b33570
commit
2190f9e1ac
@ -38,16 +38,6 @@ ggg: out
|
|||||||
hhh: out
|
hhh: out
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data class Node(val name: String, val children: Set<Node>)
|
|
||||||
|
|
||||||
fun rec(children: HashMap<String, MutableSet<String>>, p: String, visited: MutableSet<String>): Int {
|
|
||||||
if (p == "out") return 1
|
|
||||||
visited.add(p)
|
|
||||||
val res = children[p]!!.sumOf { rec(children, it, visited) }
|
|
||||||
visited.remove(p)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
fun parseInput(input: List<String>): HashMap<String, MutableSet<String>> {
|
fun parseInput(input: List<String>): HashMap<String, MutableSet<String>> {
|
||||||
val children = HashMap<String, MutableSet<String>>()
|
val children = HashMap<String, MutableSet<String>>()
|
||||||
for (i in input) {
|
for (i in input) {
|
||||||
@ -58,13 +48,7 @@ hhh: out
|
|||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
|
|
||||||
fun part1(input: List<String>): Int {
|
fun rec(
|
||||||
val children = parseInput(input)
|
|
||||||
|
|
||||||
return rec(children, "you", HashSet())
|
|
||||||
}
|
|
||||||
|
|
||||||
fun rec2(
|
|
||||||
children: HashMap<String, MutableSet<String>>,
|
children: HashMap<String, MutableSet<String>>,
|
||||||
p: String,
|
p: String,
|
||||||
stop: String,
|
stop: String,
|
||||||
@ -72,24 +56,27 @@ hhh: out
|
|||||||
memo: HashMap<String, Long> = HashMap()
|
memo: HashMap<String, Long> = HashMap()
|
||||||
): Long {
|
): Long {
|
||||||
if (p == stop) return 1L
|
if (p == stop) return 1L
|
||||||
val memoVal = memo[p]
|
return memo.getOrPut(p) {
|
||||||
if (memoVal != null) return memoVal
|
|
||||||
visited.add(p)
|
visited.add(p)
|
||||||
val res = children[p]?.sumOf { rec2(children, it, stop, visited, memo) } ?: 0L
|
val res = children[p]?.sumOf { rec(children, it, stop, visited, memo) } ?: 0L
|
||||||
visited.remove(p)
|
visited.remove(p)
|
||||||
memo[p] = res
|
res
|
||||||
return res
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1(input: List<String>): Long {
|
||||||
|
return rec(parseInput(input), "you", "out")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun part2(input: List<String>): Long {
|
fun part2(input: List<String>): Long {
|
||||||
val children = parseInput(input)
|
val children = parseInput(input)
|
||||||
|
|
||||||
val srvToDac = rec2(children, "svr", "dac")
|
val srvToDac = rec(children, "svr", "dac")
|
||||||
val dacToFft = rec2(children, "dac", "fft")
|
val dacToFft = rec(children, "dac", "fft")
|
||||||
val fftToOut = rec2(children, "fft", "out")
|
val fftToOut = rec(children, "fft", "out")
|
||||||
val srvToFft = rec2(children, "svr", "fft")
|
val srvToFft = rec(children, "svr", "fft")
|
||||||
val fftToDac = rec2(children, "fft", "dac")
|
val fftToDac = rec(children, "fft", "dac")
|
||||||
val dacToOut = rec2(children, "dac", "out")
|
val dacToOut = rec(children, "dac", "out")
|
||||||
|
|
||||||
return srvToDac * dacToFft * fftToOut + srvToFft * fftToDac * dacToOut
|
return srvToDac * dacToFft * fftToOut + srvToFft * fftToDac * dacToOut
|
||||||
}
|
}
|
||||||
@ -102,7 +89,7 @@ hhh: out
|
|||||||
println("Part 1 Test: $testInputPart1Result")
|
println("Part 1 Test: $testInputPart1Result")
|
||||||
val testInputPart2Result = part2(testInput2)
|
val testInputPart2Result = part2(testInput2)
|
||||||
println("Part 2 Test: $testInputPart2Result")
|
println("Part 2 Test: $testInputPart2Result")
|
||||||
check(testInputPart1Result == 5)
|
check(testInputPart1Result == 5L)
|
||||||
check(testInputPart2Result == 2L)
|
check(testInputPart2Result == 2L)
|
||||||
|
|
||||||
val input = readInput("aoc2025/Day11")
|
val input = readInput("aoc2025/Day11")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user