Created
October 1, 2009 10:18
-
-
Save cgrand/198880 to your computer and use it in GitHub Desktop.
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
(def board | |
(-> (vec (repeat 5 (vec (repeat 5 :off)))) | |
(assoc-in [0 0] :on) | |
(assoc-in [0 1] :on) | |
(assoc-in [1 2] :on))) | |
(defn neighbors-on [above [left _ right] below] | |
(count (filter #{:on} (concat above [left right] below)))) | |
(defn torus-windows [coll] | |
(partition 3 1 (concat [(last coll)] coll [(first coll)]))) | |
(defn rule [above current below] | |
(let [self (second current)] | |
(cond | |
(= :on self) :dying | |
(= :dying self) :off | |
(= 2 (neighbors-on above current below)) :on | |
:else :off))) | |
(defn step [board] | |
(map (fn [window] | |
(apply #(apply map rule %&) (map torus-windows window))) | |
(torus-windows board))) | |
(defn show [board] | |
(doseq [row board] | |
(println (apply str (map {:on \X :off \space :dying \.} row))))) | |
; (time (nth (iterate step board) 100)) | |
(doseq [board (take 10 (iterate step board))] | |
(println "-----------") | |
(show board)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment