aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-13 13:43:10 +0100
committerGitHub <noreply@github.com>2020-10-13 13:43:10 +0100
commit38e1dddafcebb135558f2d9bf35cb489c279cc98 (patch)
tree093b4166537c3e1016628b17bde08c28d7a26ad2
parent8fefadda8698174fc7d7e5fc9b150a3afd547f46 (diff)
parentf3c2e22b5fdb3804c142f85fc6f9b9e375b424d0 (diff)
downloadperlweeklychallenge-club-38e1dddafcebb135558f2d9bf35cb489c279cc98.tar.gz
perlweeklychallenge-club-38e1dddafcebb135558f2d9bf35cb489c279cc98.tar.bz2
perlweeklychallenge-club-38e1dddafcebb135558f2d9bf35cb489c279cc98.zip
Merge pull request #2509 from gugod/gugod-082
gugod's solution for PWC 082
-rw-r--r--challenge-082/gugod/raku/ch-1.raku10
-rw-r--r--challenge-082/gugod/raku/ch-2.raku32
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-082/gugod/raku/ch-1.raku b/challenge-082/gugod/raku/ch-1.raku
new file mode 100644
index 0000000000..f2d918b47b
--- /dev/null
+++ b/challenge-082/gugod/raku/ch-1.raku
@@ -0,0 +1,10 @@
+#!/usr/bin/env raku
+
+sub MAIN (Int $M, Int $N) {
+ my ($a, $b) = ($M, $N).sort;
+ say common-factors($a, $b);
+}
+
+sub common-factors (Int $a, Int $b) {
+ (1, 2..$a/2, $a).flat.grep(-> $n { 0 == $a % $n == $b % $n });
+}
diff --git a/challenge-082/gugod/raku/ch-2.raku b/challenge-082/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..047c4e0c4d
--- /dev/null
+++ b/challenge-082/gugod/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+
+sub MAIN (Str $A, Str $B, Str $C) {
+ say interleaves($A, $B, $C) ?? 1 !! 0;
+}
+
+sub interleaves (Str $A, Str $B, Str $C) {
+ my @stash;
+ @stash.push([-1, -1]);
+
+ my $i = 0;
+ while $i < $C.chars && @stash.elems > 0 {
+ my $c = $C.substr($i++, 1);
+ my @stash2 = gather {
+ while @stash.elems > 0 {
+ my $it = @stash.pop();
+ my $a = $A.substr($it[0]+1, 1);
+ my $b = $B.substr($it[1]+1, 1);
+ if $c eq $a {
+ take [$it[0]+1, $it[1]];
+ }
+ if $c eq $b {
+ take [$it[0], $it[1]+1];
+ }
+ }
+ };
+
+ @stash = @stash2.unique(:with(&[eqv]));
+ }
+
+ return $i == $C.chars && @stash.elems > 0 && @stash[0][0].succ == $A.chars && @stash[0][1].succ == $B.chars;
+}