From 4d99c4d6d9d3967a7a32053fabcc8c822b44b989 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 12 Dec 2021 10:04:47 +0100 Subject: [PATCH] day(12): remove redundant helper function Signed-off-by: Matej Focko --- src/year2021/day12/Day12.kt | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/year2021/day12/Day12.kt b/src/year2021/day12/Day12.kt index d46e215..9e395a5 100644 --- a/src/year2021/day12/Day12.kt +++ b/src/year2021/day12/Day12.kt @@ -2,23 +2,6 @@ package year2021.day12 import readGraph -fun findAllPaths(graph: Map>, path: List, visited: Set): Set { - 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>): Int = - findAllPaths(input, listOf("start"), setOf("start")).size - fun findAllPaths( graph: Map>, path: List, @@ -41,7 +24,11 @@ fun findAllPaths( }.fold(emptySet()) { acc, it -> acc + it } } -fun part2(input: Map>): Int = findAllPaths(input, listOf("start"), setOf("start"), false).size +fun part1(input: Map>): Int = + findAllPaths(input, listOf("start"), setOf("start"), true).size + +fun part2(input: Map>): Int = + findAllPaths(input, listOf("start"), setOf("start"), false).size fun main() { val sample = readGraph(12, "sample")