From 31730009a636f5b2646aa5c8be60679a081f4fc1 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 8 Jun 2020 13:27:12 +0100 Subject: Solutions to challenge #64. --- challenge-064/roger-bell-west/perl/ch-1.pl | 40 ++++++++++++++++++++++++++ challenge-064/roger-bell-west/perl/ch-2.pl | 45 +++++++++++++++++++++++++++++ challenge-064/roger-bell-west/raku/ch-1.p6 | 34 ++++++++++++++++++++++ challenge-064/roger-bell-west/raku/ch-2.p6 | 46 ++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100755 challenge-064/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-064/roger-bell-west/perl/ch-2.pl create mode 100755 challenge-064/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-064/roger-bell-west/raku/ch-2.p6 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; +} -- cgit