Skip to content

Instantly share code, notes, and snippets.

@ummels
Created February 20, 2012 10:00
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/1868639 to your computer and use it in GitHub Desktop.
Save ummels/1868639 to your computer and use it in GitHub Desktop.
condotti's solution to https://4clojure.com/problem/138
(fn [l u]
(let [s (apply str (take-while #(<= % u) (iterate #(* % %) l)))
d (int (Math/ceil (Math/sqrt (count s))))
n (concat (seq s) (repeat (- (* d d) (count s)) \*))
m (loop [m {} sx n
r (if (even? d) (- d 2) (dec d)) c (- d 1)
dr (mapcat #(repeat (* % 2) (if (even? %) -1 1)) (range))
dc (mapcat #(repeat (inc (* % 2)) (if (even? %) 1 -1)) (range))]
(if (empty? sx) m
(recur (assoc m [r c] (first sx)) (rest sx)
(+ r (first dr)) (+ c (first dc))
(rest dr) (rest dc))))]
(for [r (range (dec (* d 2)))] (apply str (for [c (range (dec (* d 2)))] (m [r c] \space))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment