|
|
@ -1,24 +1,31 @@
|
|
|
|
#!/usr/bin/env perl
|
|
|
|
#!/usr/bin/env perl
|
|
|
|
use strict;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use warnings;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get arguments
|
|
|
|
my $gens = $ARGV[0];
|
|
|
|
my $gens = $ARGV[0];
|
|
|
|
my $width = $ARGV[1];
|
|
|
|
my $width = $ARGV[1];
|
|
|
|
my $rule = $ARGV[2];
|
|
|
|
my $rule = $ARGV[2];
|
|
|
|
|
|
|
|
|
|
|
|
# Set up all possible rules 0 - 255
|
|
|
|
# Set up all possible rules 0 - 255
|
|
|
|
|
|
|
|
# 0 = 00000000
|
|
|
|
|
|
|
|
# 1 = 00000001
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
# 254 = 11111110
|
|
|
|
|
|
|
|
# 255 = 11111111
|
|
|
|
my @rules;
|
|
|
|
my @rules;
|
|
|
|
for (my $index = 255; $index >=0; $index--) {
|
|
|
|
for (my $index = 255; $index >=0; $index--) {
|
|
|
|
push @rules, [get_bin($index, 8)];
|
|
|
|
push @rules, [get_bin($index, 8)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# extract the correct binary number for this position and this rule
|
|
|
|
sub get_val_at_pos {
|
|
|
|
sub get_val_at_pos {
|
|
|
|
my ($binstr) = @_;
|
|
|
|
my ($binstr) = @_;
|
|
|
|
my $pos = sprintf ('%d', $binstr);
|
|
|
|
my $pos = sprintf ('%d', $binstr);
|
|
|
|
return $rules[$rule][$pos];
|
|
|
|
return $rules[$rule][$pos];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get the binary representation of a number as an array of 1's and 0's
|
|
|
|
sub get_bin{
|
|
|
|
sub get_bin{
|
|
|
|
my ($dec, $padding) = @_;
|
|
|
|
my ($dec, $padding) = @_;
|
|
|
|
my $bin = sprintf('%0' . $padding .'b',$dec);
|
|
|
|
my $bin = sprintf('%0' . $padding .'b',$dec);
|
|
|
@ -26,6 +33,7 @@ sub get_bin{
|
|
|
|
return @binarr;
|
|
|
|
return @binarr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Set up the initial condition to be all 1's except fo one 0 in the middle
|
|
|
|
sub set_up_init {
|
|
|
|
sub set_up_init {
|
|
|
|
my @init;
|
|
|
|
my @init;
|
|
|
|
my $half = $width / 2;
|
|
|
|
my $half = $width / 2;
|
|
|
@ -43,20 +51,26 @@ sub set_up_init {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Print usage
|
|
|
|
sub usage {
|
|
|
|
sub usage {
|
|
|
|
print "$0 <number of generations> <width> <rule number (0-255)>\n";
|
|
|
|
print "$0 <number of generations> <width> <rule number (0-255)>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Some error handling
|
|
|
|
if (scalar @ARGV < 3 or $rule > 255) {
|
|
|
|
if (scalar @ARGV < 3 or $rule > 255) {
|
|
|
|
usage;
|
|
|
|
usage;
|
|
|
|
exit 1;
|
|
|
|
exit 1;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get the initial condition and print it out
|
|
|
|
my @initial = set_up_init;
|
|
|
|
my @initial = set_up_init;
|
|
|
|
print @initial;
|
|
|
|
print @initial;
|
|
|
|
print "\n";
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Now loop through all the generations
|
|
|
|
for (my $gen = 0; $gen < $gens; $gen++) {
|
|
|
|
for (my $gen = 0; $gen < $gens; $gen++) {
|
|
|
|
|
|
|
|
# This is the next evoultion of the the automation
|
|
|
|
my @next;
|
|
|
|
my @next;
|
|
|
|
for (my $elem = 0; $elem < $width ; $elem++) {
|
|
|
|
for (my $elem = 0; $elem < $width ; $elem++) {
|
|
|
|
# Boundary conditions
|
|
|
|
# Boundary conditions
|
|
|
@ -65,12 +79,18 @@ for (my $gen = 0; $gen < $gens; $gen++) {
|
|
|
|
$state = $initial[$width -1] . $initial[$elem] . $initial[$elem +1 ] ;
|
|
|
|
$state = $initial[$width -1] . $initial[$elem] . $initial[$elem +1 ] ;
|
|
|
|
} elsif ($elem == $width -1 ) {
|
|
|
|
} elsif ($elem == $width -1 ) {
|
|
|
|
$state = $initial[$elem-1] . $initial[$elem] . $initial[0];
|
|
|
|
$state = $initial[$elem-1] . $initial[$elem] . $initial[0];
|
|
|
|
|
|
|
|
# Normal, we are not on the boundary
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$state = $initial[$elem - 1] . $initial[$elem] . $initial[$elem +1];
|
|
|
|
$state = $initial[$elem - 1] . $initial[$elem] . $initial[$elem +1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the corresponding value for this state
|
|
|
|
$next[$elem] = get_val_at_pos oct("0b".$state);
|
|
|
|
$next[$elem] = get_val_at_pos oct("0b".$state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# The next state is now the new initial
|
|
|
|
@initial = @next;
|
|
|
|
@initial = @next;
|
|
|
|
|
|
|
|
# Print it out
|
|
|
|
print @initial;
|
|
|
|
print @initial;
|
|
|
|
print "\n";
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exit 0
|
|
|
|