Last active
June 6, 2016 10:11
-
-
Save slipset/ef0deb9d19312561d58e0a48ebd9fa36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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