aboutsummaryrefslogtreecommitdiff
path: root/challenge-151
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-151')
-rw-r--r--challenge-151/mark-anderson/raku/ch-1.raku23
-rw-r--r--challenge-151/mark-anderson/raku/ch-2.raku34
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-151/mark-anderson/raku/ch-1.raku b/challenge-151/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..4797264da4
--- /dev/null
+++ b/challenge-151/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+
+use Test;
+
+is binary-tree-depth('1 | 2 3 | 4 5'), 2;
+is binary-tree-depth('1 | 2 3 | 4 * * 5 | * 6'), 3;
+is binary-tree-depth('1 | 2 3 | 4 * * 5 | 6 * * 7 | 8 * * 9 | 10 *'), 5;
+
+sub binary-tree-depth($str)
+{
+ my $tree := $str.split(/\s\|\s/).map(*.split(/\s/));
+ my $elems = 1;
+ my $depth = 0;
+
+ for $tree -> $nodes
+ {
+ last unless $nodes.elems == $elems;
+ $elems = ($elems - $nodes.comb('*')) * 2;
+ $depth++;
+ }
+
+ $depth;
+}
diff --git a/challenge-151/mark-anderson/raku/ch-2.raku b/challenge-151/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..b725a2b196
--- /dev/null
+++ b/challenge-151/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/env raku
+
+# With help from https://www.geeksforgeeks.org/find-maximum-possible-stolen-value-houses/
+
+use Test;
+
+is rob(2, 4, 5), 7;
+is rob(4, 2, 3, 6, 5, 3), 13;
+
+multi rob(+@houses where .elems == 1)
+{
+ @houses[0];
+}
+
+multi rob(+@houses where .elems == 2)
+{
+ max(@houses[0], @houses[1]);
+}
+
+multi rob(+@houses)
+{
+ my $val1 = @houses[0];
+ my $val2 = max($val1, @houses[1]);
+ my $max;
+
+ for 2..@houses.end -> $i
+ {
+ $max = max(@houses[$i] + $val1, $val2);
+ $val1 = $val2;
+ $val2 = $max;
+ }
+
+ $max;
+}