Created
April 15, 2012 17:15
-
-
Save slightair/2393948 to your computer and use it in GitHub Desktop.
one-dimensional cellular automaton
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
(def trials 32) | |
(def rule 90) | |
(def initial-state (concat (repeat 32 0) '(1) (repeat 32 0))) | |
(defn show-cells [cells] | |
(println (apply str (map #(cond (= % 0) " " | |
(= % 1) "*") cells)))) | |
(defn neighbors [state] | |
(concat | |
(list (cons (last state) (first (split-at 2 state)))) | |
(partition 3 1 state) | |
(list (concat (last (split-at (- (count state) 2) state)) (list (first state)))))) | |
(defn pattern [n] | |
(reduce + (map #(apply * %) (partition 2 (interleave [4 2 1] n))))) | |
(defn next-status [pat] | |
(if (> (bit-and rule (bit-shift-left 1 pat)) 0) 1 0)) | |
(defn tick [state] | |
(map next-status (map pattern (neighbors state)))) | |
(doseq [state (take trials (iterate tick initial-state))] | |
(show-cells state)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment