Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def step(live_cells):
- def get_neighbors(cell):
- e = [cell[0] + 1, cell[1] - 0]
- n = [cell[0] + 0, cell[1] + 1]
- s = [cell[0] - 0, cell[1] - 1]
- w = [cell[0] - 1, cell[1] + 0]
- se = [cell[0] + 1, cell[1] - 1]
- ne = [cell[0] + 1, cell[1] + 1]
- sw = [cell[0] - 1, cell[1] - 1]
- nw = [cell[0] - 1, cell[1] + 1]
- neighbors = [e, n, s, w, se, ne, sw, nw]
- return neighbors
- def neighbor_count(live_cells, neighbors):
- return sum(1 for a in neighbors if a in live_cells)
- new_live_cells = []
- neighbor_list = []
- # live cells with 2 or 3 neighbors live
- for cell in live_cells:
- neighbors = get_neighbors(cell)
- if neighbor_count(live_cells, neighbors) in [2, 3]:
- if cell not in new_live_cells:
- new_live_cells.append(cell)
- for a in neighbors:
- if a not in live_cells:
- neighbor_list.append(a)
- # dead cells with exactly 3 neighbors become live
- for a in neighbor_list:
- if neighbor_list.count(a) == 3:
- if a not in new_live_cells:
- new_live_cells.append(a)
- return new_live_cells
- def get_cells(n):
- return [[0, i] for i in range(n)]
- def get_final_state(n):
- live_cells = get_cells(n)
- all_states = []
- count = 0
- while True:
- live_cells = step(live_cells)
- count += 1
- if count > 1000:
- return 'no resolution'
- if live_cells == []:
- return ['disappears', count, 0]
- if live_cells in all_states:
- return ['stable', count, count - all_states.index(live_cells) - 1]
- all_states.append([i for i in live_cells])
- for i in range(1, 101):
- print i, get_final_state(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement