Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from copy import deepcopy
- def idebug(*args):
- # return
- print(*args, file=sys.stderr, flush=True)
- def debug(*args):
- # return
- print(*args, file=sys.stderr, flush=True)
- def debug_grid(grid, label):
- debug(label)
- for y in range(h):
- debug(id(grid[y]), grid[y])
- def debug_grid_2(grid):
- for y in range(h):
- line = []
- for c in grid[y]:
- new_c = 'X' if c == '.' else 'O'
- line.append(new_c)
- debug(''.join(line))
- DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, -1), (-1, 1)]
- def update_old(g):
- grid_copy = deepcopy(g)
- for y in range(h):
- for x in range(w):
- current_cell = (x, y)
- alive_nei_count = 0
- for dx, dy in DIRECTIONS:
- nei_x, nei_y = move(*current_cell, dx, dy)
- if in_grid(nei_x, nei_y) and g[nei_y][nei_x] == 'O':
- alive_nei_count += 1
- # debug(f'cell {cell} has {alive_nei_count} neighbors')
- if g[y][x] == 'O':
- grid_copy[y][x] = 'O' if alive_nei_count in [min_nei, max_nei] else '.'
- # grid_copy[y][x] = 'O' if min_nei <= alive_nei_count <= max_nei else '.'
- else:
- grid_copy[y][x] = 'O' if alive_nei_count == resurrect else '.'
- return grid_copy
- def update(g):
- grid_copy = deepcopy(g)
- for y in range(h):
- for x in range(w):
- current_cell = (x, y)
- alive_nei_count = 0
- for dx, dy in DIRECTIONS:
- nei_x, nei_y = move(*current_cell, dx, dy)
- if in_grid(nei_x, nei_y) and g[nei_y][nei_x] == 'O':
- alive_nei_count += 1
- # debug(f'cell {cell} has {alive_nei_count} neighbors')
- if alive_nei_count not in [min_nei, max_nei]:
- grid_copy[y][x] = '.'
- if g[y][x] == '.' and alive_nei_count == resurrect:
- grid_copy[y][x] = 'O'
- return grid_copy
- # Auto-generated code below aims at helping you parse
- # the standard input according to the problem statement.
- h, w, n = [int(i) for i in input().split()]
- idebug(h, w, n)
- alive = input()
- idebug(alive)
- alive = [i for i, d in enumerate(alive) if int(d)]
- min_nei, max_nei = min(alive), max(alive)
- dead = input()
- idebug(dead)
- resurrect = dead.index('1')
- debug(f'min_nei = {min_nei}, max_nei = {max_nei}, resurrect = {resurrect}')
- in_grid = lambda x, y: 0 <= x < w and 0 <= y < h
- move = lambda x, y, dx, dy: (x + dx, y + dy)
- grid = []
- for i in range(h):
- line = input()
- idebug(line)
- grid.append(list(line))
- # Write an answer using print
- # To debug: print("Debug messages...", file=sys.stderr, flush=True)
- debug_grid(grid, 'start grid:')
- # debug_grid_2(grid)
- # debug(grid)
- new_grid = deepcopy(grid)
- for i in range(n):
- new_grid = update(new_grid)
- debug_grid(new_grid, f'grid round {i}:')
- debug_grid(new_grid, 'final grid:')
- # debug_grid_2(new_grid)
- for i in range(h):
- print(''.join(new_grid[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement