from tkinter import Tk, Canvas, PhotoImage, mainloop
from math import sin
from random import randint
import sys

# populate eight possible initial states
keys = list(range(8))
for i in keys:
    keys[i] = format(i, '03b')
keys.reverse()

# populate outcome for each initial state
rule_number = int(sys.argv[1])
values = list(format(rule_number, '08b'))

rules = dict(zip(keys, values))

def next_row(row):
    next = ""
    for i in range(len(row)):
        # logic to handle the "edges" of a row
        if i == 0: 
            key = "0" + row[0:2]
        elif i == len(row) - 1:
            key = row[i-1:i+1] + "0"
        else:
            key = row[i-1:i+2]
        next = next + rules[key]
    return next

WIDTH, HEIGHT = 400, 400
row = "1" * WIDTH

# display
window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="#ffffff")
canvas.pack()
img = PhotoImage(width=WIDTH, height=HEIGHT)
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")

for y in range(HEIGHT):
    for x in range(WIDTH):
        color = "#ffffff" if row[x] == "0" else "#000000"
        img.put(color, (x,y))
    row = next_row(row)

mainloop()