Skip to content

Instantly share code, notes, and snippets.

@abeppu
Created January 16, 2011 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abeppu/781483 to your computer and use it in GitHub Desktop.
Save abeppu/781483 to your computer and use it in GitHub Desktop.
makes pretty pictures of outputs from 1D cellular automata
#!/usr/bin/python
import random
import sys
def generateLine(width):
return random.getrandbits(width)
def bitlength(n):
return len(bin(n)) - 2
def getNewLine(width, line, rule):
newline = 0
for i in range(width - 1, -1, -1):
context = 0
for j in range(1, -2, -1):
position = (i + j) % width
context = context | (((line & 1 << position) >> position) << (j + 1))
newline = newline | getNextBit(context, rule) << i
return newline
def getNextBit(context, rule):
return rule & (1 << context) == 1 << context
def prettyPrintLine(width, line):
prettyline = ""
for i in range(width - 1, -1, -1):
if(line & 1 << i == 1 << i):
prettyline = prettyline + "1 1 1 "
else:
prettyline = prettyline + "0 0 0 "
print(prettyline)
width = sys.argv[1]
height = sys.argv[2]
rule = int(sys.argv[3])
line = "";
print("P3")
print(width +" "+height)
print("1")
for linenum in range(0, int(height)):
if linenum == 0:
line = generateLine(int(width))
prettyPrintLine(int(width), line)
continue
else:
line = getNewLine(int(width), line, rule)
prettyPrintLine(int(width), line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment