aboutsummaryrefslogtreecommitdiff
path: root/challenge-101
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-02-22 22:25:54 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-02-22 22:25:54 +0800
commit3cc47d00aee13d50c872499cecf574539a4cbe2a (patch)
tree77b6f6a1e8cae075e3c70fa1b4f82e85aa390743 /challenge-101
parent572994875f5972916deabeb84f3648ac4640107b (diff)
downloadperlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.tar.gz
perlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.tar.bz2
perlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.zip
challenge 101, raku solutions
Diffstat (limited to 'challenge-101')
-rwxr-xr-xchallenge-101/feng-chang/raku/ch-1.raku61
-rwxr-xr-xchallenge-101/feng-chang/raku/ch-2.raku23
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-101/feng-chang/raku/ch-1.raku b/challenge-101/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..5e76ef3cef
--- /dev/null
+++ b/challenge-101/feng-chang/raku/ch-1.raku
@@ -0,0 +1,61 @@
+#!/bin/env raku
+
+sub get-tight-mn(UInt:D \size --> List:D) {
+ (1..size X 1..size).grep({ .[0] ≤ .[1] and .[0] * .[1] == size}).min({ .[1] - .[0] });
+}
+
+sub tighten(*@A) {
+ my UInt ($h, $w) = get-tight-mn(@A.elems);
+ my @R;
+ @R[^$h;^$w] = '.' xx ∞;
+ my UInt ($y, $x) = ($h - 1, 0);
+ while @A.elems > 0 {
+ @R[$y;$x++] = $_ for @A.splice(0, $w);
+
+ --$x;
+ --$h;
+ last unless $h > 0;
+ @R[--$y;$x] = $_ for @A.splice(0, $h);
+
+ --$w;
+ last unless $w > 0;
+ @R[$y;--$x] = $_ for @A.splice(0, $w);
+
+ --$h;
+ last unless $h > 0;
+ @R[++$y;$x] = $_ for @A.splice(0, $h);
+
+ ++$x;
+ --$w;
+ }
+
+ @R;
+}
+
+sub show(*@A) {
+ my $width = @A[*;*]».chars.max;
+ put @A[$_]».fmt("%{ $width }s").join(' ') for ^@A.elems;
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is-deeply get-tight-mn(6), (2, 3), 'get-tight-mn(6) => 2, 3';
+ is-deeply get-tight-mn(5), (1, 5), 'get-tight-mn(5) => 1, 5';
+ is-deeply get-tight-mn(9), (3, 3), 'get-tight-mn(9) => 3, 3';
+ is-deeply get-tight-mn(24), (4, 6), 'get-tight-mn(24) => 4, 6';
+
+ is-deeply tighten(1, 2, 3), [[1, 2, 3],], 'tighten(1..3) is correct';
+ is-deeply tighten(1..4), [[4, 3], [1, 2]], 'tighten(1..4) is correct';
+ is-deeply tighten(1..9), [[7, 6, 5], [8, 9, 4], [1, 2, 3]], 'tighten(1..9) is correct';
+
+ done-testing;
+}
+
+multi MAIN(UInt:D \n) {
+ show tighten(1..n);
+}
+
+multi MAIN(*@A) {
+ show tighten(@A);
+}
diff --git a/challenge-101/feng-chang/raku/ch-2.raku b/challenge-101/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..3e2823e892
--- /dev/null
+++ b/challenge-101/feng-chang/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#!/bin/env raku
+
+class Point {
+ has Int $.x;
+ has Int $.y;
+}
+
+sub area(Point:D $a, Point:D $b, Point:D $c) {
+ abs(($a.x * ($b.y - $c.y) + $b.x * ($c.y - $a.y) + $c.x * ($a.y - $b.y)) / 2);
+}
+
+sub USAGE() {
+ say "Usage: {$*PROGRAM-NAME} <6 integers>";
+}
+
+multi MAIN(*@A where @A.elems == 6 && @A.all ~~ Int) {
+ my Point $a .= new(x => @A[0], y => @A[1]);
+ my Point $b .= new(x => @A[2], y => @A[3]);
+ my Point $c .= new(x => @A[4], y => @A[5]);
+ my Point $o .= new(x => 0, y => 0);
+
+ say +(area($a, $b, $c) == area($a, $b, $o) + area($b, $c, $o) + area($c, $a, $o));
+}