--- symmetry.pl 2007/08/26 10:26:13 3 +++ symmetry.pl 2007/08/26 12:17:00 5 @@ -26,6 +26,8 @@ +-+-+-+-+-+-+-+-+ _BOARD_ +my $debug = shift @ARGV || 0; + my @board = map { split(//) } split(/\n/, $board); my @trace; @@ -42,6 +44,13 @@ # unknown trace position my $unknown = ' '; +# path traversed +my @directions; + +my @shapes_found; + +my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ ); + sub draw { my $o = 0; my $out; @@ -60,13 +69,31 @@ $trace[ $pos ] = $board[ $pos ]; } +my ( $tr_x, $tr_y ); + +sub pos_x_y { + my $y = int($pos / ($ll*2)); + my $x = int(($pos % $ll) / 2); + + $tr_x = $x unless defined $tr_x; + $tr_y = $y unless defined $tr_y; + + $tr_x = $x if $x > $tr_x && $y == $tr_y; + $tr_y = $y if $y < $tr_y && $x == $tr_x; + + warn "## pos_x_y $pos -> $x,$y\n"; + + return ($x,$y) if wantarray; + return "$x,$y"; +} + sub move { - warn "move $step $step_name[$step]\n"; $pos += $move_by[ $step ]; trace; $pos += $move_by[ $step ]; trace; - warn draw( @trace ); + push @directions, $step; + warn "move $step $step_name[$step] to ", scalar pos_x_y, "\n"; } sub follow { @@ -91,15 +118,25 @@ $trace[ $turn_pos ] = $old; if ( $board[ $turn_pos ] =~ $ok_path ) { - 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"; $step = $try_step; return 1; } else { - 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"; return 0; } } +sub show_directions { + return + join('', + map { + substr($step_name[$_],0,1) + } @directions + ) + ; +} + sub shape { my ($x,$y) = @_; @@ -108,6 +145,7 @@ warn "<<< shape from $x,$y pos: $pos\n"; @trace = ($unknown) x ( $#board + 1 ); + @directions = (); trace; my $len = 0; @@ -115,7 +153,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 +169,33 @@ } warn draw( @trace ); - print "WAIT> press enter"; my $foo = ; + if ( $debug ) { + warn "## tr: $tr_x,$tr_y\n"; + print "WAIT> press enter | ",show_directions; my $foo = ; + } } - warn ">>> ended at $pos, line length: $len\n"; + push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions }; + + warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n"; + + my $tr = "$tr_x,$tr_y"; + if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) { + print "INFO: added top-right $tr\n"; + push @shapes_start, $tr; + } + + print "WAIT> press enter"; my $foo = ; + return $len; } -my $shapes = '0,0 1,0'; - -foreach my $start ( split(/\s/,$shapes) ) { +foreach my $start ( @shapes_start ) { my $len = shape( split(/,/,$start) ); warn "## $start has $len elements\n"; } + +print ">>> RESULTS:\n"; +foreach my $r ( @shapes_found ) { + printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; +}