aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-093/wambash/raku/ch-1.raku59
-rw-r--r--challenge-093/wambash/raku/ch-2.raku36
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-093/wambash/raku/ch-1.raku b/challenge-093/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..8af254ba09
--- /dev/null
+++ b/challenge-093/wambash/raku/ch-1.raku
@@ -0,0 +1,59 @@
+#!/usr/bin/env raku
+
+class Point {
+ has $.x;
+ has $.y;
+
+ multi method COERCE ( ($x,$y) ) {
+ self.new: :$x, :$y
+ }
+}
+
+class Line {
+ has $.a;
+ has $.b;
+
+ multi method from-points(Point:D() $p, Point:D() $q) {
+ when $p.x eqv $q.x { self.new: a=> Inf, b => $p.x }
+ default {
+ my $a = ($q.y-$p.y) / ($q.x-$p.x);
+ my $b = $p.y-$a*$p.x;
+
+ self.new: :$a, :$b;
+ }
+ }
+}
+
+sub max-points ( +@a ) {
+ @a.combinations(2)
+ andthen .categorize: -> [$p,$q] {(.a,.b).raku with Line.from-points( $p, $q )}, as => {|$_}\
+ andthen .map: { .key => .value.unique }\
+ andthen .max: *.value.elems
+}
+
+
+multi MAIN(
+ +@point #= 1 1 2 2 3 1 1 3 3 3 5 3 4 3
+) {
+ @point
+ andthen .batch: 2
+ andthen .&max-points
+ andthen .value.elems
+ andthen .say
+}
+
+multi MAIN(Bool :$test!) {
+ use Test;
+
+ my Point() @p = (1,3),(2,5);
+ is-deeply Line.from-points(|@p), Line.new( a=> 2.0, b=> 1.0 );
+ is-deeply Line.from-points((1,5),(3,7)), Line.new( a=> 1.0, b=> 4.0 );
+ is-deeply Line.from-points((1,2),(1,4)), Line.new( a=> Inf, b=> 1 );
+
+ my Point() @a=(1,1), (2,2), (3,1), (1,3), (3,3),(5,3),(4,3);
+ is max-points( (1,1), (2,2), (3,3) ).value.elems, 3;
+ is max-points( (1,1), (2,2), (3,1), (1,3), (5,3) ).value.elems, 3;
+ is max-points( (1,1), (2,2), (3,1), (1,3), (5,3) ).key, (-1/1,4/1).raku;
+ is max-points( @a ).key, (0/1, 3/1).raku;
+ done-testing;
+}
diff --git a/challenge-093/wambash/raku/ch-2.raku b/challenge-093/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..415884e95a
--- /dev/null
+++ b/challenge-093/wambash/raku/ch-2.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+
+grammar Tree {
+ token TOP { <node> }
+ rule node { '(' ~ ')' [ <number>',' <branch>+ %% ',' ] }
+ token branch { <number> | <node> }
+ token number { \d+ }
+}
+
+class SumPath-actions {
+ method TOP ($/) { make $<node>.made.sum }
+ method node ($/) { make slip $<number>.made X+ $<branch>ยป.made }
+ method branch ($/) { make slip $/.chunks.map: *.value.made }
+ method number ($/) { make +$/ }
+}
+
+sub sum-path (Str $tree) {
+ Tree.parse: $tree, actions => SumPath-actions.new
+ andthen .made
+}
+
+multi MAIN (
+ Str $tree #= "(1,(2,3,4))"
+) {
+ say sum-path $tree
+}
+
+multi MAIN (Bool :$test) {
+ use Test;
+ is Tree.parse( '( 1, (2, 3, 4, ) )', actions => SumPath-actions.new, rule => 'node' ).made, (6,7);
+ is sum-path( '(1,2,3,4)', ), 12;
+ is sum-path( '(1,(2,(3,(4,5))))', ), 15;
+ is sum-path( '(1,(2,3,4))', ), 13;
+ is sum-path( '(1,(2,4),(3,5,6))', ), 26;
+ done-testing;
+}