Advertisement
philRG

Custom Game of Life

Sep 2nd, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import sys
  2. from copy import deepcopy
  3.  
  4.  
  5. def idebug(*args):
  6.     # return
  7.     print(*args, file=sys.stderr, flush=True)
  8.  
  9.  
  10. def debug(*args):
  11.     # return
  12.     print(*args, file=sys.stderr, flush=True)
  13.  
  14.  
  15. def debug_grid(grid, label):
  16.     debug(label)
  17.     for y in range(h):
  18.         debug(id(grid[y]), grid[y])
  19.  
  20.  
  21. def debug_grid_2(grid):
  22.     for y in range(h):
  23.         line = []
  24.         for c in grid[y]:
  25.             new_c = 'X' if c == '.' else 'O'
  26.             line.append(new_c)
  27.         debug(''.join(line))
  28.  
  29.  
  30. DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, -1), (-1, 1)]
  31.  
  32.  
  33. def update_old(g):
  34.     grid_copy = deepcopy(g)
  35.     for y in range(h):
  36.         for x in range(w):
  37.             current_cell = (x, y)
  38.             alive_nei_count = 0
  39.             for dx, dy in DIRECTIONS:
  40.                 nei_x, nei_y = move(*current_cell, dx, dy)
  41.                 if in_grid(nei_x, nei_y) and g[nei_y][nei_x] == 'O':
  42.                     alive_nei_count += 1
  43.             # debug(f'cell {cell} has {alive_nei_count} neighbors')
  44.             if g[y][x] == 'O':
  45.                 grid_copy[y][x] = 'O' if alive_nei_count in [min_nei, max_nei] else '.'
  46.                 # grid_copy[y][x] = 'O' if min_nei <= alive_nei_count <= max_nei else '.'
  47.             else:
  48.                 grid_copy[y][x] = 'O' if alive_nei_count == resurrect else '.'
  49.     return grid_copy
  50.  
  51.  
  52. def update(g):
  53.     grid_copy = deepcopy(g)
  54.     for y in range(h):
  55.         for x in range(w):
  56.             current_cell = (x, y)
  57.             alive_nei_count = 0
  58.             for dx, dy in DIRECTIONS:
  59.                 nei_x, nei_y = move(*current_cell, dx, dy)
  60.                 if in_grid(nei_x, nei_y) and g[nei_y][nei_x] == 'O':
  61.                     alive_nei_count += 1
  62.             # debug(f'cell {cell} has {alive_nei_count} neighbors')
  63.             if alive_nei_count not in [min_nei, max_nei]:
  64.                 grid_copy[y][x] = '.'
  65.             if g[y][x] == '.' and alive_nei_count == resurrect:
  66.                 grid_copy[y][x] = 'O'
  67.     return grid_copy
  68.  
  69.  
  70. # Auto-generated code below aims at helping you parse
  71. # the standard input according to the problem statement.
  72.  
  73. h, w, n = [int(i) for i in input().split()]
  74. idebug(h, w, n)
  75.  
  76. alive = input()
  77. idebug(alive)
  78. alive = [i for i, d in enumerate(alive) if int(d)]
  79. min_nei, max_nei = min(alive), max(alive)
  80.  
  81. dead = input()
  82. idebug(dead)
  83. resurrect = dead.index('1')
  84.  
  85. debug(f'min_nei = {min_nei}, max_nei = {max_nei}, resurrect = {resurrect}')
  86.  
  87. in_grid = lambda x, y: 0 <= x < w and 0 <= y < h
  88. move = lambda x, y, dx, dy: (x + dx, y + dy)
  89.  
  90. grid = []
  91. for i in range(h):
  92.     line = input()
  93.     idebug(line)
  94.     grid.append(list(line))
  95.  
  96. # Write an answer using print
  97. # To debug: print("Debug messages...", file=sys.stderr, flush=True)
  98. debug_grid(grid, 'start grid:')
  99. # debug_grid_2(grid)
  100. # debug(grid)
  101.  
  102. new_grid = deepcopy(grid)
  103. for i in range(n):
  104.     new_grid = update(new_grid)
  105.     debug_grid(new_grid, f'grid round {i}:')
  106.  
  107. debug_grid(new_grid, 'final grid:')
  108. # debug_grid_2(new_grid)
  109.  
  110. for i in range(h):
  111.     print(''.join(new_grid[i]))
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement