# -*- coding: utf-8 -*-
"""problem1

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1cW15b1k2KbF4twYaQDzkuR3o1Njdbg7d
"""

import numpy as np
map = [
       [0,1,0,0,0],
       [0,0,1,0,0],
       [0,1,1,0,1],
       [0,1,0,0,1],
       [0,0,0,0,0],
]
map = np.array(map)
print("Time: 0")
print(map)

def boundary(i,j):
  if ( i>=0 and i<=4) :
    if (j>=0 and j<=4):
      return True
  return False


def count_live_neighbors(map, i, j):
    # Your code goes here
    count =0
    #first neighbor 
    first_i = i -1 
    first_j = j-1 
    if ( boundary(first_i,first_j )):
      if ( map[first_i, first_j ] == 1):
        count+=1 
    #second neighbor 
    second_i = i -1 
    second_j = j 
    if ( boundary(second_i,second_j )):
      if ( map[second_i, second_j ] == 1):
        count+=1 
    #third neighbor 
    third_i = i -1 
    third_j = j+1 
    if ( boundary(third_i,third_j )):
      if ( map[third_i, third_j ] == 1):
        count+=1 

    #fourth neighbor 
    fourth_i = i 
    fourth_j = j- 1 
    if ( boundary(fourth_i,fourth_j )):
      if ( map[fourth_i, fourth_j ] == 1):
        count+=1
    #fifth neighbor 
    fifth_i  = i 
    fifth_j = j+ 1 
    if ( boundary(fifth_i,fifth_j )):
      if ( map[fifth_i, fifth_j ] == 1):
        count+=1
    #sixth neighbor 
    sixth_i  = i +1
    sixth_j = j - 1 
    if ( boundary(sixth_i,sixth_j )):
      if ( map[sixth_i, sixth_j ] == 1):
        count+=1
    #seventh neighbor 
    seventh_i  = i +1 
    seventh_j = j  
    if ( boundary(seventh_i,seventh_j )):
      if ( map[seventh_i, seventh_j ] == 1):
        count+=1
    #eight neighbor 
    eight_i  = i +1 
    eight_j = j  + 1
    if ( boundary(eight_i,eight_j )):
      if ( map[eight_i, eight_j ] == 1):
        count+=1
    return count

def flip(map, i, j):
    # Your code goes here
    # flips 
    if ( map[i,j] == 1 ):
      if ( count_live_neighbors(map, i, j) < 2):
        return True
    # not flips
    if  map[i,j] == 1 :
      if ( count_live_neighbors(map, i, j) == 2):
        return False
    # flips 
    if  map[i,j] == 1 :
      if ( count_live_neighbors(map, i, j) > 2):
        return True
    #flips
    if  map[i,j] == 0  :
      if ( count_live_neighbors(map, i, j) == 2 or  count_live_neighbors(map, i, j) == 3 ):
        return True
    
    return False

for t in range(1,21):
    new_map = np.zeros(map.shape, dtype = int)
    for i in range(0,map.shape[0]):
        for j in range(0,map.shape[1]):
            flip_flag = flip(map, i, j)
            if flip_flag==True:
                new_map[i,j] = 1 - map[i,j]
            else:
                new_map[i,j] = map[i,j]
    
    map = new_map
    print("Time: {}".format(t))
    print(map)
#print(count_live_neighbors(map, 2,2))
# -*- coding: utf-8 -*-
"""problem1

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1cW15b1k2KbF4twYaQDzkuR3o1Njdbg7d
"""

import numpy as np
map = [
       [0,1,0,0,0],
       [0,0,1,0,0],
       [0,1,1,0,1],
       [0,1,0,0,1],
       [0,0,0,0,0],
]
map = np.array(map)
print("Time: 0")
print(map)

def boundary(i,j):
  if ( i>=0 and i<=4) :
    if (j>=0 and j<=4):
      return True
  return False


def count_live_neighbors(map, i, j):
    # Your code goes here
    count =0
    #first neighbor 
    first_i = i -1 
    first_j = j-1 
    if ( boundary(first_i,first_j )):
      if ( map[first_i, first_j ] == 1):
        count+=1 
    #second neighbor 
    second_i = i -1 
    second_j = j 
    if ( boundary(second_i,second_j )):
      if ( map[second_i, second_j ] == 1):
        count+=1 
    #third neighbor 
    third_i = i -1 
    third_j = j+1 
    if ( boundary(third_i,third_j )):
      if ( map[third_i, third_j ] == 1):
        count+=1 

    #fourth neighbor 
    fourth_i = i 
    fourth_j = j- 1 
    if ( boundary(fourth_i,fourth_j )):
      if ( map[fourth_i, fourth_j ] == 1):
        count+=1
    #fifth neighbor 
    fifth_i  = i 
    fifth_j = j+ 1 
    if ( boundary(fifth_i,fifth_j )):
      if ( map[fifth_i, fifth_j ] == 1):
        count+=1
    #sixth neighbor 
    sixth_i  = i +1
    sixth_j = j - 1 
    if ( boundary(sixth_i,sixth_j )):
      if ( map[sixth_i, sixth_j ] == 1):
        count+=1
    #seventh neighbor 
    seventh_i  = i +1 
    seventh_j = j  
    if ( boundary(seventh_i,seventh_j )):
      if ( map[seventh_i, seventh_j ] == 1):
        count+=1
    #eight neighbor 
    eight_i  = i +1 
    eight_j = j  + 1
    if ( boundary(eight_i,eight_j )):
      if ( map[eight_i, eight_j ] == 1):
        count+=1
    return count

def flip(map, i, j):
    # Your code goes here
    # flips 
    if ( map[i,j] == 1 ):
      if ( count_live_neighbors(map, i, j) < 2):
        return True
    # not flips
    if  map[i,j] == 1 :
      if ( count_live_neighbors(map, i, j) == 2):
        return False
    # flips 
    if  map[i,j] == 1 :
      if ( count_live_neighbors(map, i, j) > 2):
        return True
    #flips
    if  map[i,j] == 0  :
      if ( count_live_neighbors(map, i, j) == 2 or  count_live_neighbors(map, i, j) == 3 ):
        return True
    
    return False

for t in range(1,21):
    new_map = np.zeros(map.shape, dtype = int)
    for i in range(0,map.shape[0]):
        for j in range(0,map.shape[1]):
            flip_flag = flip(map, i, j)
            if flip_flag==True:
                new_map[i,j] = 1 - map[i,j]
            else:
                new_map[i,j] = map[i,j]
    
    map = new_map
    print("Time: {}".format(t))
    print(map)
#print(count_live_neighbors(map, 2,2))
# -*- coding: utf-8 -*- problem1  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1cW15b1k2KbF4twYaQDzkuR3o1Njdbg7d # Your code goes here #first neighbor #second neighbor #third neighbor #fourth neighbor #fifth neighbor #sixth neighbor #seventh neighbor #eight neighbor # Your code goes here # flips # not flips # flips #flips #print(count_live_neighbors(map, 2,2))