/[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 3 by dpavlin, Sun Aug 26 10:26:13 2007 UTC revision 5 by dpavlin, Sun Aug 26 12:17:00 2007 UTC
# Line 26  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;  my @trace;
33    
# Line 42  my $pos = 0; Line 44  my $pos = 0;
44  # unknown trace position  # unknown trace position
45  my $unknown = ' ';  my $unknown = ' ';
46    
47    # path traversed
48    my @directions;
49    
50    my @shapes_found;
51    
52    my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ );
53    
54  sub draw {  sub draw {
55          my $o = 0;          my $o = 0;
56          my $out;          my $out;
# Line 60  sub trace { Line 69  sub trace {
69          $trace[ $pos ] = $board[ $pos ];          $trace[ $pos ] = $board[ $pos ];
70  }  }
71    
72    my ( $tr_x, $tr_y );
73    
74    sub pos_x_y {
75            my $y = int($pos / ($ll*2));
76            my $x = int(($pos % $ll) / 2);
77    
78            $tr_x = $x unless defined $tr_x;
79            $tr_y = $y unless defined $tr_y;
80    
81            $tr_x = $x if $x > $tr_x && $y == $tr_y;
82            $tr_y = $y if $y < $tr_y && $x == $tr_x;
83    
84            warn "## pos_x_y $pos -> $x,$y\n";
85    
86            return ($x,$y) if wantarray;
87            return "$x,$y";
88    }
89    
90  sub move {  sub move {
         warn "move $step $step_name[$step]\n";  
91          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
92          trace;          trace;
93          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
94          trace;          trace;
95          warn draw( @trace );          push @directions, $step;
96            warn "move $step $step_name[$step] to ", scalar pos_x_y, "\n";
97  }  }
98    
99  sub follow {  sub follow {
# Line 91  sub can_turn { Line 118  sub can_turn {
118          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
119    
120          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
121                  warn "OK can_turn $try_step $step_name[$try_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";
122                  $step = $try_step;                  $step = $try_step;
123                  return 1;                  return 1;
124          } else {          } else {
125                  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";
126                  return 0;                  return 0;
127          }          }
128  }  }
129    
130    sub show_directions {
131            return
132                    join('',
133                            map {
134                                    substr($step_name[$_],0,1)
135                            } @directions
136                    )
137            ;
138    }
139    
140  sub shape {  sub shape {
141    
142          my ($x,$y) = @_;          my ($x,$y) = @_;
# Line 108  sub shape { Line 145  sub shape {
145    
146          warn "<<< shape from $x,$y pos: $pos\n";          warn "<<< shape from $x,$y pos: $pos\n";
147          @trace = ($unknown) x ( $#board + 1 );          @trace = ($unknown) x ( $#board + 1 );
148            @directions = ();
149          trace;          trace;
150    
151          my $len = 0;          my $len = 0;
# Line 115  sub shape { Line 153  sub shape {
153          while( 1 ) {          while( 1 ) {
154    
155                  my $next_pos = $pos + $move_by[ $step ];                  my $next_pos = $pos + $move_by[ $step ];
156                  warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n";                  warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n";
157    
158                  if ( $trace[ $next_pos ] ne $unknown ) {                  if ( $trace[ $next_pos ] ne $unknown ) {
159                          warn "position $next_pos re-visited, exiting\n";                          warn "position $next_pos re-visited, exiting\n";
# Line 131  sub shape { Line 169  sub shape {
169                  }                  }
170                  warn draw( @trace );                  warn draw( @trace );
171    
172                  print "WAIT> press enter"; my $foo = <STDIN>;                  if ( $debug ) {
173                            warn "## tr: $tr_x,$tr_y\n";
174                            print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
175                    }
176          }          }
177    
178          warn ">>> ended at $pos, line length: $len\n";          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
179    
180            warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n";
181    
182            my $tr = "$tr_x,$tr_y";
183            if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {
184                    print "INFO: added top-right $tr\n";
185                    push @shapes_start, $tr;
186            }
187    
188            print "WAIT> press enter"; my $foo = <STDIN>;
189    
190          return $len;          return $len;
191  }  }
192    
193  my $shapes = '0,0 1,0';  foreach my $start ( @shapes_start ) {
   
 foreach my $start ( split(/\s/,$shapes) ) {  
194          my $len = shape( split(/,/,$start) );          my $len = shape( split(/,/,$start) );
195          warn "## $start has $len elements\n";          warn "## $start has $len elements\n";
196  }  }
197    
198    print ">>> RESULTS:\n";
199    foreach my $r ( @shapes_found ) {
200            printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};
201    }

Legend:
Removed from v.3  
changed lines
  Added in v.5

  ViewVC Help
Powered by ViewVC 1.1.26