Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mikera/1130280 to your computer and use it in GitHub Desktop.

Select an option

Save mikera/1130280 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 6, 2011.
    26 changes: 26 additions & 0 deletions enodyt-4clojure-solution94.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    ;; 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)))))))