From e37393936e2980c429ad282af9fd021ea27f27fe Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 23 Feb 2021 21:38:45 +0100 Subject: Solution to task 1 --- challenge-101/jo-37/perl/ch-1.pl | 112 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100755 challenge-101/jo-37/perl/ch-1.pl diff --git a/challenge-101/jo-37/perl/ch-1.pl b/challenge-101/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..22516331d2 --- /dev/null +++ b/challenge-101/jo-37/perl/ch-1.pl @@ -0,0 +1,112 @@ +#!/usr/bin/perl -s + +use v5.16; +use PDL; +use Test2::V0 '!float'; +use Math::Prime::Util 'divisors'; +use experimental qw(signatures postderef); + +our ($tests, $examples, $verbose, $alt); + +run_tests() if $tests || $examples; # does not return + +die <slice($alt ? '*,-1:0' : 'X,*')->sever; + say $roll if $verbose; + + while (@$arr) { + # Left-rotate the current matrix and add an apt piece off the + # list tail. + $roll = $roll + ->slice('-1:0')->xchg(0,1) + ->glue(1, pdl splice @$arr, -$roll->dim(1)) + ->sever; + say $roll if $verbose; + } + + # Return the solution as an AoA or as the piddle itself depending on + # the context. + wantarray ? $roll->unpdl->@* : $roll; +} + + +### Examples and tests + +sub run_tests { + local $alt; + SKIP: { + skip "examples" unless $examples; + + is [roll [1 .. 4]], [[4, 3], [1, 2]], 'example 1'; + is [roll [1 .. 6]], [[6, 5, 4], [1, 2, 3]], 'example 2a'; + is [roll [1 .. 6], 1], [[5, 4], [6, 3], [1, 2]], 'example 2b'; + is [roll [1 .. 12]], + [[9, 8, 7, 6], [10, 11, 12, 5], [1, 2, 3, 4]], + 'example 3a'; + is [roll [1 .. 12], 1], + [[8, 7, 6], [9, 12, 5], [10, 11, 4], [1, 2, 3]], + 'example 3b'; + } + + SKIP: { + skip "tests" unless $tests; + + is form(12 * 12), 0, 'form 0'; + is form(11 * 12), 1, 'form 1'; + is form(12 * 14), 2, 'form 2'; + is form(9 * 12), 3, 'form 3'; + is form(137), 136, 'form prime - 1'; + + is [roll [1 .. 3]], [[1 .. 3]], 'prime'; + is [roll [1 .. 3], 1], [[3], [2], [1]], 'alt prime'; + is [roll [1 .. 9]], [[7, 6, 5], [8, 9, 4], [1, 2, 3]], 'odd size'; + is [roll [1]], [[1]], 'single element'; + } + + done_testing; + exit; +} -- cgit From 84e7fd7cf5518417a53b5255fd68fd5610fc8262 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 23 Feb 2021 14:51:02 +0100 Subject: Solution to task 2 --- challenge-101/jo-37/perl/ch-2.pl | 117 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100755 challenge-101/jo-37/perl/ch-2.pl diff --git a/challenge-101/jo-37/perl/ch-2.pl b/challenge-101/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..93926b9922 --- /dev/null +++ b/challenge-101/jo-37/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/perl -s + +use v5.16; +use PDL; +use List::Util 'pairs'; +use Test2::V0 '!float'; + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <xchg(0,1)->sever; + say "coords:$p" if $verbose; + + # Get the minimum and the maximum of the three matrices' + # determinants formed by the point pairs. + my ($min_d, $max_d) = $p->range([[0, 0], [1, 0], [2, 0]], 2, 'p') + ->reorder(1, 2, 0)->determinant->minmax; + say "min/max det: $min_d/$max_d" if $verbose; + + # If determinants have different signs, the origin is outside the + # triangle. + return 0 if $min_d < 0 && $max_d > 0; + + # If there is at least one nonzero determinant and there are no + # differing signs, the origin is located inside the triangle (or on + # its border). + return 1 if $min_d >= 0 && $max_d > 0 || $min_d < 0 && $max_d <= 0; + + # At this point all determinants are zero. + + # Get the projections onto the x and y axis for collinear points + # and check if they both contain the origin. + my ($min_p, $max_p) = $p->minmaximum; + say "min/max proj: $min_p/$max_p" if $verbose; + + return 1 if max($min_p) <= 0 && min($max_p) >= 0; + + # Else: origin is not within the given line segment. + 0; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is inner_origin([0, 1], [1, 0], [2, 2]), F(), 'example 1'; + is inner_origin([1, 1], [-1, 1], [0,-3]), T(), 'example 2'; + is inner_origin([0, 1], [2, 0], [-6, 0]), T(), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is inner_origin([-1, -1], [-1, 2], [2, -1]), T(), 'inside'; + is inner_origin([2, -1], [2, 2], [-1, 2]), F(), 'outside'; + is inner_origin([0, 0], [3, 1], [1, 2]), T(), 'origin at corner'; + is inner_origin([1, 1], [3, 3], [-1, -2]), F(), 'two collinear points'; + is inner_origin([-1, -2], [0, -1], [1, 0]), F(), 'flat'; + is inner_origin([-1, -2], [1, 0], [1, 0]), F(), 'two points'; + is inner_origin([-1, -1], [1, 1], [2, 2]), T(), 'aligned around origin'; + is inner_origin([-1, 1], [-2, 2], [-4, 4]), F(), + 'aligned without origin'; + is inner_origin([0, 0], [0, 0], [1, 1]), T(), 'two points at origin'; + is inner_origin([0, 0], [0, 0], [0, 0]), T(), 'single point at origin'; + is inner_origin([1, 2], [1, 2], [1, 2]), F(), 'single point off origin'; + } + + done_testing; + exit; +} -- cgit