26 |
+-+-+-+-+-+-+-+-+ |
+-+-+-+-+-+-+-+-+ |
27 |
_BOARD_ |
_BOARD_ |
28 |
|
|
29 |
|
my $debug = shift @ARGV || 0; |
30 |
|
|
31 |
my @board = map { split(//) } split(/\n/, $board); |
my @board = map { split(//) } split(/\n/, $board); |
32 |
|
my @trace; |
33 |
|
|
34 |
# line length |
# line length |
35 |
my $ll = 8 * 2 + 1; |
my $ll = 8 * 2 + 1; |
36 |
|
|
37 |
|
my @step_name = ( qw/right down left up/ ); |
38 |
|
my @move_by = ( 1, $ll, -1, -$ll ); |
39 |
|
my $step = 0; # right |
40 |
|
|
41 |
|
# offset 0, top-left corner |
42 |
|
my $pos = 0; |
43 |
|
|
44 |
|
# unknown trace position |
45 |
|
my $unknown = ' '; |
46 |
|
|
47 |
|
# path traversed |
48 |
|
my @directions; |
49 |
|
|
50 |
|
my @found; |
51 |
|
|
52 |
sub draw { |
sub draw { |
|
my @board = @_; |
|
53 |
my $o = 0; |
my $o = 0; |
54 |
my $out; |
my $out; |
55 |
while ( $o < $#board ) { |
while ( $o < $#board ) { |
56 |
$out .= join('', @board[ $o .. $o + $ll - 1 ]) . "\n"; |
$out .= join('', @board[ $o .. $o + $ll - 1 ]); |
57 |
|
$out .= ' '; |
58 |
|
$out .= join('', @trace[ $o .. $o + $ll - 1 ]); |
59 |
|
$out .= "\n"; |
60 |
$o += $ll; |
$o += $ll; |
61 |
} |
} |
62 |
return $out; |
return $out; |
63 |
} |
} |
64 |
|
|
|
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 ); |
|
65 |
sub trace { |
sub trace { |
66 |
warn "trace $pos\n"; |
warn "## trace $pos\n"; |
67 |
$trace[ $pos ] = $board[ $pos ]; |
$trace[ $pos ] = $board[ $pos ]; |
68 |
} |
} |
69 |
|
|
|
warn draw( @trace ); |
|
|
trace; |
|
|
|
|
70 |
sub move { |
sub move { |
71 |
warn "move $step $step_name[$step]\n"; |
warn "move $step $step_name[$step]\n"; |
72 |
$pos += $move_by[ $step ]; |
$pos += $move_by[ $step ]; |
73 |
trace; |
trace; |
74 |
$pos += $move_by[ $step ]; |
$pos += $move_by[ $step ]; |
75 |
trace; |
trace; |
76 |
warn draw( @trace ); |
push @directions, $step; |
77 |
} |
} |
78 |
|
|
79 |
sub follow { |
sub follow { |
107 |
} |
} |
108 |
} |
} |
109 |
|
|
110 |
while( 1 ) { |
sub show_directions { |
111 |
|
return |
112 |
|
join('', |
113 |
|
map { |
114 |
|
substr($step_name[$_],0,1) |
115 |
|
} @directions |
116 |
|
) |
117 |
|
; |
118 |
|
} |
119 |
|
|
120 |
my $next_pos = $pos + $move_by[ $step ]; |
sub shape { |
|
warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n"; |
|
121 |
|
|
122 |
if ( $trace[ $next_pos ] ne 'x' ) { |
my ($x,$y) = @_; |
123 |
warn "position $next_pos re-visited, exiting\n"; |
|
124 |
last; |
$pos = $y * $ll * 2 + $x * 2; |
125 |
} elsif ( $board[ $next_pos ] =~ $ok_path ) { |
|
126 |
warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n"; |
warn "<<< shape from $x,$y pos: $pos\n"; |
127 |
move; |
@trace = ($unknown) x ( $#board + 1 ); |
128 |
can_turn( $step + 1 ); |
@directions = (); |
129 |
} else { |
trace; |
130 |
warn "find line continuation from $step $step_name[$step]...\n"; |
|
131 |
can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; |
my $len = 0; |
132 |
|
|
133 |
|
while( 1 ) { |
134 |
|
|
135 |
|
my $next_pos = $pos + $move_by[ $step ]; |
136 |
|
warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n"; |
137 |
|
|
138 |
|
if ( $trace[ $next_pos ] ne $unknown ) { |
139 |
|
warn "position $next_pos re-visited, exiting\n"; |
140 |
|
last; |
141 |
|
} elsif ( $board[ $next_pos ] =~ $ok_path ) { |
142 |
|
warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n"; |
143 |
|
move; |
144 |
|
$len++; |
145 |
|
can_turn( $step + 1 ); |
146 |
|
} else { |
147 |
|
warn "find line continuation from $step $step_name[$step]...\n"; |
148 |
|
can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; |
149 |
|
} |
150 |
|
warn draw( @trace ); |
151 |
|
|
152 |
|
if ( $debug ) { |
153 |
|
print "WAIT> press enter | ",show_directions; my $foo = <STDIN>; |
154 |
|
} |
155 |
} |
} |
|
warn draw( @trace ); |
|
156 |
|
|
157 |
|
push @found, { x => $x, y => $y, len => $len, directions => show_directions }; |
158 |
|
|
159 |
|
warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n"; |
160 |
|
print "WAIT> press enter"; my $foo = <STDIN>; |
161 |
|
|
162 |
|
return $len; |
163 |
} |
} |
164 |
|
|
165 |
warn "ended at $pos\n"; |
my $shapes = '0,0 1,0 0,3'; |
166 |
|
|
167 |
|
foreach my $start ( split(/\s/,$shapes) ) { |
168 |
|
my $len = shape( split(/,/,$start) ); |
169 |
|
warn "## $start has $len elements\n"; |
170 |
|
} |
171 |
|
|
172 |
|
print ">>> RESULTS:\n"; |
173 |
|
foreach my $r ( @found ) { |
174 |
|
printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; |
175 |
|
} |