30 |
|
|
31 |
my @board = map { split(//) } split(/\n/, $board); |
my @board = map { split(//) } split(/\n/, $board); |
32 |
my @trace; |
my @trace; |
33 |
|
my @visited = (' ') x ($#board + 1); |
34 |
|
|
35 |
# line length |
# line length |
36 |
my $ll = 8 * 2 + 1; |
my $ll = 8 * 2 + 1; |
48 |
# path traversed |
# path traversed |
49 |
my @directions; |
my @directions; |
50 |
|
|
51 |
my @found; |
my @shapes_found; |
52 |
|
|
53 |
|
my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ ); |
54 |
|
|
55 |
sub draw { |
sub draw { |
56 |
my $o = 0; |
my $o = 0; |
57 |
my $out; |
my $out = "\n " . join('', |
58 |
|
('0 1 2 3 4 5 6 7 8 | ') x 3 |
59 |
|
) . "\n"; |
60 |
while ( $o < $#board ) { |
while ( $o < $#board ) { |
61 |
|
my $l = '|'; |
62 |
|
if ( $o % ($ll*2) == 0) { |
63 |
|
my $y = int($o / ($ll*2)); |
64 |
|
$out .= "$y "; |
65 |
|
$l = $y; |
66 |
|
} else { |
67 |
|
$out .= " "; |
68 |
|
} |
69 |
|
|
70 |
$out .= join('', @board[ $o .. $o + $ll - 1 ]); |
$out .= join('', @board[ $o .. $o + $ll - 1 ]); |
71 |
$out .= ' '; |
$out .= " $l "; |
72 |
$out .= join('', @trace[ $o .. $o + $ll - 1 ]); |
$out .= join('', @trace[ $o .. $o + $ll - 1 ]); |
73 |
$out .= "\n"; |
$out .= " $l "; |
74 |
|
$out .= join('', @visited[ $o .. $o + $ll - 1 ]); |
75 |
|
$out .= " $l\n"; |
76 |
$o += $ll; |
$o += $ll; |
77 |
} |
} |
78 |
|
|
79 |
|
$out .= "\n"; |
80 |
|
|
81 |
return $out; |
return $out; |
82 |
} |
} |
83 |
|
|
84 |
sub trace { |
sub trace { |
85 |
warn "## trace $pos\n"; |
warn "## trace $pos\n"; |
86 |
$trace[ $pos ] = $board[ $pos ]; |
$trace[ $pos ] = $board[ $pos ]; |
87 |
|
$visited[$pos]++; |
88 |
|
} |
89 |
|
|
90 |
|
my ( $tr_x, $tr_y ); |
91 |
|
my ( $bl_x, $bl_y ); |
92 |
|
|
93 |
|
sub x_y { |
94 |
|
my $p = shift; |
95 |
|
|
96 |
|
my $update = 0; |
97 |
|
if ( ! defined( $p ) ) { |
98 |
|
$p = $pos; |
99 |
|
$update = 1; |
100 |
|
} |
101 |
|
|
102 |
|
my $y = int($p / ($ll*2)); |
103 |
|
my $x = int(($p % $ll) / 2); |
104 |
|
|
105 |
|
warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n"; |
106 |
|
|
107 |
|
if ( $update ) { |
108 |
|
|
109 |
|
# $tr_x = $x if $x > $tr_x && $y == $tr_y; |
110 |
|
# $tr_y = $y if $y < $tr_y && $x == $tr_x; |
111 |
|
|
112 |
|
if ( |
113 |
|
$y < $tr_y |
114 |
|
|| |
115 |
|
$y <= $tr_y && $x > $tr_x |
116 |
|
) { |
117 |
|
( $tr_x, $tr_y ) = ( $x, $y ); |
118 |
|
warn "## UPDATED tr: $tr_x,$tr_y\n"; |
119 |
|
} |
120 |
|
|
121 |
|
if ( |
122 |
|
$x < $bl_x |
123 |
|
|| |
124 |
|
$y > $bl_y |
125 |
|
) { |
126 |
|
( $bl_x, $bl_y ) = ( $x, $y ); |
127 |
|
warn "## UPDATED bl: $bl_x,$bl_y\n"; |
128 |
|
} |
129 |
|
|
130 |
|
# $bl_x = $x if $x < $bl_x; # && $y == $bl_y; |
131 |
|
# $bl_y = $y if $y > $bl_y; # && $x == $bl_x; |
132 |
|
|
133 |
|
} |
134 |
|
|
135 |
|
warn "## x_y($p) -> $x,$y ", |
136 |
|
$update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '', |
137 |
|
"\n"; |
138 |
|
|
139 |
|
# return ($x,$y) if wantarray; |
140 |
|
return "$x,$y"; |
141 |
} |
} |
142 |
|
|
143 |
sub move { |
sub move { |
|
warn "move $step $step_name[$step]\n"; |
|
144 |
$pos += $move_by[ $step ]; |
$pos += $move_by[ $step ]; |
145 |
trace; |
trace; |
146 |
$pos += $move_by[ $step ]; |
$pos += $move_by[ $step ]; |
147 |
trace; |
trace; |
148 |
push @directions, $step; |
push @directions, $step; |
149 |
|
warn "move $step $step_name[$step] to ", x_y, "\n"; |
150 |
} |
} |
151 |
|
|
152 |
sub follow { |
sub follow { |
167 |
|
|
168 |
my $old = $trace[ $turn_pos ]; |
my $old = $trace[ $turn_pos ]; |
169 |
$trace[ $turn_pos ] = '?'; |
$trace[ $turn_pos ] = '?'; |
170 |
warn "TEST\n",draw( @trace ); |
$trace[ $pos ] = '*'; |
171 |
|
warn "TEST ", x_y($turn_pos), "\n",draw; |
172 |
$trace[ $turn_pos ] = $old; |
$trace[ $turn_pos ] = $old; |
173 |
|
|
174 |
if ( $board[ $turn_pos ] =~ $ok_path ) { |
if ( $board[ $turn_pos ] =~ $ok_path ) { |
175 |
warn "OK can_turn $try_step $step_name[$try_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])\n"; |
176 |
$step = $try_step; |
$step = $try_step; |
177 |
return 1; |
return 1; |
178 |
} else { |
} else { |
179 |
warn "NOPE can_turn $try_step $step_name[$try_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])\n"; |
180 |
return 0; |
return 0; |
181 |
} |
} |
182 |
} |
} |
197 |
|
|
198 |
$pos = $y * $ll * 2 + $x * 2; |
$pos = $y * $ll * 2 + $x * 2; |
199 |
|
|
|
warn "<<< shape from $x,$y pos: $pos\n"; |
|
200 |
@trace = ($unknown) x ( $#board + 1 ); |
@trace = ($unknown) x ( $#board + 1 ); |
201 |
@directions = (); |
@directions = (); |
202 |
trace; |
trace; |
203 |
|
|
204 |
my $len = 0; |
my $len = 0; |
205 |
|
( $tr_x, $tr_y ) = ( $x,$y ); |
206 |
|
( $bl_x, $bl_y ) = ( $x,$y ); |
207 |
|
$step = 0; |
208 |
|
|
209 |
|
if ( $visited[$pos] > 3 ) { |
210 |
|
warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n"; |
211 |
|
return; |
212 |
|
} |
213 |
|
|
214 |
|
warn "<<< shape from $x,$y pos: $pos\n"; |
215 |
|
|
216 |
while( 1 ) { |
while( 1 ) { |
217 |
|
|
230 |
warn "find line continuation from $step $step_name[$step]...\n"; |
warn "find line continuation from $step $step_name[$step]...\n"; |
231 |
can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; |
can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction"; |
232 |
} |
} |
233 |
warn draw( @trace ); |
warn draw; |
234 |
|
|
235 |
|
warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n"; |
236 |
|
|
237 |
if ( $debug ) { |
if ( $debug ) { |
238 |
print "WAIT> press enter | ",show_directions; my $foo = <STDIN>; |
print "WAIT> press enter | ",show_directions; my $foo = <STDIN>; |
239 |
} |
} |
240 |
} |
} |
241 |
|
|
242 |
push @found, { x => $x, y => $y, len => $len, directions => show_directions }; |
push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions }; |
243 |
|
|
244 |
|
my $tr = "$tr_x,$tr_y"; |
245 |
|
my $bl = "$bl_x,$bl_y"; |
246 |
|
|
247 |
|
warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n"; |
248 |
|
|
249 |
|
if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) { |
250 |
|
warn "INFO: added $tr top-right\n"; |
251 |
|
push @shapes_start, $tr; |
252 |
|
} |
253 |
|
if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) { |
254 |
|
warn "INFO: added $bl bottom-left\n"; |
255 |
|
push @shapes_start, $bl; |
256 |
|
} |
257 |
|
|
|
warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n"; |
|
258 |
print "WAIT> press enter"; my $foo = <STDIN>; |
print "WAIT> press enter"; my $foo = <STDIN>; |
259 |
|
|
260 |
return $len; |
return $len; |
261 |
} |
} |
262 |
|
|
263 |
my $shapes = '0,0 1,0 0,3'; |
foreach my $start ( @shapes_start ) { |
|
|
|
|
foreach my $start ( split(/\s/,$shapes) ) { |
|
264 |
my $len = shape( split(/,/,$start) ); |
my $len = shape( split(/,/,$start) ); |
265 |
warn "## $start has $len elements\n"; |
warn "## $start has $len elements\n"; |
266 |
} |
} |
267 |
|
|
268 |
print ">>> RESULTS:\n"; |
print ">>> RESULTS:\n"; |
269 |
foreach my $r ( @found ) { |
foreach my $r ( @shapes_found ) { |
270 |
printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; |
printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions}; |
271 |
} |
} |