File: cellular-rule.pl

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (134 lines) | stat: -rw-r--r-- 3,660 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl -w

# Copyright 2011, 2012 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath 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 3, or (at your option) any later
# version.
#
# Math-PlanePath 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 Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use Math::PlanePath::CellularRule;

# uncomment this to run the ### lines
use Smart::Comments;


{
  # A169707 rule 750
  my %grid;
  my $width = 40;
  my $height = 40;
  $grid{0,0} = 1;
  foreach my $level (0 .. 40) {

    print "level $level\n";
    foreach my $y (reverse -$height .. $height) {
      foreach my $x (-$width .. $width) {
        print $grid{$x,$y} // ' ';
      }
      print "\n";
    }

    my %new_grid = %grid;
    my $count_new = 0;
    foreach my $y (-$height .. $height) {
      foreach my $x (-$width .. $width) {
        my $count = (($grid{$x-1,$y} || 0)
                     + ($grid{$x+1,$y} || 0)
                     + ($grid{$x,$y-1} || 0)
                     + ($grid{$x,$y+1} || 0));
        # print "$level  $x,$y  count=$count\n";
        if ($count == 1 || $count == 3) {
          $new_grid{$x,$y} = 1;
          $count_new++;
        }
      }
    }
    print "count new $count_new\n";
        %grid = %new_grid;
  }
  exit 0;
}

{
  # compare path against Cellular::Automata::Wolfram values

  require Cellular::Automata::Wolfram;
  my $width = 50;
  my $x_offset = int($width/2)-1;
  my $num_of_gens = $x_offset - 1;
 RULE: foreach my $rule (0 .. 255) {
    my $path = Math::PlanePath::CellularRule->new(rule=>$rule);
    my $auto = Cellular::Automata::Wolfram->new
      (rule=>$rule, width=>$width, num_of_gens=>$num_of_gens);
    my $gens = $auto->{'gens'};
    foreach my $y (0 .. $#$gens) {
      my $auto_str = $gens->[$y];
      my $path_str = '';
      foreach my $i (0 .. length($auto_str)-1) {
        my $x = $i - $x_offset;
        $path_str .= ($x < -$y || $x > $y ? substr($auto_str,$i,1)
                      : $path->xy_is_visited($x,$y) ? '1' : '0');
      }
      if ($auto_str ne $path_str) {
        print "$rule y=$y\n";
        print "auto $auto_str\n";
        print "path $path_str\n";
        print "\n";
        next RULE;
      }
    }
  }
  exit 0;

}
{
  my $rule = 124;
  my $path = Math::PlanePath::CellularRule->new(rule=>$rule);
  my @ys = (5..20);
  @ys = map{$_*2+1} @ys;
  my @ns = map{$path->xy_to_n(-$_,$_)
             }@ys;
  my @diffs = map {$ns[$_]-$ns[$_-1]} 1 .. $#ns;
  print "[",join(',',@diffs),"]\n";
  my @dds = map {$diffs[$_]-$diffs[$_-1]} 1 .. $#diffs;
  print "[",join(',',@dds),"]\n";
  exit 0;
}

{
  my $rule = 57;
  my $path = Math::PlanePath::CellularRule->new(rule=>$rule);
  my @ys = (5..20);
  @ys = map{$_*2+1} @ys;
  my @ns = map{$path->xy_to_n(-$_,$_)
             }@ys;
  my @diffs = map {$ns[$_]-$ns[$_-1]} 1 .. $#ns;
  print "[",join(',',@diffs),"]\n";
  my @dds = map {$diffs[$_]-$diffs[$_-1]} 1 .. $#diffs;
  print "[",join(',',@dds),"]\n";
  exit 0;
}

{
  my $rule = 57;
  my $path = Math::PlanePath::CellularRule->new(rule=>$rule);
  my @ys = (5..10);
  @ys = map{$_*2+1} @ys;
  print "[",join(',',@ys),"]\n";
  print "[",join(',',map{$path->xy_to_n(-$_,$_)
                         }@ys),"]\n";
  exit 0;
}