#!/usr/bin/env python

''' pbmlife.py outfile.pbm: play 2-D game of life to a PBM file. '''

from sys import argv
from random import randint

width, height = 1280*8, 854*8 # try, say, 800*800 at first

outf = open(argv[1], "w")

outf.write("P1 %s %s\n" % (width, height))

# To do: decode a rule-number

rules = [0, 1, 1, 1, 0, 1, 1, 0]
# print rules

def loopslice(lst, idx, count):
	'''Give me a chunk of a list as though it looped.'''
	res = []
	for i in range(count):
		res.append(lst[(i + idx) % len(lst)])
	if type(res) != type([]):
		print 'whoa'
	return res

def succ(line):
	s = []
	for cellidx in range(width):
		neigh = loopslice(line, cellidx - 1, 3)
		# print neigh
		code = sum([neigh[0], neigh[1] * 2, neigh[2] * 4])
		# print code
#		if randint(0, 255) == 137: # arbitrary!
#			code = randint(0, 7)
		s.append(rules[code])
	return s

def draw(row):
	for cell in row:
		outf.write("%s " % cell)
	outf.write("\n")

row = []
for cell in range(width):
	row.append(randint(0, 1))

for i in range(height):
	draw(row)
	row = succ(row)