Skip to content

Commit

Permalink
Original Christophe Grand's solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Paramonov committed Dec 9, 2012
1 parent b7d163d commit e31153f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/dojo/life.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

(ns dojo.life)

; http://rosettacode.org/wiki/Conway's_Game_of_Life#Clojure
; http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/

(defn neighbours [[x y]]
(for [dx [-1 0 1] dy [-1 0 1]
:when (not (and (zero? dx) (zero? dy)))]
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
[(+ x dx) (+ y dy)]))

(defn next-step [cells]
(set (for [[cell n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (some #{cell} cells)))]
cell)))
(set (for [[loc n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (cells loc)))]
loc)))

0 comments on commit e31153f

Please sign in to comment.