-
-
Save amalloy/912989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn routes | |
"Calculates all the routes from [x y] to [0 0]. | |
Routes move only down and left." | |
[x y] | |
((fn routes* [queue] | |
(lazy-seq | |
(loop [queue queue] | |
(when (pos? (count queue)) | |
(let [[current-level [x y]] (peek queue) | |
queue-left (pop queue) | |
result-level (conj current-level [x y])] | |
(if (= 0 x y) | |
(cons result-level (routes* queue-left)) | |
(recur | |
(into queue-left | |
(for [new-pos [(when (pos? x) [(dec x) y]) | |
(when (pos? y) [x (dec y)])] | |
:when new-pos] | |
[result-level new-pos]))))))))) | |
(conj clojure.lang.PersistentQueue/EMPTY [[] [x y]]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment