You are viewing a single comment's thread. Return to all comments →
My Clojure solution
(def text "_______________________________1_______________________________ ______________________________111______________________________ _____________________________11111_____________________________ ____________________________1111111____________________________ ___________________________111111111___________________________ __________________________11111111111__________________________ _________________________1111111111111_________________________ ________________________111111111111111________________________ _______________________11111111111111111_______________________ ______________________1111111111111111111______________________ _____________________111111111111111111111_____________________ ____________________11111111111111111111111____________________ ___________________1111111111111111111111111___________________ __________________111111111111111111111111111__________________ _________________11111111111111111111111111111_________________ ________________1111111111111111111111111111111________________ _______________111111111111111111111111111111111_______________ ______________11111111111111111111111111111111111______________ _____________1111111111111111111111111111111111111_____________ ____________111111111111111111111111111111111111111____________ ___________11111111111111111111111111111111111111111___________ __________1111111111111111111111111111111111111111111__________ _________111111111111111111111111111111111111111111111_________ ________11111111111111111111111111111111111111111111111________ _______1111111111111111111111111111111111111111111111111_______ ______111111111111111111111111111111111111111111111111111______ _____11111111111111111111111111111111111111111111111111111_____ ____1111111111111111111111111111111111111111111111111111111____ ___111111111111111111111111111111111111111111111111111111111___ __11111111111111111111111111111111111111111111111111111111111__ _1111111111111111111111111111111111111111111111111111111111111_ 111111111111111111111111111111111111111111111111111111111111111") (defn sierpinski [a r] (let [height 32 c \_ [x y w h] r h2 (/ h 2)] (vec (for [i (range height) :let [j (- i y) s (get a i)]] (if (and (>= j h2) (< j h)) (str (subs s 0 (+ x j)) (apply str (repeat (- w j j) c)) (subs s (- (+ x w) j))) s))))) (defn regions [r] (let [[x y w h] r h2 (int (/ h 2)) w2 (int (/ w 2)) w4 (int (/ w 4))] (list [(+ x w4 1) y w2 h2] [x (+ y h2) w2 h2] [(+ x w2 1) (+ y h2) w2 h2]))) (defn fractalize [a r] (if (nil? r) a (recur (sierpinski a (first r)) (next r)))) (let [n (read-string (read-line)) array (clojure.string/split-lines text)] (doseq [s (loop [i 0 r '([0 0 63 32]) a array] (if (>= i n) a (recur (inc i) (mapcat regions r) (fractalize a r))))] (println s)))
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals: Sierpinski triangles
You are viewing a single comment's thread. Return to all comments →
My Clojure solution