-
-
Save mlb-/38bbc679c24427c36364 to your computer and use it in GitHub Desktop.
4clojure GoL problem http://www.4clojure.com/problem/94
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
(defn gol | |
"Returns the next generation for a given game of life board" | |
[b] | |
(let [cells (for [x (range (count (first b))) | |
y (range (count b))] | |
[x y]) | |
is-alive? (fn [[x y]] | |
(= \# (get-in b [x y]))) | |
find-surrounding-live (fn [[cellx celly]] | |
(for [x (range (dec cellx) (+ 2 cellx)) | |
y (range (dec celly) (+ 2 celly)) | |
:when (is-alive? [x y]) | |
:when (not (and (= x cellx) (= y celly)))] | |
[x y])) | |
count-surrounding-live (comp count find-surrounding-live) | |
empty-board (->> (repeat (count b) | |
(vec (repeat (count (first b)) " "))) | |
vec)] | |
(->> cells | |
(filter #(or | |
(and (is-alive? %) (<= 2 (count-surrounding-live %) 3)) | |
(and (complement (is-alive? %)) (= 3 (count-surrounding-live %))))) | |
(reduce #(assoc-in %1 %2 "#") | |
empty-board) | |
(map (partial apply str))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment