1
0
Fork 0

day(12): remove redundant helper function

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-12 10:04:47 +01:00
parent 96cbf099f7
commit 4d99c4d6d9

View file

@ -2,23 +2,6 @@ package year2021.day12
import readGraph
fun findAllPaths(graph: Map<String, Set<String>>, path: List<String>, visited: Set<String>): Set<String> {
if (path.last() == "end") {
return setOf(path.joinToString("-"))
}
val neighbours = graph
.getOrDefault(path.last(), emptySet())
.filter { it == it.uppercase() || !visited.contains(it) }
return neighbours.map { neighbour ->
findAllPaths(graph, path + neighbour, visited + neighbour)
}.fold(emptySet()) { acc, it -> acc + it }
}
fun part1(input: Map<String, Set<String>>): Int =
findAllPaths(input, listOf("start"), setOf("start")).size
fun findAllPaths(
graph: Map<String, Set<String>>,
path: List<String>,
@ -41,7 +24,11 @@ fun findAllPaths(
}.fold(emptySet()) { acc, it -> acc + it }
}
fun part2(input: Map<String, Set<String>>): Int = findAllPaths(input, listOf("start"), setOf("start"), false).size
fun part1(input: Map<String, Set<String>>): Int =
findAllPaths(input, listOf("start"), setOf("start"), true).size
fun part2(input: Map<String, Set<String>>): Int =
findAllPaths(input, listOf("start"), setOf("start"), false).size
fun main() {
val sample = readGraph(12, "sample")