Skip to content

Instantly share code, notes, and snippets.

@mecdemort
Created April 11, 2011 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mecdemort/912946 to your computer and use it in GitHub Desktop.
Save mecdemort/912946 to your computer and use it in GitHub Desktop.
(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