Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Last active August 29, 2015 14:01
Show Gist options
  • Save ecounysis/3113597bb9b2336df20f to your computer and use it in GitHub Desktop.
Save ecounysis/3113597bb9b2336df20f to your computer and use it in GitHub Desktop.
Elementary cellular automaton
import time
def st(a):
return "".join([str(k).replace("0","-").replace("1","X") for k in a])
def iter(a,r):
x=zeroes(width)
ww=width-1
for i in range(len(a)):
if i==0:
if '0'+str(a[0])+str(a[1]) in r:
x[0]=1
elif i==ww:
if str(a[i-1])+str(a[i])+'0' in r:
x[i]=1
else:
if str(a[i-1])+str(a[i])+str(a[i+1]) in r:
x[i]=1
return x
rules=[]
def go(a,r):
rr=buildRules(r)
global rules
rules=rr
for j in range(length):
print st(a)
time.sleep(0.1)
a=iter(a,rr)
def buildRules(intRule):
rr=[]
for i in range(8):
if (intRule & (2**i)) > 0:
rr.append(ruleset[7-i])
return rr
def zeroes(width):
return [0 for i in range(width)]
width=110
length=20
arr=zeroes(width)
arr[width/2]=1
ruleset=['111','110','101','100','011','010','001','000']
rule=41
go(arr,rule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment