/[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 6 by dpavlin, Sun Aug 26 14:15:26 2007 UTC revision 12 by dpavlin, Fri Aug 31 15:17:11 2007 UTC
# Line 87  sub trace { Line 87  sub trace {
87          $visited[$pos]++;          $visited[$pos]++;
88  }  }
89    
 my ( $tr_x, $tr_y );  
 my ( $bl_x, $bl_y );  
   
90  sub x_y {  sub x_y {
91          my $p = shift;          my $p = shift;
92    
93          my $update = 0;          $p ||= $pos;
         if ( ! defined( $p ) ) {  
                 $p = $pos;  
                 $update = 1;  
         }  
94    
95          my $y = int($p / ($ll*2));          my $y = int($p / ($ll*2));
96          my $x = int(($p % $ll) / 2);          my $x = int(($p % $ll) / 2);
97    
98          warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";          warn "## x_y($p) -> $x,$y\n";
   
         if ( $update ) {  
   
 #               $tr_x = $x if $x > $tr_x && $y == $tr_y;  
 #               $tr_y = $y if $y < $tr_y && $x == $tr_x;  
   
                 if (  
                         $y < $tr_y  
                         ||  
                         $y <= $tr_y && $x > $tr_x  
                 ) {  
                         ( $tr_x, $tr_y ) = ( $x, $y );  
                         warn "## UPDATED tr: $tr_x,$tr_y\n";  
                 }  
   
                 if (  
                         $x < $bl_x  
                         ||  
                         $y > $bl_y  
                 ) {  
                         ( $bl_x, $bl_y ) = ( $x, $y );  
                         warn "## UPDATED bl: $bl_x,$bl_y\n";  
                 }  
   
 #               $bl_x = $x if $x < $bl_x; # && $y == $bl_y;  
 #               $bl_y = $y if $y > $bl_y; # && $x == $bl_x;  
           
         }  
   
         warn "## x_y($p) -> $x,$y ",  
                 $update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '',  
                 "\n";  
99    
100  #       return ($x,$y) if wantarray;  #       return ($x,$y) if wantarray;
101          return "$x,$y";          return "$x,$y";
# Line 157  sub follow { Line 118  sub follow {
118    
119  my $ok_path = qr/[\|\-]/;  my $ok_path = qr/[\|\-]/;
120    
121    my @corners;
122    my $corners_usage;
123    
124  sub can_turn {  sub can_turn {
125          my $try_step = shift;          my $try_step = shift;
126          die "no step?" unless defined $try_step;          die "no step?" unless defined $try_step;
# Line 172  sub can_turn { Line 136  sub can_turn {
136          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
137    
138          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
139                  warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";                  my $xy = x_y($pos);
140                    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";
141                  $step = $try_step;                  $step = $try_step;
142                    push @corners, $xy;
143                    $corners_usage->{$xy}++;
144                  return 1;                  return 1;
145          } else {          } else {
146                  warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";                  warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
# Line 199  sub shape { Line 166  sub shape {
166    
167          @trace = ($unknown) x ( $#board + 1 );          @trace = ($unknown) x ( $#board + 1 );
168          @directions = ();          @directions = ();
169            @corners = ();
170          trace;          trace;
171    
172          my $len = 0;          my $len = 0;
         ( $tr_x, $tr_y ) = ( $x,$y );  
         ( $bl_x, $bl_y ) = ( $x,$y );  
173          $step = 0;          $step = 0;
174    
175          if ( $visited[$pos] > 3 ) {          my $usage = $corners_usage->{"$x,$y"} || 0;
                 warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n";  
                 return;  
         }  
176    
177          warn "<<< shape from $x,$y pos: $pos\n";          warn "<<< shape from $x,$y [usage: $usage] pos: $pos\n";
178    
179            if ( $usage > 4 ) {
180                    warn "SKIPPED, usage > 4\n";
181                    return 0;
182            }
183    
184          while( 1 ) {          while( 1 ) {
185    
# Line 232  sub shape { Line 200  sub shape {
200                  }                  }
201                  warn draw;                  warn draw;
202    
                 warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";  
   
203                  if ( $debug ) {                  if ( $debug ) {
204                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
205                  }                  }
# Line 241  sub shape { Line 207  sub shape {
207    
208          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
209    
210          my $tr = "$tr_x,$tr_y";          warn "### corners: ",join(' ', map { $_ . " [" . $corners_usage->{$_} . "]" } @corners),"\n";
211          my $bl = "$bl_x,$bl_y";          warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n";
212    
213          warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n";          foreach my $c ( @corners ) {
214                    if ( ! grep( /\Q$c\E/, @shapes_start ) ) {
215          if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {                          warn "INFO: added corner $c as shape start\n";
216                  warn "INFO: added $tr top-right\n";                          push @shapes_start, $c;
217                  push @shapes_start, $tr;                  }
         }  
         if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) {  
                 warn "INFO: added $bl bottom-left\n";  
                 push @shapes_start, $bl;  
218          }          }
219    
220          print "WAIT> press enter"; my $foo = <STDIN>;          print "WAIT> press enter"; my $foo = <STDIN>;
# Line 261  sub shape { Line 223  sub shape {
223  }  }
224    
225  foreach my $start ( @shapes_start ) {  foreach my $start ( @shapes_start ) {
226          my $len = shape( split(/,/,$start) );          my ($x,$y) = split(/,/,$start);
227          warn "## $start has $len elements\n";          if ( $x < 8 && $y < 8 ) {
228                    my $len = shape( split(/,/,$start) );
229                    warn "## $start has $len elements\n";
230            } else {
231                    warn "SKIPPED $start\n";
232            }
233    }
234    
235    sub is_symmetric {
236            my $path = shift || die "no path?";
237    
238            my $h = length($path)/2;
239            return 0 if int($h) != $h;
240            my ($l,$r) = ( substr($path,0,$h), substr($path,$h) );
241            $r =~ tr/lrud/rldu/;
242    #       warn "$l -- $r\n";
243            return 0 unless $l eq $r;
244            return 1;
245  }  }
246    
247  print ">>> RESULTS:\n";  print ">>> RESULTS:\n";
248  foreach my $r ( @shapes_found ) {  foreach my $r ( @shapes_found ) {
249          printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};          printf "%2d,%-2d len: %-4d directions: %s %s\n",
250            $r->{x}, $r->{y}, $r->{len},
251            is_symmetric($r->{directions}) ? 'OK' : '!!',
252            $r->{directions},
253            ;
254  }  }

Legend:
Removed from v.6  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26