Skip to content

Instantly share code, notes, and snippets.

@leif
Created June 9, 2012 06:20
Show Gist options
  • Save leif/2899789 to your computer and use it in GitHub Desktop.
Save leif/2899789 to your computer and use it in GitHub Desktop.
"""
Wolfram 1-dimensional cellular automata
"""
__author__ = "Leif Ryge"
__license__ = "WTFPL"
import sys
if len(sys.argv) != 4:
print "Usage: %s rule width count" % (sys.argv[0],)
sys.exit(1)
rule, width, count = map(int, sys.argv[1:] )
world = [0] * (width/2) + [ 1 ] * (width/2)
y = 0
while True:
print "".join( " *"[cell] for cell in world )
world = [ rule>>(world[x-1]*4+world[x]*2+world[(x+1)%len(world)])&1
for x in range(len(world)) ]
y+=1
if y >= count:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment