Commit be884867 authored by Adam P. Goucher's avatar Adam P. Goucher
Browse files

File comments

parent 093e8e8e
# Function to convert rulestring to transition table:
from lifelib.genera.isotropic import str2tab
except ImportError:
......@@ -13,6 +11,11 @@ except ImportError:
import lifelib
def rule2tuples(rulestring):
Convert an isotropic rulestring to a list of transitions.
This uses the function str2tab from the isotropic code in
rulestring = rulestring.replace('B', 'b').replace('/S', 's')
tt = str2tab(rulestring)
......@@ -47,6 +50,9 @@ def rule2tuples(rulestring):
def tuples2vn(tuples, filename):
Function to optionally save a set of transitions as a Golly ruletable.
from os.path import splitext, basename
......@@ -66,6 +72,10 @@ def tuples2vn(tuples, filename):
def tuples2tape(tuples):
Convert a set of transitions into the tape header for the metacell.
The output is a list of spacings between successive gliders.
tape = [0] + ([90]*41)
......@@ -102,35 +112,37 @@ def tuples2tape(tuples):
return tape
def main():
from sys import argv
if (len(argv) != 3):
raise TypeError("Usage: python infile.rle [outfile.rule]")
def main(input_file, output_file, output_table=None):
# The final result will be in b3s23:
sess = lifelib.load_rules('b3s23')
lt = sess.lifetree()
pattern_to_metafy = lt.load(argv[1])
# Load the target pattern into lifelib and obtain the isotropic rule:
pattern_to_metafy = lt.load(input_file)
original_rule = pattern_to_metafy.getrule()
# Cast to b3s23:
pattern_to_metafy = lt.pattern("", "b3s23") + pattern_to_metafy
# Convert the transition table into a tape header for the metacell:
rtuples = rule2tuples(original_rule)
tape = tuples2tape(rtuples)
if (len(argv) >= 4):
tuples2vn(rtuples, argv[3])
# Optionally save a Golly-compatible ruletable:
if output_table is not None:
tuples2vn(rtuples, output_table)
# Load an empty metacell with a glider pointing into its input:
empty_cell = lt.load("metacell/")
# Convert the tape header from a sequence of numbers into an actual
# stream of gliders heading into the metacell:
cell_without_glider = empty_cell & empty_cell[8]
glider = empty_cell - cell_without_glider
cell_with_tape = cell_without_glider +
# Run in HashLife:
# Run in HashLife to pipe the tape header into the metacell:
gensleft = 430531840
while (gensleft > 16777216):
cell_with_tape = cell_with_tape[16777216]
......@@ -138,21 +150,30 @@ def main():
print('%d generations remaining...' % gensleft)
cell_with_tape = cell_with_tape[gensleft]
# Load the metacell with full tape and default header:
print('Loading patterns...')
default_cell = lt.load("metacell/")
default_cell_full = lt.load("metacell/")
# Use a 3-way XOR to obtain a metacell with full tape and the
# correct header for the desired rule:
print('Applying patch...')
onecell = (cell_with_tape ^ default_cell) ^ default_cell_full
onecell = onecell.centre()
# Multiplication is overloaded to mean Kronecker product:
metafied_pattern = pattern_to_metafy * onecell
print('...saved output in %s' % argv[2])
# Save the metafied pattern:
print('...saved output in %s' % output_file)
if __name__ == '__main__':
from sys import argv
if (len(argv) != 3):
raise TypeError("Usage: python infile.rle [outfile.rule]")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment