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
[" " | |
" ## " | |
" ## " | |
" ## " | |
" ## " | |
" "] |
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 gof | |
"board is a vector of strings" | |
[board] | |
(let [rows (count board) | |
cols (count (first board)) | |
[living-cell dead-cell] [\# \space] | |
alive? (fn [[row col]] | |
(= living-cell (nth (nth board row) col))) | |
live-neighbours (fn [[row col]] | |
(for [r [(dec row) row (inc row)] | |
c [(dec col) col (inc col)] | |
:when (and (< -1 r) (< r rows) | |
(< -1 c) (< c cols) | |
(not= [row col] [r c]) | |
(alive? [r c]))] | |
[r c])) | |
next-state (fn [[x y]] | |
(let [ln (-> [x y] live-neighbours count) | |
live (if (alive? [x y]) | |
(and (< 1 ln) (< ln 4)) | |
(= 3 ln))] | |
(if live living-cell dead-cell))) | |
->row (fn [r] (apply str | |
(for [c (range cols)] (next-state [r c]))))] | |
(mapv ->row (range rows)))) |
No comments :
Post a Comment