Skip to content

Instantly share code, notes, and snippets.

@cgrand
Forked from laurentpetit/gist:1200343
Created September 7, 2011 11:41
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 cgrand/1200354 to your computer and use it in GitHub Desktop.
Save cgrand/1200354 to your computer and use it in GitHub Desktop.
(defn neighbours [[x y]]
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
[(+ dx x) (+ dy y)]))
(defn step [cells]
(set (for [[loc n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (cells loc)))]
loc)))
(def board #{[2 1] [2 2] [2 3]})
(defn print-board [board]
(doseq [x (range 5) y (range 5)]
(if (= y 0) (print "\n"))
(print (if (board [x y]) "[X]" " . "))))
(defn display-grids [grids]
(doseq [board grids]
(print-board board)
(print "\n")))
(display-grids (take 5 (iterate step board)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment