Created
August 15, 2011 19:33
-
-
Save anonymous/1147550 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; 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 (and (<= 2 nbr-count 3) | |
(or (= 3 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