aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2024-08-06 13:33:00 +0800
committer冯昶 <fengchang@novel-supertv.com>2024-08-06 13:33:00 +0800
commite76534a69d6bf5ca84511a5e0d093539a83abedb (patch)
tree8427d7debe7168d0648e8960cf174f669b852eab
parent524012f041fc882afc4b0aeda4e1bffae33d7ae7 (diff)
downloadperlweeklychallenge-club-e76534a69d6bf5ca84511a5e0d093539a83abedb.tar.gz
perlweeklychallenge-club-e76534a69d6bf5ca84511a5e0d093539a83abedb.tar.bz2
perlweeklychallenge-club-e76534a69d6bf5ca84511a5e0d093539a83abedb.zip
challenge 281, raku solutions
-rwxr-xr-xchallenge-281/feng-chang/raku/ch-1.raku7
-rwxr-xr-xchallenge-281/feng-chang/raku/ch-2.raku25
-rwxr-xr-xchallenge-281/feng-chang/raku/test.raku23
3 files changed, 55 insertions, 0 deletions
diff --git a/challenge-281/feng-chang/raku/ch-1.raku b/challenge-281/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..ba6c17b454
--- /dev/null
+++ b/challenge-281/feng-chang/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $coord where *.chars == 2);
+
+put do with $coord.comb -> ($c, $n) {
+ ($c.ord - 'a'.ord + $n) %% 2
+}
diff --git a/challenge-281/feng-chang/raku/ch-2.raku b/challenge-281/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..43c506b0f1
--- /dev/null
+++ b/challenge-281/feng-chang/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#!/bin/env raku
+
+unit sub 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));
+}
+
+proto _dist(Int:D \x, Int:D \y --> 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(\x, \y where x > y) { _dist(y, x) }
+multi _dist(\x, \y) { min(_dist(x - 1, y - 2), _dist(x - 2, y - 1)) + 1 }
+
+sub dist(UInt:D \x, UInt:D \y --> UInt:D) {
+ state %dists;
+ my \K = "{x},{y}";
+ %dists{K} = _dist(x, y) without %dists{K};
+
+ %dists{K}
+}
diff --git a/challenge-281/feng-chang/raku/test.raku b/challenge-281/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..c718d77f78
--- /dev/null
+++ b/challenge-281/feng-chang/raku/test.raku
@@ -0,0 +1,23 @@
+#!/bin/env raku
+
+# The Weekly Challenge 281
+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, Check Color
+pwc-test './ch-1.raku', 'd3', 'True', 'Check Color: d3 => true';
+pwc-test './ch-1.raku', 'g5', 'False', 'Check Color: g5 => false';
+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";