aboutsummaryrefslogtreecommitdiff
path: root/challenge-064
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-15 19:29:35 +0100
committerGitHub <noreply@github.com>2020-06-15 19:29:35 +0100
commit8100fccee05143d0e516ab57929fe20fab6045da (patch)
tree8d12570afdd3874b6eefb48d7aa2a23dc385db0a /challenge-064
parented5e60c0697b6ed2699e9f04093770db5b96bd2a (diff)
parent53ca9c7a0cf7283b0b53c2d3a156d115f84a0fbb (diff)
downloadperlweeklychallenge-club-8100fccee05143d0e516ab57929fe20fab6045da.tar.gz
perlweeklychallenge-club-8100fccee05143d0e516ab57929fe20fab6045da.tar.bz2
perlweeklychallenge-club-8100fccee05143d0e516ab57929fe20fab6045da.zip
Merge pull request #1829 from brtastic/challenge-64
Challenge 64 fix
Diffstat (limited to 'challenge-064')
-rw-r--r--challenge-064/brtastic/perl/ch-1.pl28
1 files changed, 20 insertions, 8 deletions
diff --git a/challenge-064/brtastic/perl/ch-1.pl b/challenge-064/brtastic/perl/ch-1.pl
index 86a05fdb36..1adbe867ff 100644
--- a/challenge-064/brtastic/perl/ch-1.pl
+++ b/challenge-064/brtastic/perl/ch-1.pl
@@ -1,12 +1,15 @@
use v5.24;
use warnings;
-use List::Util qw(min);
+use List::Util qw(reduce);
sub find_path
{
my ($matrix) = @_;
- my @pathfinder = reverse map { [reverse $_->@*] } $matrix->@*;
+ my @pathfinder = reverse map {
+ [reverse map { [$_, [$_]] } $_->@*]
+ } $matrix->@*;
+
foreach my $path_x (keys @pathfinder) {
foreach my $path_y (keys $pathfinder[$path_x]->@*) {
my @alternatives;
@@ -16,8 +19,10 @@ sub find_path
push @alternatives, $pathfinder[$path_x - 1][$path_y]
if $path_x - 1 >= 0;
- if (@alternatives > 0) {
- $pathfinder[$path_x][$path_y] += min @alternatives;
+ my $found = reduce { $a->[0] < $b->[0] ? $a : $b } @alternatives;
+ if (defined $found) {
+ $pathfinder[$path_x][$path_y][0] += $found->[0];
+ push $pathfinder[$path_x][$path_y][1]->@*, $found->[1]->@*;
}
}
}
@@ -33,27 +38,34 @@ my @data = (
[qw( 1 2 3 )],
[qw( 4 5 6 )],
[qw( 7 8 9 )],
- ], 21
+ ], [21, [1, 2, 3, 6, 9]]
],
[
[
[qw( 1 10 10 )],
[qw( 1 1 1 )],
[qw( 10 10 1 )],
- ], 5
+ ], [5, [1, 1, 1, 1, 1]]
],
[
[
[qw( 1 10 10 )],
[qw( 1 10 1 )],
[qw( 10 10 1 )],
- ], 14
+ ], [14, [1, 1, 10, 1, 1]]
+ ],
+ [
+ [
+ [qw( 1 10 10 1 )],
+ [qw( 1 3 1 2 )],
+ [qw( 10 10 1 5 )],
+ ], [12, [qw(1 1 3 1 1 5)]]
]
);
for (@data) {
my ($matrix, $output) = @$_;
- is find_path($matrix), $output, "path ok";
+ is_deeply find_path($matrix), $output, "path ok";
}
done_testing;