Skip to content

Instantly share code, notes, and snippets.

@ummels
Created February 20, 2012 09:56
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 ummels/1868626 to your computer and use it in GitHub Desktop.
Save ummels/1868626 to your computer and use it in GitHub Desktop.
amcnamara's solution to https://4clojure.com/problem/138
(fn [s e]
(let [squares (apply str (take-while #(<= % e) (iterate #(* % %) s)))
filler (drop (count squares) (repeat (#(* % %) (Math/ceil (Math/sqrt (count squares)))) \*))
elements (concat squares filler)
dimension (int (Math/sqrt (count elements)))
cardinal (- (* 2 dimension) 1)
canvas (vec (repeat cardinal ""))
start-pos (* 2 (quot (dec dimension) 2))
path (take (count elements) (mapcat #(repeat (+ 2 (* 2 %)) (if (odd? %) dec inc)) (range)))
balance (fn [c] (#(apply str `(~@% ~@(interpose \ c) ~@%)) (repeat (/ (- cardinal (- (* 2 (count c)) 1)) 2) \ )))]
(map balance
((fn [canvas row [element & rest-elts] [move & rest-path]]
(if (nil? element)
canvas
(recur (update-in canvas [row] #(if (= move inc) `(~@% ~element) `(~element ~@%)))
(move row)
rest-elts
rest-path)))
canvas
start-pos
elements
path))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment