/[symmetry-circle]/symmetry.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /symmetry.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4 by dpavlin, Sun Aug 26 10:59:01 2007 UTC revision 8 by dpavlin, Sun Aug 26 16:31:23 2007 UTC
# Line 30  my $debug = shift @ARGV || 0; Line 30  my $debug = shift @ARGV || 0;
30    
31  my @board = map { split(//) } split(/\n/, $board);  my @board = map { split(//) } split(/\n/, $board);
32  my @trace;  my @trace;
33    my @visited = (' ') x ($#board + 1);
34    
35  # line length  # line length
36  my $ll = 8 * 2 + 1;  my $ll = 8 * 2 + 1;
# Line 47  my $unknown = ' '; Line 48  my $unknown = ' ';
48  # path traversed  # path traversed
49  my @directions;  my @directions;
50    
51  my @found;  my @shapes_found;
52    
53    my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ );
54    
55  sub draw {  sub draw {
56          my $o = 0;          my $o = 0;
57          my $out;          my $out = "\n  " . join('',
58                    ('0 1 2 3 4 5 6 7 8 | ') x 3
59            ) . "\n";
60          while ( $o < $#board ) {          while ( $o < $#board ) {
61                    my $l = '|';
62                    if ( $o % ($ll*2) == 0) {
63                            my $y = int($o / ($ll*2));
64                            $out .= "$y ";
65                            $l = $y;
66                    } else {
67                            $out .= "  ";
68                    }
69    
70                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);
71                  $out .= '   ';                  $out .= " $l ";
72                  $out .= join('', @trace[ $o .. $o + $ll - 1 ]);                  $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
73                  $out .= "\n";                  $out .= " $l ";
74                    $out .= join('', @visited[ $o .. $o + $ll - 1 ]);
75                    $out .= " $l\n";
76                  $o += $ll;                  $o += $ll;
77          }          }
78    
79            $out .= "\n";
80    
81          return $out;          return $out;
82  }  }
83    
84  sub trace {  sub trace {
85          warn "## trace $pos\n";          warn "## trace $pos\n";
86          $trace[ $pos ] = $board[ $pos ];          $trace[ $pos ] = $board[ $pos ];
87            $visited[$pos]++;
88    }
89    
90    sub x_y {
91            my $p = shift;
92    
93            $p ||= $pos;
94    
95            my $y = int($p / ($ll*2));
96            my $x = int(($p % $ll) / 2);
97    
98            warn "## x_y($p) -> $x,$y\n";
99    
100    #       return ($x,$y) if wantarray;
101            return "$x,$y";
102  }  }
103    
104  sub move {  sub move {
         warn "move $step $step_name[$step]\n";  
105          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
106          trace;          trace;
107          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
108          trace;          trace;
109          push @directions, $step;          push @directions, $step;
110            warn "move $step $step_name[$step] to ", x_y, "\n";
111  }  }
112    
113  sub follow {  sub follow {
# Line 84  sub follow { Line 118  sub follow {
118    
119  my $ok_path = qr/[\|\-]/;  my $ok_path = qr/[\|\-]/;
120    
121    my @corners;
122    
123  sub can_turn {  sub can_turn {
124          my $try_step = shift;          my $try_step = shift;
125          die "no step?" unless defined $try_step;          die "no step?" unless defined $try_step;
# Line 94  sub can_turn { Line 130  sub can_turn {
130    
131          my $old = $trace[ $turn_pos ];          my $old = $trace[ $turn_pos ];
132          $trace[ $turn_pos ] = '?';          $trace[ $turn_pos ] = '?';
133          warn "TEST\n",draw( @trace );          $trace[ $pos ] = '*';
134            warn "TEST ", x_y($turn_pos), "\n",draw;
135          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
136    
137          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
138                  warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";                  my $xy = x_y($pos);
139                    warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos]) from $xy\n";
140                  $step = $try_step;                  $step = $try_step;
141                    push @corners, $xy;
142                  return 1;                  return 1;
143          } else {          } else {
144                  warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";                  warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
145                  return 0;                  return 0;
146          }          }
147  }  }
# Line 123  sub shape { Line 162  sub shape {
162    
163          $pos = $y * $ll * 2 + $x * 2;          $pos = $y * $ll * 2 + $x * 2;
164    
         warn "<<< shape from $x,$y pos: $pos\n";  
165          @trace = ($unknown) x ( $#board + 1 );          @trace = ($unknown) x ( $#board + 1 );
166          @directions = ();          @directions = ();
167            @corners = ();
168          trace;          trace;
169    
170          my $len = 0;          my $len = 0;
171            $step = 0;
172    
173            warn "<<< shape from $x,$y pos: $pos\n";
174    
175          while( 1 ) {          while( 1 ) {
176    
# Line 147  sub shape { Line 189  sub shape {
189                          warn "find line continuation from $step $step_name[$step]...\n";                          warn "find line continuation from $step $step_name[$step]...\n";
190                          can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";                          can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
191                  }                  }
192                  warn draw( @trace );                  warn draw;
193    
194                  if ( $debug ) {                  if ( $debug ) {
195                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
196                  }                  }
197          }          }
198    
199          push @found, { x => $x, y => $y, len => $len, directions => show_directions };          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
200    
201            warn "### corners: ",join(' ', @corners),"\n";
202          warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n";          warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n";
203    
204            foreach my $c ( @corners ) {
205                    if ( ! grep( /\Q$c\E/, @shapes_start ) ) {
206                            warn "INFO: added corner $c as shape start\n";
207                            push @shapes_start, $c;
208                    }
209            }
210    
211          print "WAIT> press enter"; my $foo = <STDIN>;          print "WAIT> press enter"; my $foo = <STDIN>;
212    
213          return $len;          return $len;
214  }  }
215    
216  my $shapes = '0,0 1,0 0,3';  foreach my $start ( @shapes_start ) {
217            my ($x,$y) = split(/,/,$start);
218  foreach my $start ( split(/\s/,$shapes) ) {          if ( $x < 8 && $y < 8 ) {
219          my $len = shape( split(/,/,$start) );                  my $len = shape( split(/,/,$start) );
220          warn "## $start has $len elements\n";                  warn "## $start has $len elements\n";
221            } else {
222                    warn "SKIPPED $start\n";
223            }
224  }  }
225    
226  print ">>> RESULTS:\n";  print ">>> RESULTS:\n";
227  foreach my $r ( @found ) {  foreach my $r ( @shapes_found ) {
228          printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};          printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};
229  }  }

Legend:
Removed from v.4  
changed lines
  Added in v.8

  ViewVC Help
Powered by ViewVC 1.1.26