Last active
December 28, 2017 10:31
-
-
Save craftybones/2684aa3ecce273291d2b4df60826eb41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 game-of-life.core | |
(:require [game-of-life.utils :refer [sum-of-vectors | |
setify]])) | |
(def neighbor-offsets | |
(into (hash-set) | |
(for [i (range -1 2) | |
j (range -1 2) | |
:when (not= [i j] [0 0])] | |
[i j]))) | |
(defn neighboring | |
[co-ord] | |
(set (map (partial sum-of-vectors co-ord) neighbor-offsets))) | |
(defn number-of-live-neighbours | |
[current-generation] | |
(comp count (partial filter current-generation) neighboring)) | |
(defn three-live-neighbours? | |
[current-generation] | |
(comp (partial = 3) (number-of-live-neighbours current-generation))) | |
(defn two-or-three-live-neighbours? | |
[current-generation] | |
(comp #(<= 2 % 3) (number-of-live-neighbours current-generation))) | |
(defn neighbors-that-may-come-alive | |
[current-generation] | |
(comp (mapcat neighboring) | |
(filter (complement current-generation)) | |
(filter (three-live-neighbours? current-generation)))) | |
(defn cells-that-stay-alive | |
[current-generation] | |
(filter (two-or-three-live-neighbours? current-generation))) | |
(defn next-generation | |
[current-generation] | |
(let [new (neighbors-that-may-come-alive current-generation) | |
existing (cells-that-stay-alive current-generation)] | |
(into #{} | |
(concat (sequence new current-generation) | |
(sequence existing current-generation))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment