How to resolve the algorithm One-dimensional cellular automata step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the Clojure programming language
Table of Contents
Problem Statement
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values. Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm One-dimensional cellular automata step by step in the Clojure programming language
Source code in the clojure programming language
(ns one-dimensional-cellular-automata
(:require (clojure.contrib (string :as s))))
(defn next-gen [cells]
(loop [cs cells ncs (s/take 1 cells)]
(let [f3 (s/take 3 cs)]
(if (= 3 (count f3))
(recur (s/drop 1 cs)
(str ncs (if (= 2 (count (filter #(= \# %) f3))) "#" "_")))
(str ncs (s/drop 1 cs))))))
(defn generate [n cells]
(if (= n 0)
'()
(cons cells (generate (dec n) (next-gen cells)))))
one-dimensional-cellular-automata> (doseq [cells (generate 9 "_###_##_#_#_#_#__#__")]
(println cells))
_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
nil
#!/usr/bin/env lein-exec
(require '[clojure.string :as str])
(def first-genr "_###_##_#_#_#_#__#__")
(def hospitable #{"_##"
"##_"
"#_#"})
(defn compute-next-genr
[genr]
(let [genr (str "_" genr "_")
groups (map str/join (partition 3 1 genr))
next-genr (for [g groups]
(if (hospitable g) \# \_))]
(str/join next-genr)))
;; ---------------- main -----------------
(loop [g first-genr
i 0]
(if (not= i 10)
(do (println g)
(recur (compute-next-genr g)
(inc i)))))
(def rules
{
[0 0 0] 0
[0 0 1] 0
[0 1 0] 0
[0 1 1] 1
[1 0 0] 0
[1 0 1] 1
[1 1 0] 1
[1 1 1] 0
})
(defn nextgen [gen]
(concat [0]
(->> gen
(partition 3 1)
(map vec)
(map rules))
[0]))
; Output time!
(doseq [g (take 10 (iterate nextgen [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]))]
(println g))
You may also check:How to resolve the algorithm Convex hull step by step in the Action! programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Elena programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the M4 programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the zkl programming language