Skip to content

Instantly share code, notes, and snippets.

@WitherOrNot
Created August 29, 2019 01:31
Show Gist options
  • Save WitherOrNot/88b0fe7407ebaef8d387af6e21260a91 to your computer and use it in GitHub Desktop.
Save WitherOrNot/88b0fe7407ebaef8d387af6e21260a91 to your computer and use it in GitHub Desktop.
1d cyclic cellular automaton
from random import randrange
from PIL import Image
colortrans = [
(255,0,0),
(255,127,0),
(255,255,0),
(127,255,0),
(0,255,0),
(0,255,127),
(0,255,255),
(0,127,255),
(0,0,255),
(127,0,255),
(255,0,255),
(255,0,127)
]
n = 4
w = 1920
it = 1080
state = [randrange(0,n) for x in range(w)]
pxdata = state
def bound(n,k):
if n == k:
return 0
else:
return n
for i in range(it-1):
nstate = [0]*w
for i in range(0,w):
ng = [state[(i+j) % w] for j in [-1,1]]
if bound(state[i]+1, n) in ng:
nstate[i] = bound(state[i]+1, n)
else:
nstate[i] = state[i]
pxdata += nstate
state = nstate
pxdata = [colortrans[x] for x in pxdata]
outimg = Image.new("RGB", (w, it))
outimg.putdata(pxdata)
outimg.save("CyclicCA.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment