aboutsummaryrefslogtreecommitdiff
path: root/challenge-101
diff options
context:
space:
mode:
authormimosinnet <mimosinnet@gmail.com>2021-02-28 22:40:25 +0100
committermimosinnet <mimosinnet@gmail.com>2021-02-28 22:40:25 +0100
commit7d1b87128feb69ca7fbba34dfc925d5340ff8ea7 (patch)
tree6b588807f29b7e24e3b146f60788cc87aee2e8eb /challenge-101
parentbc52998b43e2821c55e75c92f7e193846a057c6a (diff)
downloadperlweeklychallenge-club-7d1b87128feb69ca7fbba34dfc925d5340ff8ea7.tar.gz
perlweeklychallenge-club-7d1b87128feb69ca7fbba34dfc925d5340ff8ea7.tar.bz2
perlweeklychallenge-club-7d1b87128feb69ca7fbba34dfc925d5340ff8ea7.zip
Solution for challenge 101
Diffstat (limited to 'challenge-101')
-rw-r--r--challenge-101/mimosinnet/raku/ch-1.raku78
-rw-r--r--challenge-101/mimosinnet/raku/ch-2.raku77
-rw-r--r--challenge-101/mimosinnet/raku/test.raku58
3 files changed, 213 insertions, 0 deletions
diff --git a/challenge-101/mimosinnet/raku/ch-1.raku b/challenge-101/mimosinnet/raku/ch-1.raku
new file mode 100644
index 0000000000..71bab88a23
--- /dev/null
+++ b/challenge-101/mimosinnet/raku/ch-1.raku
@@ -0,0 +1,78 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-101/
+
+# This exercise is unfinished. It works for the examples provided,
+# but it has issues with complex ones.
+
+#|
+sub challenge( @array ) {
+
+ my $cols = sqrt(@array.elems).ceiling - 1;
+ my $rows = sqrt(@array.elems).floor - 1;
+ my @spiral;
+
+ sub array_boundary( $rows, $row is copy, $cols, $col is copy, @array ) {
+ my $array_area = 2 * ( $cols + $rows );
+ # We start at the highest row, lowest column
+ my $col_inc = 1;
+ my $row_inc = 0;
+ # First value
+ @spiral[ $row ][ $col ] = @array.shift;
+ for 2..$array_area {
+ last unless @array.elems;
+ if $col == $cols and $row == $rows { $col_inc = 0; $row_inc = -1 }
+ elsif $col == $cols and $row == 0 { $col_inc = -1; $row_inc = 0 }
+ elsif $col == 0 and $row == 0 { $col_inc = 0; $row_inc = 1 }
+ $row += $row_inc;
+ $col += $col_inc;
+ @spiral[ $row ][ $col ] = @array.shift;
+ }
+ return unless @array.elems;
+ array_boundary($rows - 1, $rows -1, $cols - 1, 1, @array);
+ }
+ array_boundary($rows, $rows, $cols, 0, @array.Array);
+ return @spiral;
+}
+
+multi sub MAIN( $a ) {
+ say 'Input: @A = (',$a,')';
+ say 'Output:';
+ for challenge($a) -> @a {
+ say @a;
+ }
+}
+
+multi sub MAIN( 'challenge' ) {
+ my @challenge = (
+ (1..4),
+ (1..6),
+ (1..12)
+ );
+
+ for @challenge -> $a {
+ MAIN($a);
+ }
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my @test = (
+ (
+ (1..4),
+ [[4,3], [1,2]]
+ ),
+ (
+ (1..6),
+ [[6,5,4], [1,2,3]]
+ ),
+ (
+ (1..12),
+ [[9,8,7,6], [10,11,12,5], [1,2,3,4]]
+ )
+ );
+
+ for @test -> ($a, $b ) {
+ is-deeply challenge($a), $b;
+ }
+
+}
diff --git a/challenge-101/mimosinnet/raku/ch-2.raku b/challenge-101/mimosinnet/raku/ch-2.raku
new file mode 100644
index 0000000000..2b28bcf170
--- /dev/null
+++ b/challenge-101/mimosinnet/raku/ch-2.raku
@@ -0,0 +1,77 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-1001/
+
+# https://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/
+# Point P is inside Triangle A, B, C if:
+# Area ABC = Area ABP + Area ACP + Area BCP
+
+class Vertex
+{
+ has Int $.x;
+ has Int $.y;
+}
+
+class Triangle
+{
+ has Vertex $.a;
+ has Vertex $.b;
+ has Vertex $.c;
+
+ method area()
+ {
+ abs(
+ $!a.x * ( $!b.y - $!c.y ) +
+ $!b.x * ( $!c.y - $!a.y ) +
+ $!c.x * ( $!a.y - $!b.y )
+ ) / 2;
+ }
+}
+
+sub challenge( $a, $b, $c, --> Int ) {
+
+ # Vertex
+ my $Va = Vertex.new( x => $a[0], y => $a[1] );
+ my $Vb = Vertex.new( x => $b[0], y => $b[1] );
+ my $Vc = Vertex.new( x => $c[0], y => $c[1] );
+ my $V0 = Vertex.new( x => 0, y => 0 );
+
+ # Area
+ my $Aabc = Triangle.new( a => $Va, b => $Vb, c => $Vc ).area;
+ my $Aab0 = Triangle.new( a => $Va, b => $Vb, c => $V0 ).area;
+ my $Abc0 = Triangle.new( a => $Vb, b => $Vc, c => $V0 ).area;
+ my $Aac0 = Triangle.new( a => $Va, b => $Vc, c => $V0 ).area;
+
+ return ( $Aabc == $Aab0 + $Abc0 + $Aac0 ).Int;
+}
+
+multi sub MAIN( $a, $b, $c ) {
+ say 'Input: A=',$a,', B=',$b,' and C=',$c;
+ say 'Output: ',challenge($a, $b, $c),"\n";
+}
+
+multi sub MAIN( 'challenge' ) {
+
+ my @challenge = (
+ ((0, 1), ( 1, 0), ( 2, 2)),
+ ((1, 1), (-1, 1), ( 0, -3)),
+ ((0, 1), ( 2, 0), (-6, 0))
+ );
+
+ for @challenge -> ($a, $b, $c) {
+ MAIN($a,$b,$c);
+ }
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my @test = (
+ ((0, 1), ( 1, 0), ( 2, 2), 0),
+ ((1, 1), (-1, 1), ( 0, -3), 1),
+ ((0, 1), ( 2, 0), (-6, 0), 1)
+ );
+
+ for @test -> ($a, $b, $c, $d ) {
+ is challenge($a,$b, $c), $d;
+ }
+
+}
diff --git a/challenge-101/mimosinnet/raku/test.raku b/challenge-101/mimosinnet/raku/test.raku
new file mode 100644
index 0000000000..f75cf266b0
--- /dev/null
+++ b/challenge-101/mimosinnet/raku/test.raku
@@ -0,0 +1,58 @@
+#!/usr/bin/env raku
+
+use Test;
+plan 6;
+
+is-deeply pack-a-spiral(1..4), ((4,3), (1,2));
+is-deeply pack-a-spiral(1..6), ((6,5,4), (1,2,3));
+is-deeply pack-a-spiral(1..12), ((9,8,7,6), (10,11,12,5), (1,2,3,4));
+is-deeply pack-a-spiral(1..13), ((1,2,3,4,5,6,7,8,9,10,11,12,13),);
+is-deeply pack-a-spiral(1..143),
+(
+(35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23),
+(36, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 22),
+(37, 74, 103, 102, 101, 100, 99, 98, 97, 96, 95, 62, 21),
+(38, 75, 104, 125, 124, 123, 122, 121, 120, 119, 94, 61, 20),
+(39, 76, 105, 126, 139, 138, 137, 136, 135, 118, 93, 60, 19),
+(40, 77, 106, 127, 140, 141, 142, 143, 134, 117, 92, 59, 18),
+(41, 78, 107, 128, 129, 130, 131, 132, 133, 116, 91, 58, 17),
+(42, 79, 108, 109, 110, 111, 112, 113, 114, 115, 90, 57, 16),
+(43, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 56, 15),
+(44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 14),
+( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
+);
+is-deeply pack-a-spiral(1..144),
+(
+(34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23),
+(35, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 22),
+(36, 73, 102, 101, 100, 99, 98, 97, 96, 95, 62, 21),
+(37, 74, 103, 124, 123, 122, 121, 120, 119, 94, 61, 20),
+(38, 75, 104, 125, 138, 137, 136, 135, 118, 93, 60, 19),
+(39, 76, 105, 126, 139, 144, 143, 134, 117, 92, 59, 18),
+(40, 77, 106, 127, 140, 141, 142, 133, 116, 91, 58, 17),
+(41, 78, 107, 128, 129, 130, 131, 132, 115, 90, 57, 16),
+(42, 79, 108, 109, 110, 111, 112, 113, 114, 89, 56, 15),
+(43, 80, 81, 82, 83, 84, 85, 86, 87, 88, 55, 14),
+(44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 13),
+( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
+);
+
+sub pack-a-spiral(@list is copy)
+{
+ my $factor = .tail given map { +@list div $_ if @list %% $_ },
+ 1..sqrt(@list);
+
+ my @matrix = @list.keys.rotor($factor).map(*.Array);
+ my @keys;
+
+ while @matrix
+ {
+ @keys.append: @matrix.pop.flat;
+ try @keys.push: @matrix[$_].pop for @matrix.end...0;
+ try @keys.append: @matrix.shift.flat.reverse;
+ try @keys.push: @matrix[$_].shift for ^@matrix;
+ }
+
+ @list[@keys] = @list;
+ @list.rotor($factor);
+}