Last active
January 5, 2021 14:26
-
-
Save zmwangx/77c168e90b4321ee7eea to your computer and use it in GitHub Desktop.
Stephen Wolfram Rule 30 cellular automaton emulation in python, with the simplest initial state of exactly one filled cell. (UPDATE: I've published a much more efficient and refined package with builtin visualization: https://github.com/zmwangx/rule30)
This file contains hidden or 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
import sys | |
MAX_TIME = int(sys.argv[1]) | |
HALF_SIZE = MAX_TIME | |
indices = range(-HALF_SIZE, HALF_SIZE+1) | |
# initial condition | |
cells = {i: '0' for i in indices} | |
cells[0] = '1' | |
# padding on both ends | |
cells[-HALF_SIZE-1] = '0' | |
cells[ HALF_SIZE+1] = '0' | |
# time evolution | |
# | |
# rule set: (http://en.wikipedia.org/wiki/Rule_30) | |
# ------------------------------------------------ | |
# In all of Wolfram's elementary cellular automata, an infinite | |
# one-dimensional array of cellular automaton cells with only two | |
# states is considered, with each cell in some initial state. At | |
# discrete time intervals, every cell spontaneously changes state | |
# based on its current state and the state of its two neighbors. For | |
# Rule 30, the rule set which governs the next state of the automaton | |
# is: | |
# | |
# current pattern 111 110 101 100 011 010 001 000 | |
# new state for center cell 0 0 0 1 1 1 1 0 | |
new_state = {"111": '0', "110": '0', "101": '0', "000": '0', | |
"100": '1', "011": '1', "010": '1', "001": '1'} | |
for time in range(0, MAX_TIME): | |
# print current state | |
for i in indices: | |
if cells[i] == '1': | |
sys.stdout.write(u'\u2588') | |
else: | |
sys.stdout.write(' ') | |
sys.stdout.write('\n') | |
# evolve | |
patterns = {i: cells[i-1] + cells[i] + cells[i+1] for i in | |
indices} | |
cells = {i: new_state[patterns[i]] for i in indices} | |
cells[-HALF_SIZE-1] = '0' | |
cells[ HALF_SIZE+1] = '0' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment