Skip to content

Instantly share code, notes, and snippets.

@jpdenford
Last active March 13, 2019 07:39
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 jpdenford/836f3b59756ed1c13e50f72fd5ab9fd4 to your computer and use it in GitHub Desktop.
Save jpdenford/836f3b59756ed1c13e50f72fd5ab9fd4 to your computer and use it in GitHub Desktop.
A basic 2d Cellular Automaton
;; 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