Created
January 28, 2023 17:04
-
-
Save ytaki0801/50a658a91e33b4b68ff60d8906c8fa6a to your computer and use it in GitHub Desktop.
Elementary Cellular Automata in Python 20 lines, Rules from 1 to 255
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
from time import sleep | |
cnum, step, r, b, = 150, 50, {}, (False,True) | |
g = [(x,y,z) for x in b for y in b for z in b] | |
for rule in range(1,256): | |
n = [int(x) for x in format(rule, '08b')] | |
for i in range(8): r[g[i]] = n[7-i] | |
c, cn = [False]*cnum, [False]*cnum | |
c[cnum//2] = 1 | |
for _ in range(step): | |
print(''.join(['*' if e else ' ' for e in c])) | |
for i in range(cnum): | |
x = False if i == 0 else c[i-1] | |
y = c[i] | |
z = False if i == cnum-1 else c[i+1] | |
cn[i] = r[(x,y,z)] | |
for i in range(cnum): c[i] = cn[i] | |
sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment