Created
September 3, 2011 05:55
-
-
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
This file contains 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 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