Last active
August 31, 2023 01:08
-
-
Save gboncoffee/d876f6ce9a7f94ee50774094c42ea93a to your computer and use it in GitHub Desktop.
Rule 110 in Clojure
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 testapp.core | |
(:gen-class)) | |
(defn rule110 [a b c] | |
(case [a b c] | |
[1 1 1] 0 | |
[1 1 0] 1 | |
[1 0 1] 1 | |
[1 0 0] 0 | |
[0 1 1] 1 | |
[0 1 0] 1 | |
[0 0 1] 1 | |
[0 0 0] 0)) | |
(defn apply-rule110 [x l] | |
(cond | |
(= (count l) 0) [x] | |
(= (count l) 1) (cons (rule110 x (first l) 0) ()) | |
:else | |
(let [y (first l) r (next l)] | |
(cons (rule110 x y (first r)) (apply-rule110 y r))))) | |
(defn rule110-list [a] | |
(apply-rule110 0 a)) | |
(defn print-cell [a] | |
(if (= a 1) | |
(print "1") | |
(print " "))) | |
(defn print-cells [r] | |
(if (> (count r) 0) | |
(do | |
(print-cell (first r)) | |
(print-cells (next r))) | |
(println))) | |
(defn bin-num-from-char [c] | |
(if (= c \1) 1 0)) | |
(defn cells-from-string [s] | |
(if (> (count s) 0) | |
(cons | |
(bin-num-from-char (first s)) | |
(cells-from-string (next s))))) | |
(defn loop-rule110 [s n] | |
(when (> n 0) | |
(let [r (rule110-list s)] | |
(print-cells r) | |
(loop-rule110 r (- n 1))))) | |
(defn -main [& args] | |
(let [n (Integer/parseInt (read-line)) | |
s (cells-from-string (read-line))] | |
(loop-rule110 s n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment