Exploration of the Game of Life

Published on Nov 24th, 2018 by Kyle Piira

Basic Exploration

In-depth Exploration

  1. Can you find a pattern that returns to its starting point after more than two time steps?
  2. What's the longest you can see a pattern go without repeating a configuration?

To answer these questions I decided to build my own implementation of Conway’s Game of Life in Python to brute force all possible starting positions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Kyle's Game of Life Implementation
1) live cells die if they have 0, 1, or 4+ neighbors
2) empty cells have a birth if they have exactly three neighbors
"""

import numpy as np

# Create a blank board
board = np.zeros((5, 5))

def iterate(board):
	"""
	This function takes the current board state and returns the next state.
	"""
	conv_board = np.zeros((7, 7))
	conv_board[1:6, 1:6] = board
	conv = np.lib.stride_tricks.as_strided(
		conv_board, 
		(5, 5, 3, 3), # view shape
		(56, 8, 56, 8) # strides
	)
	
	# The new board
	b = np.zeros((5, 5))
	for i in range(5):
		for j in range(5):
			# Count the number of neighbor live cells
			if conv[i, j, 1, 1] == 1:
				# Subtract itself from total count
				b[i, j] = conv[i, j].sum() - 1
			else:
				b[i, j] = conv[i, j].sum()

	# Cells with 0, 1, or 4+ die
	b[np.any([b <= 1, b >= 4], axis=0)] = 0
	# Living cells with 2 neighbors get to keep living
	b[np.all([b == 2, board == 1], axis=0)] = 1
	# Dead cells with 2 neighbors stay dead
	b[np.all([b == 2, board == 0], axis=0)] = 0
	# All cells with 3 neighbors live
	b[b == 3] = 1
	# Return the new board state
	return b

if __name__ == '__main__':
	while input('Continue? [y/n] ') == 'y':
		print(board)
board = iterate(board)

Results

It took approximately two hours to play all 225 possible starting positions. There were 300,477,379 total steps taken with an average of 8.95 steps per game. The game with the longest period was 39 steps.

The Longest Game