aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2022-02-07 13:08:23 +0000
committerMark <53903062+andemark@users.noreply.github.com>2022-02-07 13:08:23 +0000
commit640ba1f04df03a2835067fd91cee9a6fbece083a (patch)
tree2228bdd8ae2907eea3b49e4e4406c02d25e3fd07
parentd4fec6b7157921fd3a1f1178f0899696c4d405d5 (diff)
downloadperlweeklychallenge-club-640ba1f04df03a2835067fd91cee9a6fbece083a.tar.gz
perlweeklychallenge-club-640ba1f04df03a2835067fd91cee9a6fbece083a.tar.bz2
perlweeklychallenge-club-640ba1f04df03a2835067fd91cee9a6fbece083a.zip
initial 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..8a4fae9903
--- /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;
+
+sub binary-tree-depth($tree)
+{
+ my @tree = $tree.split(/\s\|\s/).map(*.split(/\s/));
+ my $elems = 1;
+ my $depth;
+
+ while @tree
+ {
+ my $node = shift @tree;
+ return $depth unless $node.elems == $elems;
+ $elems = ($elems - $node.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..bb7d64b1dd
--- /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 < 3)
+{
+ return @houses.head;
+}
+
+multi rob(+@houses where .elems == 3)
+{
+ return @houses[0] + @houses[2];
+}
+
+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;
+}