Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amalloy/1147551 to your computer and use it in GitHub Desktop.
Save amalloy/1147551 to your computer and use it in GitHub Desktop.
;; amalloy's solution to Game of Life
;; https://4clojure.com/problem/94
(fn [board]
(let [board (vec (map vec board))
[h w] (map count ((juxt identity first) board))
cell (fn [y x]
(get-in board (map mod [y x] [h w])))
alive? (comp #{\#} cell)
neighbors (let [offsets [-1 0 1]
deltas (for [y offsets, x offsets
:when (not= y x 0)]
[y x])]
(fn [y x]
(count
(for [[dy dx] deltas
:when (alive? (+ y dy) (+ x dx))]
true))))
new-state (fn [y x]
(let [nbr-count (neighbors y x)]
(if (or (= 3 nbr-count)
(and (= 2 nbr-count)
(alive? y x)))
\#
\space)))]
(for [y (range h)]
(apply str (for [x (range w)]
(new-state y x))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment