/[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 1 by dpavlin, Sun Aug 26 02:53:41 2007 UTC revision 6 by dpavlin, Sun Aug 26 14:15:26 2007 UTC
# Line 6  Line 6 
6    
7  use strict;  use strict;
8    
 use Data::Dump qw/dump/;  
   
9  my $board = << '_BOARD_';  my $board = << '_BOARD_';
10  +-+-+-+-+-+-+-+-+  +-+-+-+-+-+-+-+-+
11  | |   |o| |     |  | |   |o| |     |
# Line 28  my $board = << '_BOARD_'; Line 26  my $board = << '_BOARD_';
26  +-+-+-+-+-+-+-+-+  +-+-+-+-+-+-+-+-+
27  _BOARD_  _BOARD_
28    
29    my $debug = shift @ARGV || 0;
30    
31  my @board = map { split(//) } split(/\n/, $board);  my @board = map { split(//) } split(/\n/, $board);
32    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;
37    
38    my @step_name = ( qw/right down left up/ );
39    my @move_by = ( 1, $ll, -1, -$ll );
40    my $step = 0;   # right
41    
42    # offset 0, top-left corner
43    my $pos = 0;
44    
45    # unknown trace position
46    my $unknown = ' ';
47    
48    # path traversed
49    my @directions;
50    
51    my @shapes_found;
52    
53    my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ );
54    
55  sub draw {  sub draw {
         my @board = @_;  
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                  $out .= join('', @board[ $o .. $o + $ll - 1 ]) . "\n";                  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 ]);
71                    $out .= " $l ";
72                    $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
73                    $out .= " $l ";
74                    $out .= join('', @visited[ $o .. $o + $ll - 1 ]);
75                    $out .= " $l\n";
76                  $o += $ll;                  $o += $ll;
77          }          }
         return $out;  
 }  
   
 print $board, draw( @board );  
78    
79  my @step_name = ( qw/right down left up/ );          $out .= "\n";
 my @move_by = ( 1, $ll, -1, -$ll );  
 my $step = 0;   # right  
80    
81  # offset 0, top-left corner          return $out;
82  my $pos = 0;  }
 $pos = 2;  
 $pos = 6;  
