Skip to content

Instantly share code, notes, and snippets.

@stevefaeembra
Last active August 29, 2015 14:00
Show Gist options
  • Save stevefaeembra/e4dc65fe20e7b513aa54 to your computer and use it in GitHub Desktop.
Save stevefaeembra/e4dc65fe20e7b513aa54 to your computer and use it in GitHub Desktop.
small python script to generate a wolfram cellular automaton
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
Created on 29 Apr 2014
@author: Steven Kay
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
ch = u"▓"
blank = " "
sett = {0:blank, 1:ch}
def getrules(ruleno):
rules={}
for x in range(0, 8):
rules[x] = (ruleno >> x) & 0x1
return rules
def getgen(rules, input):
op = []
wid = len(input)
for ix in range(0, wid):
left = (ix - 1) % wid
right = (ix + 1) % wid
value = ((input[left] == ch) << 2) | ((input[ix] == ch) << 1) | ((input[right] == ch))
op.append(sett[rules[value]])
return op
def getstartconfig(wid):
half = ((wid / 2) - 2)
return ((blank * half) + ch + (blank * half))
def loop(ruleno, wid):
op = getstartconfig(wid)
rules = getrules(ruleno)
s = "".join(op)
while True:
print s
op = getgen(rules, op)
s = "".join(op)
# loop(rule_number, width_in_characters)
loop(30, 132)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment