Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikera/1130280 to your computer and use it in GitHub Desktop.
Save mikera/1130280 to your computer and use it in GitHub Desktop.
;; 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