Last active
March 13, 2019 07:39
-
-
Save jpdenford/836f3b59756ed1c13e50f72fd5ab9fd4 to your computer and use it in GitHub Desktop.
A basic 2d Cellular Automaton
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
;; https://brilliant.org/practice/cellular-automaton | |
(defn offset [x size] | |
(map (fn [idx] (if (and (>= idx 0) (< idx size)) ind nil)) | |
[(- x 1) x (+ x 1)])) | |
(defn calc-cell [elems] | |
(case (map #(or % false) elems) | |
[true true true] true | |
[true true false] false | |
[true false true] true | |
[true false false] false | |
[false true true] true | |
[false true false] false | |
[false false true] true | |
[false false false] false)) | |
(defn next-state [cells] | |
(vec (map-indexed (fn [ind el] ;; how to avoid calling vec as map creates a seq? | |
(calc-cell | |
(map #(get cells %) (offset ind (count cells)) ) )) cells))) | |
(def st (assoc (vec (repeat 21 false)) 10 true)) | |
(next-state [false false false true false false false]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment