aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-12 13:22:04 +0100
committerGitHub <noreply@github.com>2025-08-12 13:22:04 +0100
commit8fa79c8f9bdb1cc2ffef28f822d13d486ba126c5 (patch)
tree7d0a9e358420cc60261d51cb5e14845e694b0987
parent11496929afa7c40da5ca91c68f98ec0912fff8fc (diff)
parentad851f71671d39d7734f5ea41157a6f89731b22a (diff)
downloadperlweeklychallenge-club-8fa79c8f9bdb1cc2ffef28f822d13d486ba126c5.tar.gz
perlweeklychallenge-club-8fa79c8f9bdb1cc2ffef28f822d13d486ba126c5.tar.bz2
perlweeklychallenge-club-8fa79c8f9bdb1cc2ffef28f822d13d486ba126c5.zip
Merge pull request #12501 from andemark/challenge-334
Challenge 334 Solutions (Raku)
-rw-r--r--challenge-334/mark-anderson/raku/ch-1.raku13
-rw-r--r--challenge-334/mark-anderson/raku/ch-2.raku45
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-334/mark-anderson/raku/ch-1.raku b/challenge-334/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..e2c72a4b8b
--- /dev/null
+++ b/challenge-334/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#!/usr/bin/env raku
+use Test;
+
+is range-sum(<-2 0 3 -5 2 -1>, 0, 2), 1;
+is range-sum(<1 -2 3 -4 5>, 1, 3), -3;
+is range-sum(<1 0 2 -1 3>, 3, 4), 2;
+is range-sum(<-5 4 -3 2 -1 0>, 0, 3), -2;
+is range-sum(<-1 0 2 -3 -2 1>, 0, 2), 1;
+
+sub range-sum(@ints, $x, $y)
+{
+ sum @ints[$x..$y]
+}
diff --git a/challenge-334/mark-anderson/raku/ch-2.raku b/challenge-334/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..00f9cbbab7
--- /dev/null
+++ b/challenge-334/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,45 @@
+#!/usr/bin/env raku
+use Test;
+
+is nearest-valid-point(3, 4, [1,2], [3,1], [2,4], [2,3]), 2;
+is nearest-valid-point(2, 5, [3,4], [2,3], [1,5], [2,5]), 3;
+is nearest-valid-point(1, 1, [2,2], [3,3], [4,4]), -1;
+is nearest-valid-point(0, 0, [0,1], [1,0], [0,2], [2,0]), 0;
+is nearest-valid-point(5, 5, [5,6], [6,5], [5,4], [4,5]), 0;
+
+is nearest-valid-point-maybe(3, 4, [1,2], [3,1], [2,4], [2,3]), 2;
+is nearest-valid-point-maybe(2, 5, [3,4], [2,3], [1,5], [2,5]), 3;
+is nearest-valid-point-maybe(1, 1, [2,2], [3,3], [4,4]), -1;
+is nearest-valid-point-maybe(0, 0, [0,1], [1,0], [0,2], [2,0]), 0;
+is nearest-valid-point-maybe(5, 5, [5,6], [6,5], [5,4], [4,5]), 0;
+
+sub nearest-valid-point($x, $y, +@points)
+{
+ sub distance($p)
+ {
+ $p.key => abs($x - $p.value[0]) + abs($y - $p.value[1])
+ }
+
+ my @mins = @points.grep({ any $x == .[0], $y == .[1] }, :p)
+ .map(&distance)
+ .Map
+ .minpairs;
+
+ @mins ?? @mins.min(:by(*.key)).key !! -1
+}
+
+# this seems to work but I'm not sure it should
+sub nearest-valid-point-maybe($x, $y, +@points)
+{
+ sub distance($p)
+ {
+ abs($x - $p.value[0]) + abs($y - $p.value[1]) => $p.key
+ }
+
+ try return @points.grep({ any $x == .[0], $y == .[1] }, :p)
+ .map(&distance)
+ .min
+ .value;
+
+ return -1
+}