Created
February 20, 2012 09:56
-
-
Save ummels/1868626 to your computer and use it in GitHub Desktop.
amcnamara's solution to https://4clojure.com/problem/138
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
(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