day(12): follow DRY
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
4d99c4d6d9
commit
3203126dad
1 changed files with 3 additions and 5 deletions
|
@ -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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue