Skip to content

Instantly share code, notes, and snippets.

@mlb-
Forked from featheredtoast/gol.clj
Created October 7, 2015 01:58
Show Gist options
  • Save mlb-/38bbc679c24427c36364 to your computer and use it in GitHub Desktop.
Save mlb-/38bbc679c24427c36364 to your computer and use it in GitHub Desktop.
4clojure GoL problem http://www.4clojure.com/problem/94
(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