Last active
October 12, 2017 19:24
-
-
Save Shy/1803463f7e69aba0e5f66d0906b9ee11 to your computer and use it in GitHub Desktop.
Takes stdin containing information about a Conway's game of life board and returns iterations on that board.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Takes stdin containing information about a Conway's game of life board | |
and returns iterations on that board. | |
Format expected is: | |
n | |
w h | |
<row> <row> <EOF> | |
Try the following example: | |
14 | |
6 6 | |
0 0 0 0 0 0 | |
0 1 1 0 0 0 | |
0 1 1 0 0 0 | |
0 0 0 1 1 0 | |
0 0 0 1 1 0 | |
0 0 0 0 0 0 | |
""" | |
import sys | |
import copy | |
def iterate_conway_generation(current_game_board, width, height): | |
""" Takes a gameboard, iterates a generation and returns the updated game board.""" | |
# Deep Copies the game board since we'll need the board to update all at once. | |
next_game_board = copy.deepcopy(current_game_board) | |
for current_row in range(0, height): | |
for current_column in range(0, width): | |
neighbours = 0 | |
# Creates generator to provide locations of neighbouring columns. | |
n_generator = neighbours_generator(current_row, current_column, width, height) | |
# Iterates through each neighbour | |
for _ in range(0, 8): | |
neighbour_row, neighbour_column = next(n_generator) | |
neighbours += current_game_board[neighbour_row][neighbour_column] | |
# An empty square "comes to life" if it is adjacent to exactly three live squares | |
if neighbours == 3: | |
if current_game_board[current_row][current_column] == 0: | |
next_game_board[current_row][current_column] = 1 | |
# A live square "dies" if it has less than two live neighbors, | |
# or more than three live neighbors | |
elif neighbours < 2 or neighbours > 3: | |
if current_game_board[current_row][current_column] == 1: | |
next_game_board[current_row][current_column] = 0 | |
return next_game_board | |
def neighbours_generator(current_row, current_column, width, height): | |
"""Generator that takes current column and returns locations of the 8 neigbouring columns.""" | |
for x_offset in range(-1, 2): | |
for y_offset in range(-1, 2): | |
if (y_offset, x_offset) == (0, 0): | |
continue | |
else: | |
neighbour_column = current_column + x_offset | |
neighbour_row = current_row + y_offset | |
# Check if neighbour is outside the game grid. | |
# We don't need to check -1 since python automatically wraps for us. | |
if neighbour_column == width: | |
neighbour_column = 0 | |
if neighbour_row == height: | |
neighbour_row = 0 | |
yield (neighbour_row, neighbour_column) | |
def main(game_board, number_iterations, width, height): | |
"""Takes game board, runs conway generations and prints each generation.""" | |
current_generation = 0 | |
while current_generation is not number_iterations: | |
game_board = iterate_conway_generation(game_board, width, height) | |
for row in game_board: | |
print ' '.join(map(str, row)) | |
print '\n' | |
current_generation += 1 | |
if __name__ == "__main__": | |
# Parse stdin. | |
NUMBER_ITERATIONS = int(sys.stdin.readline()) | |
WIDTH, HEIGHT = map(int, sys.stdin.readline().split(" ")) | |
# Create empty game board, populate it with each row and then close the file. | |
STARTING_GAME_BOARD = [] | |
for _ in range(0, HEIGHT): | |
STARTING_GAME_BOARD.append(map(int, sys.stdin.readline().split(" "))) | |
main(STARTING_GAME_BOARD, NUMBER_ITERATIONS, WIDTH, HEIGHT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment