X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~jacobn/git?p=bedbugs.git;a=blobdiff_plain;f=src%2Fmungetable.pl;h=369d861dcf852cfaf52d20e43c956d3cc8752f5a;hp=705a198ccfaddc3b691c7a8833c4f3a1a29d5ef4;hb=9ca5a58108ea873729fb129d50654356785fae3f;hpb=f0cabf59def2699e8d877b944dc1e3c06cb63af0;ds=sidebyside diff --git a/src/mungetable.pl b/src/mungetable.pl index 705a198..369d861 100755 --- a/src/mungetable.pl +++ b/src/mungetable.pl @@ -1,5 +1,30 @@ #! /usr/bin/perl -w -# Hack to munge life.table for bedbugs. +# This awful single-use script expands out a description of a +# two-state cellular automaton in Golly table format (such as Conway's +# Life) into an equivalent rule table that collapses the 32 "Bedstead" +# states (16 on, 16 off) into the 2 automaton states while applying +# the same rule. +# It's slightly complicated because it's designed to work with +# the output of Golly make-ruletable.cpp, which is somewhat optimised +# using variables, but this script makes no attempt to optimise its +# output. It uses poorly-named variables to do a lot of its work. + +# This file is part of Bedbugs. +# Copyright (C) 2014 Jacob Nevins. +# +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. my %vars = (); my $done_vars = 0; @@ -13,12 +38,17 @@ while(<>) { die "Complicated variable" unless m/^var (.)=\{0,1\}$/; $vars{$1} = 1; } else { + # Assume this is a table rule. chomp; my @cells = split /,/; my %myvars = (); + # Only supports 8-neighbour automata die "Unexpected number of comma-separated items" unless @cells == 10; my @dirs = qw/c n ne e se s sw w nw/; if (!$done_vars) { + # Variables to match 32 states rather than 2 + # Numbering of states highly tied to bedbugs.c output + # Crap naming scheme: z=off, a=on, q=either print "var zz={0," . join(",", 2..17) . "}\n"; print "var aa={" . join(",", 18..33) . "}\n"; foreach my $d (@dirs) { @@ -30,6 +60,8 @@ while(<>) { } my $i = 0; while (my $dir = pop @dirs) { + # Assume each variable in the input is only referenced once + # per table row die "Duplicate var" if exists($myvars{$cells[$i]}); if (exists($vars{$cells[$i]})) { $myvars{$cells[$i]} = 1;