Skip to content

Instantly share code, notes, and snippets.

@slipset slipset/conway.clj
Last active Jun 6, 2016

Embed
What would you like to do?
(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
You can’t perform that action at this time.