aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScimon <simon.proctor@gmail.com>2025-08-11 09:47:27 +0100
committerScimon <simon.proctor@gmail.com>2025-08-11 09:47:27 +0100
commitd4fb3135b9bd2ef094e9c3f45d184a4121f3c670 (patch)
tree582d49efc2aa6c434a885cba25eeeab1b336d752
parent730e172acb159a2fc320a78a6250083328ef1067 (diff)
downloadperlweeklychallenge-club-d4fb3135b9bd2ef094e9c3f45d184a4121f3c670.tar.gz
perlweeklychallenge-club-d4fb3135b9bd2ef094e9c3f45d184a4121f3c670.tar.bz2
perlweeklychallenge-club-d4fb3135b9bd2ef094e9c3f45d184a4121f3c670.zip
Challenge 334
-rwxr-xr-xchallenge-334/simon-proctor/raku/ch-1.raku14
-rwxr-xr-xchallenge-334/simon-proctor/raku/ch-2.raku23
2 files changed, 37 insertions, 0 deletions
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) }