I'm a software engineer, get me out of here: Difference between revisions
I'm a software engineer, get me out of here (view source)
Revision as of 16:05, 24 January 2021
, 3 years ago→{{header|Perl}}: a little more fiddling
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: a little less terse) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: a little more fiddling) |
||
Line 787:
my $w = 0;
my $d = join '', <DATA>
length > $w and $w = length for split "\n", $d;
$d =~ s/.+/ sprintf "%-${w}s", $& /ge; # padding for single number addressing
Line 796:
my @directions = ( 1, -1, -$w-1 .. -$w+1, $w-1 .. $w+1 );
my $startpos = from_xy 11, 11;▼
my @nodes;
push @nodes, $-[0] while $d =~ /\d/g;
my %dist = map { $_ => all_destinations($_) } @nodes; # all shortest from-to paths
sub all_destinations▼
{▼
my @queue = shift;▼
my $dd = $d;▼
my %to;▼
while( my $path = shift @queue )▼
{▼
my $from = (split ' ', $path)[-1];▼
my $steps = substr $dd, $from, 1;▼
' ' eq $steps and next;▼
$to{$from} //= $path;▼
$steps or next;▼
substr $dd, $from, 1, '0';▼
for my $dir ( @directions )▼
{▼
my $next = $from + $steps * $dir;▼
next if $next < 0 or $next > length $dd;
(substr $dd, $next, 1) =~ /\d/ and push @queue, "$path $next";▼
}▼
}▼
return \%to;▼
}▼
▲my $startpos = from_xy 11, 11;
my @best;
Line 827 ⟶ 851:
print "\nlongest reinforcement from HQ is $#best for:\n",
$best[-1] =~ s/\d+/ to_xy $& /ger;
▲sub all_destinations
▲ {
▲ my @queue = shift;
▲ my $dd = $d;
▲ my %to;
▲ while( my $path = shift @queue )
▲ {
▲ my $from = (split ' ', $path)[-1];
▲ my $steps = substr $dd, $from, 1;
▲ ' ' eq $steps and next;
▲ $to{$from} //= $path;
▲ $steps or next;
▲ substr $dd, $from, 1, 0;
▲ for my $dir ( @directions )
▲ {
▲ my $next = $from + $steps * $dir;
▲ (substr $dd, $next, 1) =~ /\d/ and push @queue, "$path $next";
▲ }
▲ }
▲ return \%to;
▲ }
__DATA__
Line 873 ⟶ 875:
000122111322000
00001120000
00000</lang>
{{out}}
<pre>shortest escape routes (40) of length 4:▼
▲shortest escape routes (40) of length 4:
(11,11) (11,12) (8,12) (6,12) (0,12)
(11,11) (10,11) (7,11) (7,12) (1,6)
Line 942:
(11,11) (12,11) (9,14) (6,17) (4,17) (4,18) (3,19)
(11,11) (11,12) (14,12) (16,12) (20,12) (21,11) (22,12)
(11,11) (11,12) (14,15) (16,17) (17,16) (18,16) (20,14)</pre>
=={{header|Phix}}==
|