Guest User

Untitled

a guest
Dec 12th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import itertools
  2.  
  3. def neighbors(point):
  4. x, y = point
  5. yield x + 1, y
  6. yield x - 1, y
  7. yield x, y + 1
  8. yield x, y - 1
  9. yield x + 1, y + 1
  10. yield x + 1, y - 1
  11. yield x - 1, y + 1
  12. yield x - 1, y - 1
  13.  
  14.  
  15. def advance(board):
  16. new_state = set()
  17. recalc = board | set(itertools.chain(*map(neighbors, board)))
  18. for point in recalc:
  19. lives = sum((neigh in board) for neigh in neighbors(point))
  20. if lives == 3 or (lives == 2 and point in board):
  21. new_state.add(point)
  22. return new_state
  23.  
  24.  
  25. def print_board(state):
  26. for x in range(10):
  27. for y in range(10):
  28. mark = "*" if (x, y) in state else "-"
  29. print(mark, end='')
  30. print("")
  31. print("")
  32.  
  33.  
  34. #state = {(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)}
  35. state = {(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)}
  36. print_board(state)
  37.  
  38. for i in range(100):
  39. input()
  40. state = advance(state)
  41. print_board(state)
Add Comment
Please, Sign In to add comment