Skip to content

Instantly share code, notes, and snippets.

@nulldatamap
Created July 15, 2014 20:48
Show Gist options
  • Select an option

  • Save nulldatamap/9830396542d4f4ba446d to your computer and use it in GitHub Desktop.

Select an option

Save nulldatamap/9830396542d4f4ba446d to your computer and use it in GitHub Desktop.
conway.py
y = 0
around = [ ( x, y ) for x in range( -1, 2 ) for y in range( -1, 2 ) ]
del around[4]
cells = [ [ False for j in xrange( 10 ) ] for i in xrange( 10 ) ]
def display_cells( c ):
s = ""
for w in c:
for l in w:
if l:
s += "#"
else:
s += " "
s += "\n"
print s
def inside( b ):
(x, y) = b
return (x > 0 and x < 10) and (y > 0 and y < 10)
def nmap( c ):
nm = [ [ 0 for j in xrange( 10 ) ] for i in xrange( 10 ) ]
for x in xrange( 10 ):
for y in xrange( 10 ):
a = filter( inside, map( lambda (i, j): (i+x, j+y), around ) )
for (ax, ay) in a:
if c[ax][ay]:
nm[x][y] += 1
return nm
def update( c, n ):
nc = list( c )
for x in xrange( 10 ):
for y in xrange( 10 ):
if not n[x][y] in [ 2, 3 ]:
nc[x][y] = False
elif (not c[x][y]) and n[x][y] == 3:
nc[x][y] = True
return nc
def step( initial ):
return update( initial, nmap( initial ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment