aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2024-08-07 11:40:11 +0800
committer冯昶 <fengchang@novel-supertv.com>2024-08-07 11:40:11 +0800
commit76ab2ab2218e601ba98b788afa6f690f31ed0c60 (patch)
tree60b91c7d25a296871edf07d631b64ef60e5fe96e
parente76534a69d6bf5ca84511a5e0d093539a83abedb (diff)
downloadperlweeklychallenge-club-76ab2ab2218e601ba98b788afa6f690f31ed0c60.tar.gz
perlweeklychallenge-club-76ab2ab2218e601ba98b788afa6f690f31ed0c60.tar.bz2
perlweeklychallenge-club-76ab2ab2218e601ba98b788afa6f690f31ed0c60.zip
fix
-rwxr-xr-xchallenge-281/feng-chang/raku/ch-2.raku42
-rwxr-xr-xchallenge-281/feng-chang/raku/test.raku4
2 files changed, 42 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";