aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-093/feng-chang/ch-1.raku50
-rwxr-xr-xchallenge-093/feng-chang/ch-2.raku24
-rwxr-xr-xchallenge-094/feng-chang/ch-1.raku18
-rwxr-xr-xchallenge-094/feng-chang/ch-2.raku35
4 files changed, 127 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);
+}
diff --git a/challenge-094/feng-chang/ch-1.raku b/challenge-094/feng-chang/ch-1.raku
new file mode 100755
index 0000000000..deaeb15a2d
--- /dev/null
+++ b/challenge-094/feng-chang/ch-1.raku
@@ -0,0 +1,18 @@
+#!/bin/env raku
+
+sub USAGE {
+ put "$*PROGRAM 0\n" ~
+ "$*PROGRAM 1\n" ~
+ "$*PROGRAM opt bat saw tab pot top was\n" ~
+ "$*PROGRAM x";
+}
+
+sub group-anagrams(@S) {
+ for @S».comb».sort».join.unique -> $foo {
+ put @S.grep({ $_.comb.sort.join eq $foo }).join(', ');
+ }
+}
+
+my @T = <opt bat saw tab pot top was>, [<x>];
+multi MAIN(UInt:D \n) { group-anagrams(@T[n]) }
+multi MAIN(*@S) { @S.elems > 0 ?? group-anagrams(@S) !! USAGE() }
diff --git a/challenge-094/feng-chang/ch-2.raku b/challenge-094/feng-chang/ch-2.raku
new file mode 100755
index 0000000000..dc6bacda24
--- /dev/null
+++ b/challenge-094/feng-chang/ch-2.raku
@@ -0,0 +1,35 @@
+#!/bin/env raku
+
+sub USAGE {
+ put "$*PROGRAM 0\n" ~
+ "$*PROGRAM 1\n";
+}
+
+sub tree-to-list(Any:D $node, Array:D $a --> List:D) {
+ given $node {
+ when Int|Str { $a.push($node) }
+ when Pair { tree-to-list($node.key, $a); tree-to-list($node.value, $a) }
+ when List { tree-to-list($_, $a) for |$node }
+ when Hash { tree-to-list($node.keys.first, $a); tree-to-list($node.values.first, $a) }
+ default { die "got { $_.^name }!" }
+ }
+ (|$a);
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is-deeply |tree-to-list(1, my Array $a .= new), 1, "1 : 1";
+ is-deeply |tree-to-list('x', $a .= new), 'x', "x : x";
+
+ is tree-to-list((1 => 2), $a .= new), (1, 2), "1 => 2 : 1";
+}
+
+my @T = [
+ { 1 => (2 => (4, 5 => (6, 7)), 3) },
+ { 1 => (2 => (4, 5)) },
+];
+
+multi MAIN(UInt:D \n) {
+ put tree-to-list(@T[n], my Array $a .= new);
+}