--- symmetry.pl 2007/08/26 02:53:41 1 +++ symmetry.pl 2007/08/26 09:20:07 2 @@ -6,8 +6,6 @@ use strict; -use Data::Dump qw/dump/; - my $board = << '_BOARD_'; +-+-+-+-+-+-+-+-+ | | |o| | | @@ -51,9 +49,7 @@ my $step = 0; # right # offset 0, top-left corner -my $pos = 0; -$pos = 2; -$pos = 6; +my $pos = shift @ARGV || 0; my @trace = ('x') x ( $#board + 1 ); sub trace { @@ -82,10 +78,12 @@ my $ok_path = qr/[\|\-]/; sub can_turn { - my $step = shift; - die "no step?" unless defined $step; + my $try_step = shift; + die "no step?" unless defined $try_step; + + $try_step %= 4; - my $turn_pos = $pos + $move_by[ $step % 4 ]; + my $turn_pos = $pos + $move_by[$try_step]; my $old = $trace[ $turn_pos ]; $trace[ $turn_pos ] = '?'; @@ -93,10 +91,11 @@ $trace[ $turn_pos ] = $old; if ( $board[ $turn_pos ] =~ $ok_path ) { - warn "OK can_turn $step_name[$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])"; + $step = $try_step; return 1; } else { - warn "NOPE can_turn $step_name[$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])"; return 0; } } @@ -112,17 +111,10 @@ } elsif ( $board[ $next_pos ] =~ $ok_path ) { warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n"; move; - follow( $step+1 ) if can_turn( $step+1 ); + can_turn( $step + 1 ); } else { warn "find line continuation from $step $step_name[$step]...\n"; - foreach my $o ( -1, 1 ) { - if ( can_turn( $step + $o ) ) { - $step = $step+$o; - warn "new direction: $step $step_name[$step]\n"; - follow( $step ); - last; - } - } + can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; } warn draw( @trace );