Created
August 6, 2011 19:41
-
-
Save anonymous/1129676 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
;; enodyt's solution to Game of Life | |
;; https://4clojure.com/problem/94 | |
(fn [board] | |
(let [dx (count (first board)) | |
dy (count board) | |
neighbours (fn [pos board] | |
(let [xs [[-1 -1] [0 -1] [1 -1] | |
[-1 0] [1 0] | |
[-1 1] [0 1] [1 1]]] | |
(count (filter #(= % \#) | |
(map #(get-in board | |
(map + pos %)) xs)))))] | |
(map | |
#(apply str %) | |
(partition | |
dx | |
(for [x (range dx) y (range dy)] | |
(let [n (neighbours [x y] board) | |
live? (= \# (get-in board [x y]))] | |
(cond | |
(and live? (< n 2)) \space | |
(and live? (or (= n 2) (= n 3))) \# | |
(and live? (> n 3)) \space | |
(and (not live?) (= n 3)) \# | |
:else \space))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment