Skip to content

Instantly share code, notes, and snippets.

@prajwalit
Created September 3, 2011 05:55
Show Gist options
  • Save prajwalit/1190678 to your computer and use it in GitHub Desktop.
Save prajwalit/1190678 to your computer and use it in GitHub Desktop.
[4clojure #106] Given a pair of numbers, the start and end point, find a path between the two using only three possible operations: double, halve (odd numbers cannot be halved) add 2 Find the shortest path through the "maze". Because there are multiple sh
(defn next-nums [n]
(filter integer? ((juxt (partial + n) (partial * n) (partial / n)) 2)))
(defn collect-next [coll step x]
(if (coll x)
step
(let [res (set (mapcat next-nums coll))]
(recur res (inc step) x))))
(defn get-steps [initial final]
(collect-next #{initial} 1 final))
(get-steps 3 12)
;; => 3
(get-steps 9 12)
;; => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment