1
0
Fork 0

day(12): follow DRY

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-12 10:07:23 +01:00
parent 4d99c4d6d9
commit 3203126dad

View file

@ -15,12 +15,10 @@ fun findAllPaths(
val neighbours = graph val neighbours = graph
.getOrDefault(path.last(), emptySet()) .getOrDefault(path.last(), emptySet())
.filter { it == it.uppercase() || (!twiceVisited && it != "start") || !visited.contains(it) } .filter { it == it.uppercase() || (!twiceVisited && it != "start") || !visited.contains(it) }
.map { it to ((it == it.lowercase() && visited.contains(it)) || twiceVisited) }
return neighbours.map { neighbour -> return neighbours.map { (neighbour, newTwiceVisited) ->
if (neighbour == neighbour.lowercase() && visited.contains(neighbour)) findAllPaths(graph, path + neighbour, visited + neighbour, newTwiceVisited)
findAllPaths(graph, path + neighbour, visited, true)
else
findAllPaths(graph, path + neighbour, visited + neighbour, twiceVisited)
}.fold(emptySet()) { acc, it -> acc + it } }.fold(emptySet()) { acc, it -> acc + it }
} }