Quick and dirty rulestring simplifier

For scripts to aid with computation or simulation in cellular automata.
Post Reply
Naszvadi
Posts: 1244
Joined: May 7th, 2016, 8:53 am
Contact:

Quick and dirty rulestring simplifier

Post by Naszvadi » March 22nd, 2017, 7:22 pm

Hi!

A standalone python script, does not need golly, just standard python env. Here you are:

Code: Select all

#!/usr/bin/env python
# No commercial use
# Author: Naszvadi P.
# Do not violate the laws of author's country!
import sys

myarglist = sys.argv[1:]
oldrule = myarglist[0]

helper1 = range(2)

for i in range(2):
  helper1[i] = [
    { "0": 0 },
    { "c": 0, "e": 0  },
    { "a": 0, "c": 0, "e": 0, "i": 0, "k": 0, "n": 0  },
    { "a": 0, "c": 0, "e": 0, "i": 0, "j": 0, "k": 0, "n": 0, "q": 0, "r": 0, "y": 0 },
    { "a": 0, "c": 0, "e": 0, "i": 0, "j": 0, "k": 0, "n": 0, "q": 0, "r": 0, "t": 0, "w": 0, "y": 0, "z": 0 },
    { "a": 0, "c": 0, "e": 0, "i": 0, "j": 0, "k": 0, "n": 0, "q": 0, "r": 0, "y": 0 },
    { "a": 0, "c": 0, "e": 0, "i": 0, "k": 0, "n": 0  },
    { "c": 0, "e": 0  },
    { "0": 0 }
  ]

state = ''
survive = 1
direction = 1
oldrule += '.'
for char in oldrule:
  if char in '012345678./_Ss':
    if char != state:
      if state and state in '012345678' and 0 == max([i[1] for i in helper1[survive][int(state)].iteritems()]):
        for j, k in helper1[survive][int(state)].iteritems():
          helper1[survive][int(state)][j] = 1
      state = char
    if char in 'sS':
      survive = 1
    elif char in '/_':
      survive = 1 - survive
    direction = 1
  elif char.lower() in 'aceijknqrtwyz':
    helper1[survive][int(state)][char.lower()] = direction
  elif char == '-':
    direction = 0
    for i in helper1[survive][int(state)].iteritems():
      helper1[survive][int(state)][i[0]] = 1
  elif char.lower() =='b':
    survive = 0
    direction = 1

newrule = "B"
for base in range(2):
  for state in range(9):
    helper2 = [i[1] for i in helper1[base][state].iteritems()]
    if 1 == max(helper2):
      helper3 = ''
      if 0 == min(helper2):
        if helper2.count(1)/(1.0*len(helper2))<=0.501:
          helper3 = [j for j, k in helper1[base][state].iteritems() if ((k==1) and (j!='0'))]
          helper3.sort()
          helper3 = ''.join(helper3)
        else:
          helper3 = [j for j, k in helper1[base][state].iteritems() if ((k==0) and (j!='0'))]
          helper3.sort()
          helper3 = '-' + ''.join(helper3)
      newrule += str(state) + helper3
  if base == 0:
    newrule += '/S'

print newrule
Example session:

Code: Select all

$ python rule_repack.py B01c2cn3c4c2ei3kajr4kaiqtwz5kajr6ei4e5c6cn7c8/S
B01c2-ak3acjkr4-jnry5acjkr6-ak7c8/S
$
Used the well-known Hensel notation, more info here at Non-totalistic_Life-like_cellular_automaton wikientry.

Solution for this: ../forums/viewtopic.php?f=11&t=2368&start=75#p41945

Post Reply