83    
 my @trace = ('x') x ( $#board + 1 );  
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  warn draw( @trace );  my ( $tr_x, $tr_y );
91  trace;  my ( $bl_x, $bl_y );
92    
93    sub x_y {
94            my $p = shift;
95    
96            my $update = 0;
97            if ( ! defined( $p ) ) {
98                    $p = $pos;
99                    $update = 1;
100            }
101    
102            my $y = int($p / ($ll*2));
103            my $x = int(($p % $ll) / 2);
104    
105            warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
106    
107            if ( $update ) {
108    
109    #               $tr_x = $x if $x > $tr_x && $y == $tr_y;
110    #               $tr_y = $y if $y < $tr_y && $x == $tr_x;
111    
112                    if (
113                            $y < $tr_y
114                            ||
115                            $y <= $tr_y && $x > $tr_x
116                    ) {
117                            ( $tr_x, $tr_y ) = ( $x, $y );
118                            warn "## UPDATED tr: $tr_x,$tr_y\n";
119                    }
120    
121                    if (
122                            $x < $bl_x
123                            ||
124                            $y > $bl_y
125                    ) {
126                            ( $bl_x, $bl_y ) = ( $x, $y );
127                            warn "## UPDATED bl: $bl_x,$bl_y\n";
128                    }
129    
130    #               $bl_x = $x if $x < $bl_x; # && $y == $bl_y;
131    #               $bl_y = $y if $y > $bl_y; # && $x == $bl_x;
132            
133            }
134    
135            warn "## x_y($p) -> $x,$y ",
136                    $update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '',
137                    "\n";
138    
139    #       return ($x,$y) if wantarray;
140            return "$x,$y";
141    }
142    
143  sub move {  sub move {
         warn "move $step $step_name[$step]\n";  
144          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
145          trace;          trace;
146          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
147          trace;          trace;
148          warn draw( @trace );          push @directions, $step;
149            warn "move $step $step_name[$step] to ", x_y, "\n";
150  }  }
151    
152  sub follow {  sub follow {
# Line 82  sub follow { Line 158  sub follow {
158  my $ok_path = qr/[\|\-]/;  my $ok_path = qr/[\|\-]/;
159    
160  sub can_turn {  sub can_turn {
161          my $step = shift;          my $try_step = shift;
162          die "no step?" unless defined $step;          die "no step?" unless defined $try_step;
163    
164          my $turn_pos = $pos + $move_by[ $step % 4 ];          $try_step %= 4;
165    
166            my $turn_pos = $pos + $move_by[$try_step];
167    
168          my $old = $trace[ $turn_pos ];          my $old = $trace[ $turn_pos ];
169          $trace[ $turn_pos ] = '?';          $trace[ $turn_pos ] = '?';
170          warn "TEST\n",draw( @trace );          $trace[ $pos ] = '*';
171            warn "TEST ", x_y($turn_pos), "\n",draw;
172          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
173    
174          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
175                  warn "OK can_turn $step_name[$step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";                  warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
176                    $step = $try_step;
177                  return 1;                  return 1;
178          } else {          } else {
179                  warn "NOPE can_turn $step_name[$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";
180                  return 0;                  return 0;
181          }          }
182  }  }
183    
184  while( 1 ) {  sub show_directions {
185            return
186                    join('',
187                            map {
188                                    substr($step_name[$_],0,1)
189                            } @directions
190                    )
191            ;
192    }
193    
194    sub shape {
195    
196          my $next_pos = $pos + $move_by[ $step ];          my ($x,$y) = @_;
         warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n";  
197    
198          if ( $trace[ $next_pos ] ne 'x' ) {          $pos = $y * $ll * 2 + $x * 2;
199                  warn "position $next_pos re-visited, exiting\n";  
200                  last;          @trace = ($unknown) x ( $#board + 1 );
201          } elsif ( $board[ $next_pos ] =~ $ok_path ) {          @directions = ();
202                  warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";          trace;
203                  move;  
204                  follow( $step+1 ) if can_turn( $step+1 );          my $len = 0;
205          } else {          ( $tr_x, $tr_y ) = ( $x,$y );
206                  warn "find line continuation from $step $step_name[$step]...\n";          ( $bl_x, $bl_y ) = ( $x,$y );
207                  foreach my $o ( -1, 1 ) {          $step = 0;
208                          if ( can_turn( $step + $o ) ) {  
209                                  $step = $step+$o;          if ( $visited[$pos] > 3 ) {
210                                  warn "new direction: $step $step_name[$step]\n";                  warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n";
211                                  follow( $step );                  return;
212                                  last;          }
213                          }  
214            warn "<<< shape from $x,$y pos: $pos\n";
215    
216            while( 1 ) {
217    
218                    my $next_pos = $pos + $move_by[ $step ];
219                    warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n";
220    
221                    if ( $trace[ $next_pos ] ne $unknown ) {
222                            warn "position $next_pos re-visited, exiting\n";
223                            last;
224                    } elsif ( $board[ $next_pos ] =~ $ok_path ) {
225                            warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";
226                            move;
227                            $len++;
228                            can_turn( $step + 1 );
229                    } else {
230                            warn "find line continuation from $step $step_name[$step]...\n";
231                            can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
232                  }                  }
233                    warn draw;
234    
235                    warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
236    
237                    if ( $debug ) {
238                            print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
239                    }
240            }
241    
242            push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
243    
244            my $tr = "$tr_x,$tr_y";
245            my $bl = "$bl_x,$bl_y";
246    
247            warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n";
248    
249            if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {
250                    warn "INFO: added $tr top-right\n";
251                    push @shapes_start, $tr;
252          }          }
253          warn draw( @trace );          if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) {
254                    warn "INFO: added $bl bottom-left\n";
255                    push @shapes_start, $bl;
256            }
257    
258            print "WAIT> press enter"; my $foo = <STDIN>;
259    
260            return $len;
261  }  }
262    
263  warn "ended at $pos\n";  foreach my $start ( @shapes_start ) {
264            my $len = shape( split(/,/,$start) );
265            warn "## $start has $len elements\n";
266    }
267    
268    print ">>> RESULTS:\n";
269    foreach my $r ( @shapes_found ) {
270            printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};
271    }

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

  ViewVC Help
Powered by ViewVC 1.1.26