1
0
Fork 0

day(12): keep only count

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-12 13:53:09 +01:00
parent 3203126dad
commit a415bf279e

View file

@ -7,9 +7,9 @@ fun findAllPaths(
path: List<String>, path: List<String>,
visited: Set<String>, visited: Set<String>,
twiceVisited: Boolean twiceVisited: Boolean
): Set<String> { ): Int {
if (path.last() == "end") { if (path.last() == "end") {
return setOf(path.joinToString("-")) return 1
} }
val neighbours = graph val neighbours = graph
@ -19,14 +19,14 @@ fun findAllPaths(
return neighbours.map { (neighbour, newTwiceVisited) -> return neighbours.map { (neighbour, newTwiceVisited) ->
findAllPaths(graph, path + neighbour, visited + neighbour, newTwiceVisited) findAllPaths(graph, path + neighbour, visited + neighbour, newTwiceVisited)
}.fold(emptySet()) { acc, it -> acc + it } }.fold(0, Int::plus)
} }
fun part1(input: Map<String, Set<String>>): Int = fun part1(input: Map<String, Set<String>>): Int =
findAllPaths(input, listOf("start"), setOf("start"), true).size findAllPaths(input, listOf("start"), setOf("start"), true)
fun part2(input: Map<String, Set<String>>): Int = fun part2(input: Map<String, Set<String>>): Int =
findAllPaths(input, listOf("start"), setOf("start"), false).size findAllPaths(input, listOf("start"), setOf("start"), false)
fun main() { fun main() {
val sample = readGraph(12, "sample") val sample = readGraph(12, "sample")