aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-281/feng-chang/raku/ch-2.raku42
-rwxr-xr-xchallenge-281/feng-chang/raku/test.raku4
-rwxr-xr-xchallenge-282/feng-chang/raku/ch-1.raku6
-rwxr-xr-xchallenge-282/feng-chang/raku/ch-2.raku5
-rwxr-xr-xchallenge-282/feng-chang/raku/ch-2a.raku7
-rwxr-xr-xchallenge-282/feng-chang/raku/test.raku27
6 files changed, 87 insertions, 4 deletions
diff --git a/challenge-281/feng-chang/raku/ch-2.raku b/challenge-281/feng-chang/raku/ch-2.raku
index 43c506b0f1..cc273a8eb0 100755
--- a/challenge-281/feng-chang/raku/ch-2.raku
+++ b/challenge-281/feng-chang/raku/ch-2.raku
@@ -1,18 +1,52 @@
#!/bin/env raku
-unit sub MAIN(Str:D $p1 where *.chars == 2, Str:D $p2 where *.chars == 2);
+multi MAIN(Str:D $p1 where *.chars == 2, Str:D $p2 where *.chars == 2) {
+ with |$p1.comb, |$p2.comb -> (\c1,\d1,\c2,\d2) {
+ put dist(abs(c1.ord - c2.ord), abs(d1 - d2));
+ }
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is dist(0, 0), 0, '0,0 => 0';
+ is dist(0, 1), 3, '0,1 => 3';
+ is dist(1, 0), 3, '1,0 => 3';
+ is dist(0, 2), 2, '0,2 => 2';
+ is dist(1, 2), 1, '1,2 => 1';
+ is dist(2, 2), 4, '2,2 => 4';
+ is dist(2, 0), 2, '2,0 => 2';
+ is dist(2, 1), 1, '2,1 => 1';
+ is dist(0, 3), 3, '0,3 => 3';
+ is dist(1, 3), 2, '1,3 => 2';
+ is dist(2, 3), 3, '2,3 => 3';
+ is dist(3, 3), 2, '3,3 => 2';
+ is dist(3, 0), 3, '3,0 => 3';
+ is dist(3, 1), 2, '3,1 => 2';
+ is dist(3, 2), 3, '3,2 => 3';
+ is dist(0, 4), 2, '0,4 => 2';
+ is dist(1, 4), 3, '1,4 => 3';
+ is dist(2, 4), 2, '2,4 => 2';
+ is dist(3, 4), 3, '3,4 => 3';
+ is dist(4, 4), 4, '4,4 => 4';
+ is dist(0, 5), 3, '0,5 => 3';
+ is dist(0, 6), 4, '0,6 => 4';
+ is dist(0, 7), 5, '0,7 => 5';
+ is dist(0, 8), 4, '0,8 => 4';
-with |$p1.comb, |$p2.comb -> (\c1,\d1,\c2,\d2) {
- put dist(abs(c1.ord - c2.ord), abs(d1 - d2));
+ done-testing;
}
-proto _dist(Int:D \x, Int:D \y --> UInt:D) {*}
+proto _dist(Int:D, Int:D --> UInt:D) {*}
multi _dist(\x where * < 0, $_) { Inf }
multi _dist($_, \y where * < 0) { Inf }
multi _dist(0, 0) { 0 }
multi _dist(0, 1) { 3 }
multi _dist(1, 1) { 2 }
multi _dist(0, 2) { 2 }
+multi _dist(0, 3) { 3 }
+multi _dist(1, 3) { 2 }
+multi _dist(0, \y) { min(_dist(0, y - 2) + 2, _dist(0, y - 4) + 2, _dist(0, y - 5) + 3) }
multi _dist(\x, \y where x > y) { _dist(y, x) }
multi _dist(\x, \y) { min(_dist(x - 1, y - 2), _dist(x - 2, y - 1)) + 1 }
diff --git a/challenge-281/feng-chang/raku/test.raku b/challenge-281/feng-chang/raku/test.raku
index c718d77f78..c1dc94d4d5 100755
--- a/challenge-281/feng-chang/raku/test.raku
+++ b/challenge-281/feng-chang/raku/test.raku
@@ -21,3 +21,7 @@ pwc-test './ch-1.raku', 'e6', 'True', 'Check Color: e6 => true';
# Task 2, Knight's Move
pwc-test './ch-2.raku', <g2 a8>, 4, "Knight\'s Move: g2, a8 => 4";
pwc-test './ch-2.raku', <g2 h2>, 3, "Knight\'s Move: g2, h2 => 3";
+pwc-test './ch-2.raku', <a1 b4>, 2, "Knight\'s Move: a1, b4 => 2";
+pwc-test './ch-2.raku', <a1 a4>, 3, "Knight\'s Move: a1, a4 => 3";
+pwc-test './ch-2.raku', <a1 a5>, 2, "Knight\'s Move: a1, a5 => 2";
+pwc-test './ch-2.raku', <a1 a6>, 3, "Knight\'s Move: a1, a6 => 3";
diff --git a/challenge-282/feng-chang/raku/ch-1.raku b/challenge-282/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..b93cdd694a
--- /dev/null
+++ b/challenge-282/feng-chang/raku/ch-1.raku
@@ -0,0 +1,6 @@
+#!/bin/env raku
+
+unit sub MAIN(UInt:D $int);
+
+my token Seq { (<[0..9]>) $0 $0+ }
+put $int.match(/<Seq>/, :g).grep(*.chars == 3).first // -1;
diff --git a/challenge-282/feng-chang/raku/ch-2.raku b/challenge-282/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..19bd7e4012
--- /dev/null
+++ b/challenge-282/feng-chang/raku/ch-2.raku
@@ -0,0 +1,5 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $str);
+
+put $str.lc.comb.squish.elems - 1;
diff --git a/challenge-282/feng-chang/raku/ch-2a.raku b/challenge-282/feng-chang/raku/ch-2a.raku
new file mode 100755
index 0000000000..f38b42d8d2
--- /dev/null
+++ b/challenge-282/feng-chang/raku/ch-2a.raku
@@ -0,0 +1,7 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $str is copy);
+
+$str .= lc;
+$str ~~ s:g/$<c>=(<[a..z]>) $<c>+/$<c>/;
+put $str.chars - 1;
diff --git a/challenge-282/feng-chang/raku/test.raku b/challenge-282/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..c03f25effd
--- /dev/null
+++ b/challenge-282/feng-chang/raku/test.raku
@@ -0,0 +1,27 @@
+#!/bin/env raku
+
+# The Weekly Challenge 282
+use Test;
+
+sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) {
+ my ($expect, $assertion) = @input.splice(*-2, 2);
+ my $p = run $script, |@input, :out;
+ if $deeply {
+ is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion;
+ } else {
+ is $p.out.slurp(:close).chomp, $expect, $assertion;
+ }
+}
+
+# Task 1, Good Integer
+pwc-test './ch-1.raku', 12344456, 444, 'Good Integer: 12344456 => 444';
+pwc-test './ch-1.raku', 1233334, -1, 'Good Integer: 1233334 => -1';
+pwc-test './ch-1.raku', 10020003, '000', 'Good Integer: 10020003 => 000';
+
+# Task 2, Changing Keys
+pwc-test './ch-2.raku', 'pPeERrLl', 3, "Changing Keys: pPeERrLl => 3";
+pwc-test './ch-2.raku', 'rRr', 0, "Changing Keys: rRr => 0";
+pwc-test './ch-2.raku', 'GoO', 1, "Changing Keys: GoO => 1";
+pwc-test './ch-2a.raku', 'pPeERrLl', 3, "Changing Keys: pPeERrLl => 3";
+pwc-test './ch-2a.raku', 'rRr', 0, "Changing Keys: rRr => 0";
+pwc-test './ch-2a.raku', 'GoO', 1, "Changing Keys: GoO => 1";