/[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 2 by dpavlin, Sun Aug 26 09:20:07 2007 UTC revision 3 by dpavlin, Sun Aug 26 10:26:13 2007 UTC
# Line 27  my $board = << '_BOARD_'; Line 27  my $board = << '_BOARD_';
27  _BOARD_  _BOARD_
28    
29  my @board = map { split(//) } split(/\n/, $board);  my @board = map { split(//) } split(/\n/, $board);
30    my @trace;
31    
32  # line length  # line length
33  my $ll = 8 * 2 + 1;  my $ll = 8 * 2 + 1;
34    
35    my @step_name = ( qw/right down left up/ );
36    my @move_by = ( 1, $ll, -1, -$ll );
37    my $step = 0;   # right
38    
39    # offset 0, top-left corner
40    my $pos = 0;
41    
42    # unknown trace position
43    my $unknown = ' ';
44    
45  sub draw {  sub draw {
         my @board = @_;  
46          my $o = 0;          my $o = 0;
47          my $out;          my $out;
48          while ( $o < $#board ) {          while ( $o < $#board ) {
49                  $out .= join('', @board[ $o .. $o + $ll - 1 ]) . "\n";                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);
50                    $out .= '   ';
51                    $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
52                    $out .= "\n";
53                  $o += $ll;                  $o += $ll;
54          }          }
55          return $out;          return $out;
56  }  }
57    
 print $board, draw( @board );  
   
 my @step_name = ( qw/right down left up/ );  
 my @move_by = ( 1, $ll, -1, -$ll );  
 my $step = 0;   # right  
   
 # offset 0, top-left corner  
 my $pos = shift @ARGV || 0;  
   
 my @trace = ('x') x ( $#board + 1 );  
58  sub trace {  sub trace {
59          warn "trace $pos\n";          warn "## trace $pos\n";
60          $trace[ $pos ] = $board[ $pos ];          $trace[ $pos ] = $board[ $pos ];
61  }  }
62    
 warn draw( @trace );  
 trace;  
   
63  sub move {  sub move {
64          warn "move $step $step_name[$step]\n";          warn "move $step $step_name[$step]\n";
65          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
# Line 100  sub can_turn { Line 100  sub can_turn {
100          }          }
101  }  }
102    
103  while( 1 ) {  sub shape {
104    
105          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";  
106    
107          if ( $trace[ $next_pos ] ne 'x' ) {          $pos = $y * $ll * 2 + $x * 2;
108                  warn "position $next_pos re-visited, exiting\n";  
109                  last;          warn "<<< shape from $x,$y pos: $pos\n";
110          } elsif ( $board[ $next_pos ] =~ $ok_path ) {          @trace = ($unknown) x ( $#board + 1 );
111                  warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";          trace;
112                  move;  
113                  can_turn( $step + 1 );          my $len = 0;
114          } else {  
115                  warn "find line continuation from $step $step_name[$step]...\n";          while( 1 ) {
116                  can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";  
117                    my $next_pos = $pos + $move_by[ $step ];
118                    warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n";
119    
120                    if ( $trace[ $next_pos ] ne $unknown ) {
121                            warn "position $next_pos re-visited, exiting\n";
122                            last;
123                    } elsif ( $board[ $next_pos ] =~ $ok_path ) {
124                            warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";
125                            move;
126                            $len++;
127                            can_turn( $step + 1 );
128                    } else {
129                            warn "find line continuation from $step $step_name[$step]...\n";
130                            can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
131                    }
132                    warn draw( @trace );
133    
134                    print "WAIT> press enter"; my $foo = <STDIN>;
135          }          }
         warn draw( @trace );  
136    
137            warn ">>> ended at $pos, line length: $len\n";
138            return $len;
139  }  }
140    
141  warn "ended at $pos\n";  my $shapes = '0,0 1,0';
142    
143    foreach my $start ( split(/\s/,$shapes) ) {
144            my $len = shape( split(/,/,$start) );
145            warn "## $start has $len elements\n";
146    }

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

  ViewVC Help
Powered by ViewVC 1.1.26