From 640ba1f04df03a2835067fd91cee9a6fbece083a Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 13:08:23 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-1.raku | 23 ++++++++++++++++++++ challenge-151/mark-anderson/raku/ch-2.raku | 34 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-151/mark-anderson/raku/ch-1.raku create mode 100644 challenge-151/mark-anderson/raku/ch-2.raku 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; +} -- cgit From 0468084b856d79ab5dca03235eab017a4d1ebfc4 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 13:17:27 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-2.raku | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-2.raku b/challenge-151/mark-anderson/raku/ch-2.raku index bb7d64b1dd..bb9002588d 100644 --- a/challenge-151/mark-anderson/raku/ch-2.raku +++ b/challenge-151/mark-anderson/raku/ch-2.raku @@ -7,14 +7,19 @@ use Test; is rob(2, 4, 5), 7; is rob(4, 2, 3, 6, 5, 3), 13; -multi rob(+@houses where .elems < 3) +multi rob(+@houses where .elems == 1) { - return @houses.head; + return @houses[0]; +} + +multi rob(+@houses where .elems == 2) +{ + return max(@houses[0], @houses[1]); } multi rob(+@houses where .elems == 3) { - return @houses[0] + @houses[2]; + return max(@houses[1], @houses[0] + @houses[2]); } multi rob(+@houses) -- cgit From 2c3b322f0935a4435806cb0b1b408a5491f58307 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 13:24:46 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-2.raku | 5 ----- 1 file changed, 5 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-2.raku b/challenge-151/mark-anderson/raku/ch-2.raku index bb9002588d..2aaca7e450 100644 --- a/challenge-151/mark-anderson/raku/ch-2.raku +++ b/challenge-151/mark-anderson/raku/ch-2.raku @@ -17,11 +17,6 @@ multi rob(+@houses where .elems == 2) return max(@houses[0], @houses[1]); } -multi rob(+@houses where .elems == 3) -{ - return max(@houses[1], @houses[0] + @houses[2]); -} - multi rob(+@houses) { my $val1 = @houses[0]; -- cgit From 69c0b9d68d589493beb80934b7407b9fb004d1d6 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 13:28:58 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-2.raku b/challenge-151/mark-anderson/raku/ch-2.raku index 2aaca7e450..b725a2b196 100644 --- a/challenge-151/mark-anderson/raku/ch-2.raku +++ b/challenge-151/mark-anderson/raku/ch-2.raku @@ -9,12 +9,12 @@ is rob(4, 2, 3, 6, 5, 3), 13; multi rob(+@houses where .elems == 1) { - return @houses[0]; + @houses[0]; } multi rob(+@houses where .elems == 2) { - return max(@houses[0], @houses[1]); + max(@houses[0], @houses[1]); } multi rob(+@houses) -- cgit From 4202406099de9f62e170cc0cf3d0357006e6e156 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 13:50:04 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-1.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-1.raku b/challenge-151/mark-anderson/raku/ch-1.raku index 8a4fae9903..bdbf7dc7a8 100644 --- a/challenge-151/mark-anderson/raku/ch-1.raku +++ b/challenge-151/mark-anderson/raku/ch-1.raku @@ -9,13 +9,13 @@ sub binary-tree-depth($tree) { my @tree = $tree.split(/\s\|\s/).map(*.split(/\s/)); my $elems = 1; - my $depth; + my $depth = 0; while @tree { - my $node = shift @tree; - return $depth unless $node.elems == $elems; - $elems = ($elems - $node.comb('*')) * 2; + my $nodes := shift @tree; + return $depth unless $nodes.elems == $elems; + $elems = ($elems - $nodes.comb('*')) * 2; $depth++; } -- cgit From a1bf165258539b0aa7117dbfebce87aae1f60cd3 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 17:01:56 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-1.raku | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-1.raku b/challenge-151/mark-anderson/raku/ch-1.raku index bdbf7dc7a8..21d3f470ac 100644 --- a/challenge-151/mark-anderson/raku/ch-1.raku +++ b/challenge-151/mark-anderson/raku/ch-1.raku @@ -11,10 +11,9 @@ sub binary-tree-depth($tree) my $elems = 1; my $depth = 0; - while @tree + for @tree -> $nodes { - my $nodes := shift @tree; - return $depth unless $nodes.elems == $elems; + last unless $nodes.elems == $elems; $elems = ($elems - $nodes.comb('*')) * 2; $depth++; } -- cgit From 512732b7f20ab5fc2f32dd307092bb82b58c97e1 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 7 Feb 2022 17:21:38 +0000 Subject: initial 151 --- challenge-151/mark-anderson/raku/ch-1.raku | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/challenge-151/mark-anderson/raku/ch-1.raku b/challenge-151/mark-anderson/raku/ch-1.raku index 21d3f470ac..4797264da4 100644 --- a/challenge-151/mark-anderson/raku/ch-1.raku +++ b/challenge-151/mark-anderson/raku/ch-1.raku @@ -4,14 +4,15 @@ 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($tree) +sub binary-tree-depth($str) { - my @tree = $tree.split(/\s\|\s/).map(*.split(/\s/)); + my $tree := $str.split(/\s\|\s/).map(*.split(/\s/)); my $elems = 1; my $depth = 0; - for @tree -> $nodes + for $tree -> $nodes { last unless $nodes.elems == $elems; $elems = ($elems - $nodes.comb('*')) * 2; -- cgit