Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- def neighbors(point):
- x, y = point
- yield x + 1, y
- yield x - 1, y
- yield x, y + 1
- yield x, y - 1
- yield x + 1, y + 1
- yield x + 1, y - 1
- yield x - 1, y + 1
- yield x - 1, y - 1
- def advance(board):
- new_state = set()
- recalc = board | set(itertools.chain(*map(neighbors, board)))
- for point in recalc:
- lives = sum((neigh in board) for neigh in neighbors(point))
- if lives == 3 or (lives == 2 and point in board):
- new_state.add(point)
- return new_state
- def print_board(state):
- for x in range(10):
- for y in range(10):
- mark = "*" if (x, y) in state else "-"
- print(mark, end='')
- print("")
- print("")
- #state = {(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)}
- state = {(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)}
- print_board(state)
- for i in range(100):
- input()
- state = advance(state)
- print_board(state)
Add Comment
Please, Sign In to add comment