--- symmetry.pl 2007/08/26 10:26:13 3 +++ symmetry.pl 2007/08/26 10:59:01 4 @@ -26,6 +26,8 @@ +-+-+-+-+-+-+-+-+ _BOARD_ +my $debug = shift @ARGV || 0; + my @board = map { split(//) } split(/\n/, $board); my @trace; @@ -42,6 +44,11 @@ # unknown trace position my $unknown = ' '; +# path traversed +my @directions; + +my @found; + sub draw { my $o = 0; my $out; @@ -66,7 +73,7 @@ trace; $pos += $move_by[ $step ]; trace; - warn draw( @trace ); + push @directions, $step; } sub follow { @@ -100,6 +107,16 @@ } } +sub show_directions { + return + join('', + map { + substr($step_name[$_],0,1) + } @directions + ) + ; +} + sub shape { my ($x,$y) = @_; @@ -108,6 +125,7 @@ warn "<<< shape from $x,$y pos: $pos\n"; @trace = ($unknown) x ( $#board + 1 ); + @directions = (); trace; my $len = 0; @@ -115,7 +133,7 @@ while( 1 ) { my $next_pos = $pos + $move_by[ $step ]; - 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"; if ( $trace[ $next_pos ] ne $unknown ) { warn "position $next_pos re-visited, exiting\n"; @@ -131,16 +149,27 @@ } warn draw( @trace ); - print "WAIT> press enter"; my $foo = ; + if ( $debug ) { + print "WAIT> press enter | ",show_directions; my $foo = ; + } } - warn ">>> ended at $pos, line length: $len\n"; + push @found, { x => $x, y => $y, len => $len, directions => show_directions }; + + warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n"; + print "WAIT> press enter"; my $foo = ; + return $len; } -my $shapes = '0,0 1,0'; +my $shapes = '0,0 1,0 0,3'; foreach my $start ( split(/\s/,$shapes) ) { my $len = shape( split(/,/,$start) ); warn "## $start has $len elements\n"; } + +print ">>> RESULTS:\n"; +foreach my $r ( @found ) { + printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; +}