# Game of Life
#
# Author: Domingo Gallardo Saavedra - dngallar@uc.cl
#

import random

def createOneRow(width):
    """ returns one row of zeros of width "width"...  
         You should use this in your
         createBoard(width, height) function """
    row = []
    for col in range(width):
        row += [0]
    return row
    
    
def createBoard(width, height):
    """ returns a 2d array with "height" rows and "width" cols """
    A = []
    for row in range(height):
        A += [createOneRow(width)]    # What do you need to add a whole row here?
    return A
	
def printBoard(A):
    for row in A:
        line = ''
        for col in row:
            if col == 0:
                line += ' '
            else:
                line += '*'
        print line
    
def printBoard1(A):
    for row in A:
        line = ''
        for col in row:
            line += str(col)
        print line
        
def diagonalize(width, height):
    """ creates an empty board and then modifies it
        so that it has a diagonal strip of "on" cells.
    """
    A = createBoard(width, height)

    for row in range(height):
        for col in range(width):
            if row == col:
                A[row][col] = 1
            else:
                A[row][col] = 0

    return A

def innerCells(w, h):
    """ returns a 2d array that has all live cells, 
        except for a one-cell-wide border of empty cells 
    """
    A = createBoard(w, h)

    for i in range(h):
        for j in range(w):
            if i == 0 or i == h-1 or j == 0 or j == w-1:
                A[i][j] = 0
            else:
                A[i][j] = 1

    return A
    
def randomCells(w, h):
    """ returns an array of randomly-assigned 1's and 0's 
        except that the outer edge of the array is still 
        completely empty
    """
    A = createBoard(w, h)

    for i in range(h):
        for j in range(w):
            if i == 0 or i == h-1 or j == 0 or j == w-1:
                A[i][j] = 0
            else:
                A[i][j] = random.choice([0, 1])

    return A
    
    
def copy(A):
    """ make a deep copy of the 2d array A.
    """
    h = len(A)
    w = len(A[0])
    
    B = createBoard(w, h)
    
    for i in range(h):
        for j in range(w):
            B[i][j] = A[i][j]
            
    return B
    
    
def innerReverse(A):
     """ takes an old 2d array (or "generation") 
        and then creates a new generation of the same 
        shape and size
     """
     h = len(A)
     w = len(A[0])
    
     B = copy(A)
     
     for i in range(h):
        for j in range(w):
            if i == 0 or i == h-1 or j == 0 or j == w-1:
                B[i][j] = 0
            else:
                if A[i][j] == 1:
                    B[i][j] = 0
                elif A[i][j] == 0:
                    B[i][j] = 1

     return B
        
def countNeighbors(row,col,A):
    """ return the number of live neighbors 
        for a cell in the board A at a particular row and col.
    """
    
    cont = 0
     
    for i in range(row-1,row+2):
        for j in range(col-1,col+2):
            cont += A[i][j]
    
    return cont - A[row][col]
            

def next_life_generation(A):
    """ makes a copy of A and then advances one
        generation of Conway's game of life within
        the *inner cells* of that copy.
        The outer edge always stays at 0.
    """
    h = len(A)
    w = len(A[0])
    
    newA = copy(A)
     
    for i in range(h):
        for j in range(w):
            if i == 0 or i == h-1 or j == 0 or j == w-1:
                A[i][j] = 0
            else:
                if countNeighbors(i,j,A) < 2:
                    newA[i][j] = 0
                elif countNeighbors(i,j,A) > 3:
                    newA[i][j] = 0
                elif A[i][j] == 0 and countNeighbors(i,j,A) == 3:
                    newA[i][j] = 1

    return newA
      
    
A = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0],
[0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,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