--- symmetry.pl 2007/08/26 09:20:07 2 +++ symmetry.pl 2007/08/26 10:26:13 3 @@ -27,39 +27,39 @@ _BOARD_ my @board = map { split(//) } split(/\n/, \$board); +my @trace; # line length my \$ll = 8 * 2 + 1; +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 = 0; + +# unknown trace position +my \$unknown = ' '; + sub draw { - my @board = @_; my \$o = 0; my \$out; while ( \$o < \$#board ) { - \$out .= join('', @board[ \$o .. \$o + \$ll - 1 ]) . "\n"; + \$out .= join('', @board[ \$o .. \$o + \$ll - 1 ]); + \$out .= ' '; + \$out .= join('', @trace[ \$o .. \$o + \$ll - 1 ]); + \$out .= "\n"; \$o += \$ll; } return \$out; } -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 ); sub trace { - warn "trace \$pos\n"; + warn "## trace \$pos\n"; \$trace[ \$pos ] = \$board[ \$pos ]; } -warn draw( @trace ); -trace; - sub move { warn "move \$step \$step_name[\$step]\n"; \$pos += \$move_by[ \$step ]; @@ -100,25 +100,47 @@ } } -while( 1 ) { +sub shape { - my \$next_pos = \$pos + \$move_by[ \$step ]; - warn "in loop - pos = \$pos next_pos = \$next_pos step = \$step \$step_name[\$step]\n"; + my (\$x,\$y) = @_; - if ( \$trace[ \$next_pos ] ne 'x' ) { - warn "position \$next_pos re-visited, exiting\n"; - last; - } elsif ( \$board[ \$next_pos ] =~ \$ok_path ) { - warn "OK next_pos = \$next_pos b(\$board[\$next_pos]) t(\$trace[\$next_pos])\n"; - move; - can_turn( \$step + 1 ); - } else { - warn "find line continuation from \$step \$step_name[\$step]...\n"; - can_turn( \$step - 1 ) || can_turn( \$step + 1 ) || die "can't find new direction"; + \$pos = \$y * \$ll * 2 + \$x * 2; + + warn "<<< shape from \$x,\$y pos: \$pos\n"; + @trace = (\$unknown) x ( \$#board + 1 ); + trace; + + my \$len = 0; + + while( 1 ) { + + my \$next_pos = \$pos + \$move_by[ \$step ]; + warn "in loop - pos = \$pos next_pos = \$next_pos step = \$step \$step_name[\$step]\n"; + + if ( \$trace[ \$next_pos ] ne \$unknown ) { + warn "position \$next_pos re-visited, exiting\n"; + last; + } elsif ( \$board[ \$next_pos ] =~ \$ok_path ) { + warn "OK next_pos = \$next_pos b(\$board[\$next_pos]) t(\$trace[\$next_pos])\n"; + move; + \$len++; + can_turn( \$step + 1 ); + } else { + 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 ); + + print "WAIT> press enter"; my \$foo = ; } - warn draw( @trace ); + warn ">>> ended at \$pos, line length: \$len\n"; + return \$len; } -warn "ended at \$pos\n"; +my \$shapes = '0,0 1,0'; +foreach my \$start ( split(/\s/,\$shapes) ) { + my \$len = shape( split(/,/,\$start) ); + warn "## \$start has \$len elements\n"; +}