aboutsummaryrefslogtreecommitdiff
path: root/challenge-093
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-01-05 19:36:26 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-01-05 19:36:26 +0800
commitd339de8c246c9b8d80460a07e615afc2c83e5002 (patch)
tree9aa5159fee2e35e1a9574d2a85522d798264574b /challenge-093
parentbfae5b89818b43ac4bb5ade673cf4aa9915d4ce6 (diff)
downloadperlweeklychallenge-club-d339de8c246c9b8d80460a07e615afc2c83e5002.tar.gz
perlweeklychallenge-club-d339de8c246c9b8d80460a07e615afc2c83e5002.tar.bz2
perlweeklychallenge-club-d339de8c246c9b8d80460a07e615afc2c83e5002.zip
Challenge 094, Raku solutions
Diffstat (limited to 'challenge-093')
-rwxr-xr-xchallenge-093/feng-chang/ch-1.raku50
-rwxr-xr-xchallenge-093/feng-chang/ch-2.raku24
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-093/feng-chang/ch-1.raku b/challenge-093/feng-chang/ch-1.raku
new file mode 100755
index 0000000000..00a375faa7
--- /dev/null
+++ b/challenge-093/feng-chang/ch-1.raku
@@ -0,0 +1,50 @@
+#!/bin/env raku
+
+grammar Dots {
+ rule TOP { <coord>+ % ',' }
+ rule coord { <[(]> <number> ** 2 % ',' <[)]> }
+ token number { \d+ }
+}
+
+class DotsActions {
+ method TOP($/) { make $<coord>.map(*.made).Array }
+ method coord($/) { make $<number>.map(*.made).Array }
+ method number($/) { make $/.UInt }
+}
+
+sub on-same-line(Array:D $dots -->Bool) {
+ $dots.elems == 2 ||
+ ([==] $dots.map({ .[0] })) ||
+ ([==] (1..^$dots.elems).map({ ($dots[$_;1] - $dots[0;1])/($dots[$_;0] - $dots[0;0]) }));
+}
+
+sub max-dots(Array:D $dots -->UInt:D) {
+ $dots.combinations(2..$dots.elems).grep({ on-same-line($_.Array) })».elems.max;
+}
+
+sub USAGE {
+ put "./ch-1.raku test\n" ~
+ "./ch-1.raku '(1,1), (2,2), (3,3)'\n" ~
+ "./ch-1.raku '(1,1), (2,2), (3,1), (1,3), (5,3)'"
+}
+
+multi MAIN('test') {
+ use Test;
+
+ ok on-same-line([[1, 2], [1, 3], [1, -3]]), 'the 3 points are on the same vertical line';
+ ok on-same-line([[1, 1], [2, 2], [3, 3]]), 'the 3 points are on the same line';
+ nok on-same-line([[1, 1], [2, 2], [3, 1]]), 'the 3 points are not on the same line';
+
+ is max-dots([[1,1], [2,2], [3,3]]), 3, '(1,1), (2,2), (3,3) => 3';
+ is max-dots([[1,1], [2,2], [3,1], [1,3], [5,3]]), 3, '(1,1), (2,2), (3,1), (1,3), (5,3) => 3';
+
+ is Dots.parse('123', :rule('number'), :actions(DotsActions)).made, 123, '"123" => 123';
+ is-deeply Dots.parse('(3,5)', :rule('coord'), :actions(DotsActions)).made, [3,5], '(3,5) => [3,5]';
+ is-deeply Dots.parse('(3, 5)', :rule('coord'), :actions(DotsActions)).made, [3,5], '(3, 5) => [3,5]';
+
+ is-deeply Dots.parse('(3, 5), (1,2)', :actions(DotsActions)).made, [[3,5],[1,2]], '"(3, 5), (1,2)" => [[3,5],[1,2]]';
+}
+
+multi MAIN(Str:D $input) {
+ put max-dots(Dots.parse($input, :actions(DotsActions)).made);
+}
diff --git a/challenge-093/feng-chang/ch-2.raku b/challenge-093/feng-chang/ch-2.raku
new file mode 100755
index 0000000000..ff099926a8
--- /dev/null
+++ b/challenge-093/feng-chang/ch-2.raku
@@ -0,0 +1,24 @@
+#!/bin/env raku
+
+sub traverse(Any:D $node, UInt:D $sum --> UInt:D) {
+ $node.^name ~~ 'Int' ??
+ $node + $sum !!
+ [+] (traverse($_, $sum + $node.key) for |$node.value);
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is traverse(1, 0), 1, '1 : 1';
+ is traverse(1 => 2, 0), 3, '1 => 2 : 3';
+ is traverse(1 => (2, 3), 0), 7, '1 => (2, 3) : 7';
+}
+
+my Array $T .= new(
+ 1 => (2 => (3, 4)),
+ 1 => (2 => 4, 3 => (5, 6)),
+);
+
+multi MAIN(UInt:D \n) {
+ put traverse($T[n], 0);
+}