aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-334/ash/raku/ch-1.raku12
-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
-rw-r--r--challenge-334/mark-anderson/raku/ch-1.raku13
-rw-r--r--challenge-334/mark-anderson/raku/ch-2.raku45
-rw-r--r--challenge-334/pokgopun/go/ch-1.go90
-rw-r--r--challenge-334/pokgopun/go/ch-2.go146
-rw-r--r--challenge-334/pokgopun/python/ch-1.py74
-rw-r--r--challenge-334/pokgopun/python/ch-2.py121
-rwxr-xr-xchallenge-334/simon-proctor/raku/ch-1.raku14
-rwxr-xr-xchallenge-334/simon-proctor/raku/ch-2.raku23
12 files changed, 582 insertions, 0 deletions
diff --git a/challenge-334/ash/raku/ch-1.raku b/challenge-334/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..c61a1ae2c1
--- /dev/null
+++ b/challenge-334/ash/raku/ch-1.raku
@@ -0,0 +1,12 @@
+# Task 1 of the Weekly Challenge 334
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-334/#TASK1
+
+say solve([-2, 0, 3, -5, 2, -1], 0, 2); # 1
+say solve([1, -2, 3, -4, 5], 1, 3); # -3
+say solve([1, 0, 2, -1, 3], 3, 4); # 2
+say solve([-5, 4, -3, 2, -1, 0], 0, 3); # -2
+say solve([-1, 0, 2, -3, -2, 1], 0, 2); # 1
+
+sub solve(@data, $beg, $end) {
+ [+] @data[$beg..$end]
+}
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';
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
+}
diff --git a/challenge-334/pokgopun/go/ch-1.go b/challenge-334/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..66cc1bb2e5
--- /dev/null
+++ b/challenge-334/pokgopun/go/ch-1.go
@@ -0,0 +1,90 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-334/
+/*#
+
+Task 1: Range Sum
+
+Submitted by: [50]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a list integers and pair of indices..
+
+ Write a script to return the sum of integers between the given indices
+ (inclusive).
+
+Example 1
+
+Input: @ints = (-2, 0, 3, -5, 2, -1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-2, 0, 3)
+Range Sum: (-2) + 0 + 3 => 1
+
+Example 2
+
+Input: @ints = (1, -2, 3, -4, 5), $x = 1, $y = 3
+Output: -3
+
+Elements between indices (1, 3) => (-2, 3, -4)
+Range Sum: (-2) + 3 + (-4) => -3
+
+Example 3
+
+Input: @ints = (1, 0, 2, -1, 3), $x = 3, $y = 4
+Output: 2
+
+Elements between indices (3, 4) => (-1, 3)
+Range Sum: (-1) + 3 => 2
+
+Example 4
+
+Input: @ints = (-5, 4, -3, 2, -1, 0), $x = 0, $y = 3
+Output: -2
+
+Elements between indices (0, 3) => (-5, 4, -3, 2)
+Range Sum: (-5) + 4 + (-3) + 2 => -2
+
+Example 5
+
+Input: @ints = (-1, 0, 2, -3, -2, 1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-1, 0, 2)
+Range Sum: (-1) + 0 + 2 => 1
+
+Task 2:
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) rs(x, y int) int {
+ sum := 0
+ for _, v := range in[x : y+1] {
+ sum += v
+ }
+ return sum
+}
+
+func main() {
+ for _, data := range []struct {
+ ints ints
+ x, y, res int
+ }{
+ {ints{-2, 0, 3, -5, 2, -1}, 0, 2, 1},
+ {ints{1, -2, 3, -4, 5}, 1, 3, -3},
+ {ints{1, 0, 2, -1, 3}, 3, 4, 2},
+ {ints{-5, 4, -3, 2, -1, 0}, 0, 3, -2},
+ {ints{-1, 0, 2, -3, -2, 1}, 0, 2, 1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.ints.rs(data.x, data.y), data.res)) // blank if ok, otherwise show the differece
+ }
+}
diff --git a/challenge-334/pokgopun/go/ch-2.go b/challenge-334/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..a5345e9c78
--- /dev/null
+++ b/challenge-334/pokgopun/go/ch-2.go
@@ -0,0 +1,146 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-334/
+/*#
+
+Task 2:
+
+Submitted by: [51]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given current location as two integers: x and y. You are also
+ given a list of points on the grid.
+
+ A point is considered valid if it shares either the same x-coordinate
+ or the same y-coordinate as the current location.
+
+ Write a script to return the index of the valid point that has the
+ smallest Manhattan distance to the current location. If multiple valid
+ points are tied for the smallest distance, return the one with the
+ lowest index. If no valid points exist, return -1.
+
+ The Manhattan distance between two points (x1, y1) and (x2, y2) is
+ calculated as: |x1 - x2| + |y1 - y2|
+
+Example 1
+
+Input: $x = 3, $y = 4, @points ([1, 2], [3, 1], [2, 4], [2, 3])
+Output: 2
+
+Valid points: [3, 1] (same x), [2, 4] (same y)
+
+Manhattan distances:
+ [3, 1] => |3-3| + |4-1| = 3
+ [2, 4] => |3-2| + |4-4| = 1
+
+Closest valid point is [2, 4] at index 2.
+
+Example 2
+
+Input: $x = 2, $y = 5, @points ([3, 4], [2, 3], [1, 5], [2, 5])
+Output: 3
+
+Valid points: [2, 3], [1, 5], [2, 5]
+
+Manhattan distances:
+ [2, 3] => 2
+ [1, 5] => 1
+ [2, 5] => 0
+
+Closest valid point is [2, 5] at index 3.
+
+Example 3
+
+Input: $x = 1, $y = 1, @points ([2, 2], [3, 3], [4, 4])
+Output: -1
+
+No point shares x or y with (1, 1).
+
+Example 4
+
+Input: $x = 0, $y = 0, @points ([0, 1], [1, 0], [0, 2], [2, 0])
+Output: 0
+
+Valid points: all of them
+
+Manhattan distances:
+ [0, 1] => 1
+ [1, 0] => 1
+ [0, 2] => 2
+ [2, 0] => 2
+
+Tie between index 0 and 1, pick the smaller index: 0
+
+Example 5
+
+Input: $x = 5, $y = 5, @points ([5, 6], [6, 5], [5, 4], [4, 5])
+Output: 0
+
+Valid points: all of them
+ [5, 6] => 1
+ [6, 5] => 1
+ [5, 4] => 1
+ [4, 5] => 1
+
+All tie, return the one with the lowest index: 0
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 17th August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type point struct {
+ x, y int
+}
+
+type points []point
+
+// mmd return index of minimum mahattan distance for given origin point
+func (ps points) mmd(x, y int) int {
+ var (
+ idx = -1 // default index if not found minimum manhattan distance
+ mn, md int // mininum and manhattan distance
+ )
+ for i, p := range ps {
+ switch { // calculate manhattan distance when x or y coordinate matches, otherwise skip
+ case p.x == x:
+ md = max(p.y, y) - min(p.y, y)
+ case p.y == y:
+ md = max(p.x, x) - min(p.x, x)
+ default:
+ continue
+ }
+ if idx < 0 || mn > md { // update 1st minimum manhattan distance or less than the current mininum
+ mn = md
+ idx = i
+ }
+ }
+ return idx
+}
+
+func main() {
+ for _, data := range []struct {
+ x, y int
+ points points
+ output int
+ }{
+ {3, 4, points{point{1, 2}, point{3, 1}, point{2, 4}, point{2, 3}}, 2},
+ {2, 5, points{point{3, 4}, point{2, 3}, point{1, 5}, point{2, 5}}, 3},
+ {1, 1, points{point{2, 2}, point{3, 3}, point{4, 4}}, -1},
+ {0, 0, points{point{0, 1}, point{1, 0}, point{0, 2}, point{2, 0}}, 0},
+ {5, 5, points{point{5, 6}, point{6, 5}, point{5, 4}, point{4, 5}}, 0},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.points.mmd(data.x, data.y), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-334/pokgopun/python/ch-1.py b/challenge-334/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..7e5de27b4e
--- /dev/null
+++ b/challenge-334/pokgopun/python/ch-1.py
@@ -0,0 +1,74 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-334/
+"""
+
+Task 1: Range Sum
+
+Submitted by: [50]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a list integers and pair of indices..
+
+ Write a script to return the sum of integers between the given indices
+ (inclusive).
+
+Example 1
+
+Input: @ints = (-2, 0, 3, -5, 2, -1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-2, 0, 3)
+Range Sum: (-2) + 0 + 3 => 1
+
+Example 2
+
+Input: @ints = (1, -2, 3, -4, 5), $x = 1, $y = 3
+Output: -3
+
+Elements between indices (1, 3) => (-2, 3, -4)
+Range Sum: (-2) + 3 + (-4) => -3
+
+Example 3
+
+Input: @ints = (1, 0, 2, -1, 3), $x = 3, $y = 4
+Output: 2
+
+Elements between indices (3, 4) => (-1, 3)
+Range Sum: (-1) + 3 => 2
+
+Example 4
+
+Input: @ints = (-5, 4, -3, 2, -1, 0), $x = 0, $y = 3
+Output: -2
+
+Elements between indices (0, 3) => (-5, 4, -3, 2)
+Range Sum: (-5) + 4 + (-3) + 2 => -2
+
+Example 5
+
+Input: @ints = (-1, 0, 2, -3, -2, 1), $x = 0, $y = 2
+Output: 1
+
+Elements between indices (0, 2) => (-1, 0, 2)
+Range Sum: (-1) + 0 + 2 => 1
+
+Task 2:
+"""
+### solution by pokgopun@gmail.com
+
+def rs(ints: tuple[int], x: int, y: int) -> int:
+ return sum(ints[x:y+1])
+
+import unittest
+
+class TestRs(unittest.TestCase):
+ def test(self):
+ for (ints,x,y),otpt in {
+ ((-2, 0, 3, -5, 2, -1), 0, 2): 1,
+ ((1, -2, 3, -4, 5), 1, 3): -3,
+ ((1, 0, 2, -1, 3), 3, 4): 2,
+ ((-5, 4, -3, 2, -1, 0), 0, 3): -2,
+ ((-1, 0, 2, -3, -2, 1), 0, 2): 1,
+ }.items():
+ self.assertEqual(rs(ints,x,y), otpt)
+
+unittest.main()
diff --git a/challenge-334/pokgopun/python/ch-2.py b/challenge-334/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..1c53d96513
--- /dev/null
+++ b/challenge-334/pokgopun/python/ch-2.py
@@ -0,0 +1,121 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-334/
+"""
+
+Task 2:
+
+Submitted by: [51]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given current location as two integers: x and y. You are also
+ given a list of points on the grid.
+
+ A point is considered valid if it shares either the same x-coordinate
+ or the same y-coordinate as the current location.
+
+ Write a script to return the index of the valid point that has the
+ smallest Manhattan distance to the current location. If multiple valid
+ points are tied for the smallest distance, return the one with the
+ lowest index. If no valid points exist, return -1.
+
+ The Manhattan distance between two points (x1, y1) and (x2, y2) is
+ calculated as: |x1 - x2| + |y1 - y2|
+
+Example 1
+
+Input: $x = 3, $y = 4, @points ([1, 2], [3, 1], [2, 4], [2, 3])
+Output: 2
+
+Valid points: [3, 1] (same x), [2, 4] (same y)
+
+Manhattan distances:
+ [3, 1] => |3-3| + |4-1| = 3
+ [2, 4] => |3-2| + |4-4| = 1
+
+Closest valid point is [2, 4] at index 2.
+
+Example 2
+
+Input: $x = 2, $y = 5, @points ([3, 4], [2, 3], [1, 5], [2, 5])
+Output: 3
+
+Valid points: [2, 3], [1, 5], [2, 5]
+
+Manhattan distances:
+ [2, 3] => 2
+ [1, 5] => 1
+ [2, 5] => 0
+
+Closest valid point is [2, 5] at index 3.
+
+Example 3
+
+Input: $x = 1, $y = 1, @points ([2, 2], [3, 3], [4, 4])
+Output: -1
+
+No point shares x or y with (1, 1).
+
+Example 4
+
+Input: $x = 0, $y = 0, @points ([0, 1], [1, 0], [0, 2], [2, 0])
+Output: 0
+
+Valid points: all of them
+
+Manhattan distances:
+ [0, 1] => 1
+ [1, 0] => 1
+ [0, 2] => 2
+ [2, 0] => 2
+
+Tie between index 0 and 1, pick the smaller index: 0
+
+Example 5
+
+Input: $x = 5, $y = 5, @points ([5, 6], [6, 5], [5, 4], [4, 5])
+Output: 0
+
+Valid points: all of them
+ [5, 6] => 1
+ [6, 5] => 1
+ [5, 4] => 1
+ [4, 5] => 1
+
+All tie, return the one with the lowest index: 0
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 17th August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from dataclasses import dataclass
+
+@dataclass
+class Point:
+ x: int
+ y: int
+
+def mmd(x: int, y: int, ps: tuple[Point]) -> int:
+ return list(
+ [e[1] for e in sorted(
+ (abs(ps[i]. x-x) + abs(ps[i]. y-y), i) for i in range(len(ps)) if ps[i].x==x or ps[i].y==y
+ )
+ ] + [-1]
+ )[0]
+
+import unittest
+
+class TestMmd(unittest.TestCase):
+ def test(self):
+ for (x, y, ps), otpt in {
+ (3, 4, ((1, 2), (3, 1), (2, 4), (2, 3))): 2,
+ (2, 5, ((3, 4), (2, 3), (1, 5), (2, 5))): 3,
+ (1, 1, ((2, 2), (3, 3), (4, 4))): -1,
+ (0, 0, ((0, 1), (1, 0), (0, 2), (2, 0))): 0,
+ }.items():
+ self.assertEqual(mmd(x, y, tuple(Point(e[0],e[1]) for e in ps)), otpt)
+
+unittest.main()
diff --git a/challenge-334/simon-proctor/raku/ch-1.raku b/challenge-334/simon-proctor/raku/ch-1.raku
new file mode 100755
index 0000000000..118f3b556b
--- /dev/null
+++ b/challenge-334/simon-proctor/raku/ch-1.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+
+multi sub MAIN(:t(:$test)) is hidden-from-USAGE {
+ use Test;
+ is range-sum( x=>0, y=>2, -2, 0, 3, -5, 2, -1), 1;
+ is range-sum( x=>1, y=>3, 1, -2, 3, -4, 5), -3;
+ is range-sum( x=>3, y=>4, 1, 0, 2, -1, 3), 2;
+ is range-sum( x=>0, y=>3, -5, 4, -3, 2, -1, 0), -2;
+ is range-sum( x=>0, y=>2, -1, 0, 2, -3, -2, 1), 1;
+ done-testing;
+}
+
+sub range-sum( Int() :$x, Int() :$y, *@ints where all(@ints) ~~ Int() ) { [+] @ints[$x..$y] }
+
diff --git a/challenge-334/simon-proctor/raku/ch-2.raku b/challenge-334/simon-proctor/raku/ch-2.raku
new file mode 100755
index 0000000000..cb1a995036
--- /dev/null
+++ b/challenge-334/simon-proctor/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+
+sub MAIN(:t(:$test)) is hidden-from-USAGE {
+ use Test;
+ is closest-point-in-line( x => 3, y => 4, [1, 2], [3, 1], [2, 4], [2, 3]), 2;
+ is closest-point-in-line( x => 2, y => 5, [3, 4], [2, 3], [1, 5], [2, 5]), 3;
+ is closest-point-in-line( x => 1, y => 1, [2, 2], [3, 3], [4, 4]), -1;
+ is closest-point-in-line( x => 0, y => 0, [0, 1], [1, 0], [0, 2], [2, 0]), 0;
+ is closest-point-in-line( x => 5, y => 5, [5, 6], [6, 5], [5, 4], [4, 5]), 0;
+ done-testing;
+}
+
+sub closest-point-in-line( Int :$x, Int :$y, **@points ) {
+ my @indexed = @points
+ .map( { $++ => $_ } )
+ .grep( -> $p { $p.value[0] ~~ $x || $p.value[1] ~~ $y } )
+ .map( -> $p { $p.key => manhattan( $x, $y, |$p.value ) } )
+ .sort( { $^a.value <=> $^b.value } )
+ .map(*.key);
+ @indexed ?? @indexed[0] !! -1;
+}
+
+sub manhattan($x1, $y1, $x2, $y2) { abs($x1-$x2) + abs($y1-$y2) }