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; |
my @trace; |
33 |
|
|
44 |
# unknown trace position |
# unknown trace position |
45 |
my $unknown = ' '; |
my $unknown = ' '; |
46 |
|
|
47 |
|
# path traversed |
48 |
|
my @directions; |
49 |
|
|
50 |
|
my @found; |
51 |
|
|
52 |
sub draw { |
sub draw { |
53 |
my $o = 0; |
my $o = 0; |
54 |
my $out; |
my $out; |
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 |
|
sub show_directions { |
111 |
|
return |
112 |
|
join('', |
113 |
|
map { |
114 |
|
substr($step_name[$_],0,1) |
115 |
|
} @directions |
116 |
|
) |
117 |
|
; |
118 |
|
} |
119 |
|
|
120 |
sub shape { |
sub shape { |
121 |
|
|
122 |
my ($x,$y) = @_; |
my ($x,$y) = @_; |
125 |
|
|
126 |
warn "<<< shape from $x,$y pos: $pos\n"; |
warn "<<< shape from $x,$y pos: $pos\n"; |
127 |
@trace = ($unknown) x ( $#board + 1 ); |
@trace = ($unknown) x ( $#board + 1 ); |
128 |
|
@directions = (); |
129 |
trace; |
trace; |
130 |
|
|
131 |
my $len = 0; |
my $len = 0; |
133 |
while( 1 ) { |
while( 1 ) { |
134 |
|
|
135 |
my $next_pos = $pos + $move_by[ $step ]; |
my $next_pos = $pos + $move_by[ $step ]; |
136 |
warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n"; |
warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n"; |
137 |
|
|
138 |
if ( $trace[ $next_pos ] ne $unknown ) { |
if ( $trace[ $next_pos ] ne $unknown ) { |
139 |
warn "position $next_pos re-visited, exiting\n"; |
warn "position $next_pos re-visited, exiting\n"; |
149 |
} |
} |
150 |
warn draw( @trace ); |
warn draw( @trace ); |
151 |
|
|
152 |
print "WAIT> press enter"; my $foo = <STDIN>; |
if ( $debug ) { |
153 |
|
print "WAIT> press enter | ",show_directions; my $foo = <STDIN>; |
154 |
|
} |
155 |
} |
} |
156 |
|
|
157 |
warn ">>> ended at $pos, line length: $len\n"; |
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; |
return $len; |
163 |
} |
} |
164 |
|
|
165 |
my $shapes = '0,0 1,0'; |
my $shapes = '0,0 1,0 0,3'; |
166 |
|
|
167 |
foreach my $start ( split(/\s/,$shapes) ) { |
foreach my $start ( split(/\s/,$shapes) ) { |
168 |
my $len = shape( split(/,/,$start) ); |
my $len = shape( split(/,/,$start) ); |
169 |
warn "## $start has $len elements\n"; |
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 |
|
} |