aboutsummaryrefslogtreecommitdiff
path: root/challenge-115
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@gmail.com>2021-06-14 16:45:54 +0100
committerSimon Proctor <simon.proctor@gmail.com>2021-06-14 16:45:54 +0100
commit33d755a877b0c514b6e900869577a3e741cc6d2b (patch)
tree44c2d6e4537d253f1f8921cc83fb2ec4959b360b /challenge-115
parent3ac133dfc3921d7be043c1e5ecfef94cab5c1a76 (diff)
downloadperlweeklychallenge-club-33d755a877b0c514b6e900869577a3e741cc6d2b.tar.gz
perlweeklychallenge-club-33d755a877b0c514b6e900869577a3e741cc6d2b.tar.bz2
perlweeklychallenge-club-33d755a877b0c514b6e900869577a3e741cc6d2b.zip
Challenges 1 and 2 done. Challenge 2 takes a while for 10... going to look at possibly trying something else
Diffstat (limited to 'challenge-115')
-rw-r--r--challenge-115/simon-proctor/raku/ch-2.raku76
1 files changed, 76 insertions, 0 deletions
diff --git a/challenge-115/simon-proctor/raku/ch-2.raku b/challenge-115/simon-proctor/raku/ch-2.raku
new file mode 100644
index 0000000000..b2b368a144
--- /dev/null
+++ b/challenge-115/simon-proctor/raku/ch-2.raku
@@ -0,0 +1,76 @@
+#!/usr/bin/env raku
+
+class Graph {
+ has @.edges;
+
+ method moves-from( :$point ) {
+ @.edges.grep( *.key == $point ).map( *.value );
+ }
+}
+
+#|( Given a triangle of height $N return the possible routes from the top
+ To the bottom right
+)
+multi sub MAIN(UInt $N) {
+ say traverse-triangle($N).join(", ");
+}
+
+multi sub MAIN("test") is hidden-from-USAGE {
+ use Test;
+ ok traverse-triangle(1) (==) <R LH>;
+ ok traverse-triangle(2) (==) <RR LHR LHLH LLHH RLH LRH>;
+ my ( $g1, $s1, $e1 ) = make-triangle-graph( size => 1 );
+ my ( $g2, $s2, $e2 ) = make-triangle-graph( size => 2 );
+ my ( $g3, $s3, $e3 ) = make-triangle-graph( size => 3 );
+ is $s1, 0;
+ is $e1, 2;
+ is $s2, 0;
+ is $e2, 5;
+ is $s3, 0;
+ is $e3, 9;
+ is $g1.edges.elems, 3;
+ is $g2.edges.elems, 9;
+ is $g3.edges.elems, 18;
+ ok $g1.moves-from( point => 0 ) (==) ( ('L', 1), ('R', 2) );
+ done-testing;
+}
+
+sub traverse-triangle( $size ) {
+ my ( $graph, $current, $end ) = make-triangle-graph( :$size );
+
+ return travel-to( $graph, $current, $end );
+}
+
+multi sub travel-to( Graph $g, Int $s, Int $e where $e == $s ) {
+ return [""];
+}
+
+multi sub travel-to( Graph $g, Int $point, Int $e ) {
+ my @routes = [];
+
+ for $g.moves-from(:$point).map( -> ($d, $p ) { [$d] X~ travel-to($g,$p,$e) } ) -> @p {
+ @routes.push($_) for @p.flat;
+ }
+
+ return @routes;
+}
+
+
+sub make-triangle-graph( :$size ) {
+ my $start = 0;
+ my @points = [$start];
+ my $end = @points[*-1];
+ my @edges;
+ for (1..$size) -> $width {
+ my @next = [$end+1..($end+$width+1)];
+ for @points Z @next.rotor(2=>-1) -> ($s,@n) {
+ @edges.push( $s => ( 'L', @n[0]) );
+ @edges.push( $s => ( 'R', @n[1]) );
+ @edges.push( @n[0] => ( 'H', @n[1] ) );
+ }
+ @points = @next;
+ $end = @points[*-1];
+ }
+
+ return Graph.new( :@edges ), $start, $end;
+} \ No newline at end of file