aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2022-02-19 18:52:39 -0500
committerrir <rirans@comcast.net>2022-02-19 18:52:39 -0500
commitb20b7fb576f0648e12690950e0cd3198ca3a2943 (patch)
tree9429e7d6a7cbde009ebb279487aa5e09fe41c3c2
parentebae5b3c087b716ca482cc7d2564957f966cc540 (diff)
downloadperlweeklychallenge-club-b20b7fb576f0648e12690950e0cd3198ca3a2943.tar.gz
perlweeklychallenge-club-b20b7fb576f0648e12690950e0cd3198ca3a2943.tar.bz2
perlweeklychallenge-club-b20b7fb576f0648e12690950e0cd3198ca3a2943.zip
Just ch-1.raku
-rw-r--r--challenge-152/0rir/raku/ch-1.raku65
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-152/0rir/raku/ch-1.raku b/challenge-152/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..0441a0a249
--- /dev/null
+++ b/challenge-152/0rir/raku/ch-1.raku
@@ -0,0 +1,65 @@
+#!/usr/bin/env
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+use Test;
+
+constant TEST = True;
+
+# Triangle Sum Path
+constant indent = 18;
+
+my @test =
+ [[ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8]], [1,3,2,0,2]],
+ [[ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9]], [5,2,1,0,1]],
+ [[ [1], [2,3], [4,5,6], [7,8,9,0], [1,2,3,4,5],
+ [6,7,8,9,0,1] ], [1,2,4,0,1,0]],
+ [[ [1], [2,3], [4,5,6], [7,8,9,0], [1,2,3,4,5],
+ [6,7,8,9,0,1], [2,3,4,5,6,7,9]], [1,2,4,0,1,0,2]],
+;
+
+for @test[0][0] -> @t {
+ triangle-sum( @t, indent);
+}
+
+exit unless TEST;
+
+sub triangle-sum( @ary, $indent --> Array ) {
+ my $indentation = $indent;
+
+ t-valid( @ary) or die 'non-triangle';
+
+ {
+ $_ = @ary.raku;
+ s:g/ ' ' //;
+ s:g/ ',[' /, [/;
+ s/'[['/[ [/;
+ s/']]'/] ]/;
+
+ say "Input: \$triangle = $_\n";
+ }
+
+ my @path;
+
+ for @ary -> @a {
+ print ' ' x $indentation, @a.split( ' '), "\n";
+ --$indentation;
+ @path.push( @a.min);
+ }
+ say "\nOutput: ", ([+] @path), "\n\nMinimum Sum Path = ",
+ @path.join( ' + ' ), ' => ', [+] @path;
+
+ @path; # for tests
+}
+
+sub t-valid( @ary --> Bool ) {
+ for 0 … @ary.end -> $ix {
+ return False if @ary[$ix].elems ≠ $ix +1;
+ }
+ True;
+}
+
+plan @test.elems;
+for @test -> @t {
+ is-deeply triangle-sum( @t[0], indent), @t[1], 'path';
+}
+done-testing;