--- symmetry.pl 2007/08/26 14:45:26 7 +++ symmetry.pl 2007/08/31 14:23:21 10 @@ -119,6 +119,7 @@ my $ok_path = qr/[\|\-]/; my @corners; +my $corners_usage; sub can_turn { my $try_step = shift; @@ -135,10 +136,11 @@ $trace[ $turn_pos ] = $old; if ( $board[ $turn_pos ] =~ $ok_path ) { - my $xy = x_y($turn_pos); - warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos]) to $xy\n"; + my $xy = x_y($pos); + warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos]) from $xy\n"; $step = $try_step; push @corners, $xy; + $corners_usage->{$xy}++; 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])\n"; @@ -170,7 +172,14 @@ my $len = 0; $step = 0; - warn "<<< shape from $x,$y pos: $pos\n"; + my $usage = $corners_usage->{"$x,$y"} || 0; + + warn "<<< shape from $x,$y [usage: $usage] pos: $pos\n"; + + if ( $usage > 4 ) { + warn "SKIPPED, usage > 4\n"; + return; + } while( 1 ) { @@ -198,7 +207,7 @@ push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions }; - warn "### corners: ",join(' ', @corners),"\n"; + warn "### corners: ",join(' ', map { $_ . " [" . $corners_usage->{$_} . "]" } @corners),"\n"; warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n"; foreach my $c ( @corners ) { @@ -223,7 +232,20 @@ } } +sub is_symmetric { + my $path = shift || die "no path?"; + + my $h = length($path)/2; + return 0 if int($h) != $h; + my ($l,$r) = ( substr($path,0,$h), substr($path,$h) ); + $r =~ tr/lrud/rldu/; +# warn "$l -- $r\n"; + return 0 unless $l eq $r; + return 1; +} + print ">>> RESULTS:\n"; foreach my $r ( @shapes_found ) { - printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; + printf "%2d,%-2d len: %-4d directions: %s %s\n", + $r->{x}, $r->{y}, $r->{len}, $r->{directions}, is_symmetric($r->{directions}) ? 'symetric' : ''; }