Skip to content

Instantly share code, notes, and snippets.

@slipset
Last active June 6, 2016 10:11
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 slipset/ef0deb9d19312561d58e0a48ebd9fa36 to your computer and use it in GitHub Desktop.
Save slipset/ef0deb9d19312561d58e0a48ebd9fa36 to your computer and use it in GitHub Desktop.
(ns conways.core
(:gen-class))
(def board [5 5])
(def coords (for [x (range (first board)) y (range (second board))] [y x]))
(def neighbour-coords [[-1 -1] [0 -1] [1 -1]
[-1 0] [1 0]
[-1 1] [0 1] [1 1]])
(defn life [_ n] (get [0 0 1 1] n 0))
(defn neighbours [world [x y]]
(->> neighbour-coords
(map (fn [[x' y']] [(+ x x') (+ y y')]))
(keep (partial get-in world))
(apply +)))
(defn next-world [world new-world coord]
(update-in new-world coord life (neighbours world coord)))
(defn tick [world]
(reduce (partial next-world world) world coords))
(-> [[0 1 1 1 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
tick
tick
tick
tick
tick
tick
tick
tick
tick)
;;=> [[1 1 1 1 1] [0 0 0 0 0] [1 0 1 0 1] [0 0 0 0 0] [1 1 1 1 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment