Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2012 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3374386 to your computer and use it in GitHub Desktop.
Save anonymous/3374386 to your computer and use it in GitHub Desktop.
Solutions for 4Clojure problem 106 - http://www.4clojure.com/problem/106
;;;;;;;;;;;;;;;;;;;
;; CLOJURE CODE: ;;
;;;;;;;;;;;;;;;;;;;
(defn transform [x]
(if (even? x) [(+ x 2) (* x 2) (/ x 2)]
[(+ x 2) (* x 2)]))
(defn find-path
([number goal]
(find-path 1 [number] goal))
([step branches goal]
(if (not-any? #(= % goal) branches)
(find-path (inc step) (flatten (map transform branches)) goal)
step)))
(println (find-path 3 347))
################
# PYTHON CODE: #
################
goal = 347
start = 3
steps = 1
branches = [start]
while not goal in branches:
steps += 1
newBranches = []
for branch in branches:
newBranches.append(branch+2)
newBranches.append(branch*2)
if branch % 2 == 0:
newBranches.append(branch/2)
branches = newBranches
print steps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment