From 22b72824b729edef282ad851fa1cce628ecd823f Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Thu, 27 Oct 2016 17:24:38 +0200 Subject: [PATCH] Allow for arbitrary number of rules --- cellaut.pl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cellaut.pl b/cellaut.pl index dcfe69e..aae9703 100755 --- a/cellaut.pl +++ b/cellaut.pl @@ -1,12 +1,22 @@ #!/usr/bin/env perl use strict; use warnings; +use POSIX; # Get arguments my $gens = $ARGV[0]; my $width = $ARGV[1]; my $rule = $ARGV[2]; +# Set num_rules to 255 and bits to 8 by default +my $num_rules = $ARGV[3]; +my $bits = 8; +if($num_rules) { + $bits = floor(log($num_rules)/log(2)) + 1; +} else { + $num_rules = 255; +} + # Set up all possible rules 0 - 255 # 0 = 00000000 # 1 = 00000001 @@ -14,8 +24,8 @@ my $rule = $ARGV[2]; # 254 = 11111110 # 255 = 11111111 my @rules; -for (my $index = 255; $index >=0; $index--) { - push @rules, [get_bin($index, 8)]; +for (my $index = $num_rules; $index >=0; $index--) { + push @rules, [get_bin($index, $bits)]; } # extract the correct binary number for this position and this rule @@ -53,11 +63,11 @@ sub set_up_init { # Print usage sub usage { - print "$0 \n"; + print "$0 [num rules]\n"; } # Some error handling -if (scalar @ARGV < 3 or $rule > 255) { +if (scalar @ARGV < 3 or $rule > $num_rules) { usage; exit 1;