day(12): remove redundant helper function
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
96cbf099f7
commit
4d99c4d6d9
1 changed files with 5 additions and 18 deletions
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue