aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-08 14:56:18 +0100
committerGitHub <noreply@github.com>2020-06-08 14:56:18 +0100
commiteaba1cb5e9ea3bd4d95159663f625960849477b6 (patch)
tree5f80127a7c6a33dd6e09c78b3a807b5e8981ad9f
parente718cae482ded10d118e5598d0ed77ace632f8cc (diff)
parent31730009a636f5b2646aa5c8be60679a081f4fc1 (diff)
downloadperlweeklychallenge-club-eaba1cb5e9ea3bd4d95159663f625960849477b6.tar.gz
perlweeklychallenge-club-eaba1cb5e9ea3bd4d95159663f625960849477b6.tar.bz2
perlweeklychallenge-club-eaba1cb5e9ea3bd4d95159663f625960849477b6.zip
Merge pull request #1802 from Firedrake/rogerbw-challenge-064
Solutions to challenge #64.
-rwxr-xr-xchallenge-064/roger-bell-west/perl/ch-1.pl40
-rwxr-xr-xchallenge-064/roger-bell-west/perl/ch-2.pl45
-rwxr-xr-xchallenge-064/roger-bell-west/raku/ch-1.p634
-rwxr-xr-xchallenge-064/roger-bell-west/raku/ch-2.p646
4 files changed, 165 insertions, 0 deletions
diff --git a/challenge-064/roger-bell-west/perl/ch-1.pl b/challenge-064/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..c8dc9b03f7
--- /dev/null
+++ b/challenge-064/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use List::Util qw(sum);
+use utf8;
+
+my @in=(
+ [qw(1 2 3)],
+ [qw(4 5 6)],
+ [qw(7 8 9)],
+ );
+
+my @l=([0,0]);
+
+my $s;
+my @p;
+
+while (@l) {
+ my $t=shift @l;
+ my @xy=splice @{$t},0,2;
+ push @{$t},$in[$xy[0]][$xy[1]];
+ if ($t->[-1] == $in[-1][-1]) {
+ my $sa=sum(@{$t});
+ if (!defined $s || $sa < $s) {
+ $s=$sa;
+ @p=@{$t};
+ }
+ }
+ if ($xy[0] < $#in && $xy[1] <= $#{$in[$xy[0]+1]}) {
+ push @l,[$xy[0]+1,$xy[1],@{$t}];
+ }
+ if ($xy[1] < $#{$in[$xy[0]]}) {
+ push @l,[$xy[0],$xy[1]+1,@{$t}];
+ }
+}
+
+binmode STDOUT,':utf8';
+print "$s ( ".join(' → ',@p)." )\n";
diff --git a/challenge-064/roger-bell-west/perl/ch-2.pl b/challenge-064/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..b52abc4db7
--- /dev/null
+++ b/challenge-064/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+my $S="perlweeklychallenge";
+my @W = ("weekly", "challenge", "perl");
+
+#$S="perlandraku";
+#@W = (qw(python ruby haskell));
+
+my $sl=length($S);
+
+my @l;
+my $done=0;
+
+while (!$done) {
+ my $c=[];
+ if (@l) {
+ $c=shift @l;
+ }
+ my $cc=join('',@{$c});
+ foreach my $wc (@W) {
+ my $ccw=$cc.$wc;
+ my $ccwl=length($ccw);
+ if ($ccwl <= $sl) {
+ if (index($S,$ccw)==0) {
+ push @l,[@{$c},$wc];
+ if ($ccwl == $sl) {
+ $done=1;
+ last;
+ }
+ }
+ }
+ }
+ unless (@l) {
+ last;
+ }
+}
+
+if (@l) {
+ print join(', ',map {'"' . $_ . '"'} @{$l[-1]}),"\n";
+} else {
+ print "0\n";
+}
diff --git a/challenge-064/roger-bell-west/raku/ch-1.p6 b/challenge-064/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..af1d3e423c
--- /dev/null
+++ b/challenge-064/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,34 @@
+#! /usr/bin/perl6
+
+my @in=(
+ (1,2,3),
+ (4,5,6),
+ (7,8,9),
+ );
+
+
+my @l=((0,0),);
+
+my $s='';
+my @p;
+
+while (@l) {
+ my @t=(shift @l).flat;
+ my @xy=splice @t,0,2;
+ push @t,@in[@xy[0]][@xy[1]];
+ if (@t[@t.end] == @in[@in.end][@in[@in.end].end]) {
+ my $sa=sum(@t);
+ if ($s eq '' || $sa < $s) {
+ $s=$sa;
+ @p=@t;
+ }
+ }
+ if (@xy[0] < @in.end && @xy[1] <= @in[@xy[0]+1].end) {
+ push @l,(@xy[0]+1,@xy[1],map {$_},@t);
+ }
+ if (@xy[1] < @in[@xy[0]].end) {
+ push @l,(@xy[0],@xy[1]+1,map {$_},@t);
+ }
+}
+
+say "$s ( " ~ join(' → ',@p) ~ " )";
diff --git a/challenge-064/roger-bell-west/raku/ch-2.p6 b/challenge-064/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..abb18707ae
--- /dev/null
+++ b/challenge-064/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,46 @@
+#! /usr/bin/perl6
+
+my $S="perlweeklychallenge";
+my @W = ("weekly", "challenge", "perl");
+
+#$S="perlandraku";
+#@W = ("python","ruby","haskell");
+
+my $sl=chars($S);
+
+my @l;
+my $done=0;
+
+while (!$done) {
+ my @c;
+ if (@l) {
+ @c=map {$_},(shift @l).flat;
+ }
+ my $cc=join('',@c);
+ for @W -> $wc {
+ my $ccw=$cc ~ $wc;
+ my $ccwl=chars($ccw);
+ if ($ccwl <= $sl) {
+ with index($S,$ccw) -> $i {
+ if ($i==0) {
+ my @q=@c;
+ push @q,$wc;
+ push @l,@q;
+ if ($ccwl == $sl) {
+ $done=1;
+ last;
+ }
+ }
+ }
+ }
+ }
+ unless (@l) {
+ last;
+ }
+}
+
+if (@l) {
+ say join(', ',map {'"' ~ $_ ~ '"'},@l[@l.end].flat);
+} else {
+ say 0;
+}