aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2025-08-11 16:57:48 +0800
committer冯昶 <fengchang@novel-supertv.com>2025-08-11 16:57:48 +0800
commit872437fcdc11329a645067d5bf28fbfca5dee755 (patch)
tree71d6ec1cc7793790996e46a211bb9be198030ea3
parent730e172acb159a2fc320a78a6250083328ef1067 (diff)
downloadperlweeklychallenge-club-872437fcdc11329a645067d5bf28fbfca5dee755.tar.gz
perlweeklychallenge-club-872437fcdc11329a645067d5bf28fbfca5dee755.tar.bz2
perlweeklychallenge-club-872437fcdc11329a645067d5bf28fbfca5dee755.zip
challenge 334, raku solutions
-rwxr-xr-xchallenge-334/feng-chang/raku/ch-1.raku6
-rwxr-xr-xchallenge-334/feng-chang/raku/ch-2.raku10
-rwxr-xr-xchallenge-334/feng-chang/raku/test.raku28
3 files changed, 44 insertions, 0 deletions
diff --git a/challenge-334/feng-chang/raku/ch-1.raku b/challenge-334/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..06c17f0d3c
--- /dev/null
+++ b/challenge-334/feng-chang/raku/ch-1.raku
@@ -0,0 +1,6 @@
+#!/bin/env raku
+
+unit sub MAIN(*@ints);
+
+my ($x, $y) = @ints.splice(*-2, 2);
+put @ints[$x..$y].sum;
diff --git a/challenge-334/feng-chang/raku/ch-2.raku b/challenge-334/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..eb8e05589e
--- /dev/null
+++ b/challenge-334/feng-chang/raku/ch-2.raku
@@ -0,0 +1,10 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $points);
+
+use MONKEY-SEE-NO-EVAL;
+
+my ($x, $y, @points) = EVAL $points;
+with @points.map({ ($x==.[0] || $y==.[1]) ?? abs(.[0]-$x) + abs(.[1]-$y) !! Inf }).min(:p).head {
+ put .value != Inf ?? .key !! -1;
+}
diff --git a/challenge-334/feng-chang/raku/test.raku b/challenge-334/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..f3bc51eeaf
--- /dev/null
+++ b/challenge-334/feng-chang/raku/test.raku
@@ -0,0 +1,28 @@
+#!/bin/env raku
+
+# The Weekly Challenge 334
+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, Range Sum
+pwc-test './ch-1.raku', <-- -2 0 3 -5 2 -1 0 2>, 1, 'Range Sum: (-2,0,3,-5,2,-1), 0..2 => 1';
+pwc-test './ch-1.raku', <-- 1 -2 3 -4 5 1 3>, -3, 'Range Sum: (1,-2,3,-4,5), 1..3 => -3';
+pwc-test './ch-1.raku', <-- 1 0 2 -1 3 3 4>, 2, 'Range Sum: (1,0,2,-1,3), 3..4 => 2';
+pwc-test './ch-1.raku', <-- -5 4 -3 2 -1 0 0 3>, -2, 'Range Sum: (-5,4,-3,2,-1,0), 0..3 => -2';
+pwc-test './ch-1.raku', <-- -1 0 2 -3 -2 1 0 2>, 1, 'Range Sum: (-1,0,2,-3,-2,1), 0..2 => 1';
+
+# Task 2
+pwc-test './ch-2.raku', '3, 4, [1,2],[3,1],[2,4],[2,3]', 2, 'Task 2: $x=3,$y=4, @points=[1,2],[3,1],[2,4],[2,3] => 2';
+pwc-test './ch-2.raku', '2, 5, [3,4],[2,3],[1,5],[2,5]', 3, 'Task 2: $x=2,$y=5, @points=[3,4],[2,3],[1,5],[2,5] => 3';
+pwc-test './ch-2.raku', '1, 1, [2,2],[3,3],[4,4]', -1, 'Task 2: $x=1,$y=1, @points=[2,2],[3,3],[4,4] => -1';
+pwc-test './ch-2.raku', '0, 0, [0,1],[1,0],[0,2],[2,0]', 0, 'Task 2: $x=0,$y=0, @points=[0,1],[1,0],[0,2],[2,0] => 0';
+pwc-test './ch-2.raku', '5, 5, [5,6],[6,5],[5,4],[4,5]', 0, 'Task 2: $x=5,$y=5, @points=[5,6],[6,5],[5,4],[4,5] => 0';