--- symmetry.pl 2007/08/26 12:17:00 5 +++ symmetry.pl 2007/08/26 14:15:26 6 @@ -30,6 +30,7 @@ my @board = map { split(//) } split(/\n/, $board); my @trace; +my @visited = (' ') x ($#board + 1); # line length my $ll = 8 * 2 + 1; @@ -53,37 +54,89 @@ sub draw { my $o = 0; - my $out; + my $out = "\n " . join('', + ('0 1 2 3 4 5 6 7 8 | ') x 3 + ) . "\n"; while ( $o < $#board ) { + my $l = '|'; + if ( $o % ($ll*2) == 0) { + my $y = int($o / ($ll*2)); + $out .= "$y "; + $l = $y; + } else { + $out .= " "; + } + $out .= join('', @board[ $o .. $o + $ll - 1 ]); - $out .= ' '; + $out .= " $l "; $out .= join('', @trace[ $o .. $o + $ll - 1 ]); - $out .= "\n"; + $out .= " $l "; + $out .= join('', @visited[ $o .. $o + $ll - 1 ]); + $out .= " $l\n"; $o += $ll; } + + $out .= "\n"; + return $out; } sub trace { warn "## trace $pos\n"; $trace[ $pos ] = $board[ $pos ]; + $visited[$pos]++; } my ( $tr_x, $tr_y ); +my ( $bl_x, $bl_y ); + +sub x_y { + my $p = shift; -sub pos_x_y { - my $y = int($pos / ($ll*2)); - my $x = int(($pos % $ll) / 2); + my $update = 0; + if ( ! defined( $p ) ) { + $p = $pos; + $update = 1; + } + + my $y = int($p / ($ll*2)); + my $x = int(($p % $ll) / 2); + + warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n"; - $tr_x = $x unless defined $tr_x; - $tr_y = $y unless defined $tr_y; + if ( $update ) { - $tr_x = $x if $x > $tr_x && $y == $tr_y; - $tr_y = $y if $y < $tr_y && $x == $tr_x; +# $tr_x = $x if $x > $tr_x && $y == $tr_y; +# $tr_y = $y if $y < $tr_y && $x == $tr_x; + + if ( + $y < $tr_y + || + $y <= $tr_y && $x > $tr_x + ) { + ( $tr_x, $tr_y ) = ( $x, $y ); + warn "## UPDATED tr: $tr_x,$tr_y\n"; + } - warn "## pos_x_y $pos -> $x,$y\n"; + if ( + $x < $bl_x + || + $y > $bl_y + ) { + ( $bl_x, $bl_y ) = ( $x, $y ); + warn "## UPDATED bl: $bl_x,$bl_y\n"; + } - return ($x,$y) if wantarray; +# $bl_x = $x if $x < $bl_x; # && $y == $bl_y; +# $bl_y = $y if $y > $bl_y; # && $x == $bl_x; + + } + + warn "## x_y($p) -> $x,$y ", + $update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '', + "\n"; + +# return ($x,$y) if wantarray; return "$x,$y"; } @@ -93,7 +146,7 @@ $pos += $move_by[ $step ]; trace; push @directions, $step; - warn "move $step $step_name[$step] to ", scalar pos_x_y, "\n"; + warn "move $step $step_name[$step] to ", x_y, "\n"; } sub follow { @@ -114,7 +167,8 @@ my $old = $trace[ $turn_pos ]; $trace[ $turn_pos ] = '?'; - warn "TEST\n",draw( @trace ); + $trace[ $pos ] = '*'; + warn "TEST ", x_y($turn_pos), "\n",draw; $trace[ $turn_pos ] = $old; if ( $board[ $turn_pos ] =~ $ok_path ) { @@ -143,12 +197,21 @@ $pos = $y * $ll * 2 + $x * 2; - warn "<<< shape from $x,$y pos: $pos\n"; @trace = ($unknown) x ( $#board + 1 ); @directions = (); trace; my $len = 0; + ( $tr_x, $tr_y ) = ( $x,$y ); + ( $bl_x, $bl_y ) = ( $x,$y ); + $step = 0; + + if ( $visited[$pos] > 3 ) { + warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n"; + return; + } + + warn "<<< shape from $x,$y pos: $pos\n"; while( 1 ) { @@ -167,23 +230,30 @@ warn "find line continuation from $step $step_name[$step]...\n"; can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; } - warn draw( @trace ); + warn draw; + + warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n"; if ( $debug ) { - warn "## tr: $tr_x,$tr_y\n"; print "WAIT> press enter | ",show_directions; my $foo = ; } } 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"; + my $bl = "$bl_x,$bl_y"; + + warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n"; + if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) { - print "INFO: added top-right $tr\n"; + warn "INFO: added $tr top-right\n"; push @shapes_start, $tr; } + if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) { + warn "INFO: added $bl bottom-left\n"; + push @shapes_start, $bl; + } print "WAIT> press enter"; my $foo = ;