diff options
| author | Kang-min Liu <gugod@gugod.org> | 2020-10-13 00:09:51 +0900 |
|---|---|---|
| committer | Kang-min Liu <gugod@gugod.org> | 2020-10-13 00:09:51 +0900 |
| commit | f3c2e22b5fdb3804c142f85fc6f9b9e375b424d0 (patch) | |
| tree | 1a985db54524d83a139d2bcf44ca58923ce09de0 | |
| parent | 34ffa543b02e35a80e91461bef167732eb224704 (diff) | |
| download | perlweeklychallenge-club-f3c2e22b5fdb3804c142f85fc6f9b9e375b424d0.tar.gz perlweeklychallenge-club-f3c2e22b5fdb3804c142f85fc6f9b9e375b424d0.tar.bz2 perlweeklychallenge-club-f3c2e22b5fdb3804c142f85fc6f9b9e375b424d0.zip | |
a solution of 082.2 in raku
| -rw-r--r-- | challenge-082/gugod/raku/ch-2.raku | 32 |
1 files changed, 32 insertions, 0 deletions
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; +} |
