aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/feng-chang
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-08-08 16:39:26 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-08-08 16:39:26 +0800
commit1139f7cfdb562e91c2c282bad7155ecfe4d1a7ee (patch)
treed459e4d53be9200a7da2cdd1f22716ec1bac7c03 /challenge-151/feng-chang
parent0462e0b7406154f2daad93d84648d65a945b9458 (diff)
downloadperlweeklychallenge-club-1139f7cfdb562e91c2c282bad7155ecfe4d1a7ee.tar.gz
perlweeklychallenge-club-1139f7cfdb562e91c2c282bad7155ecfe4d1a7ee.tar.bz2
perlweeklychallenge-club-1139f7cfdb562e91c2c282bad7155ecfe4d1a7ee.zip
challenge 151, add ch-1.raku and test.raku
Diffstat (limited to 'challenge-151/feng-chang')
-rwxr-xr-xchallenge-151/feng-chang/raku/ch-1.raku21
-rwxr-xr-xchallenge-151/feng-chang/raku/ch-2.raku7
-rwxr-xr-xchallenge-151/feng-chang/raku/test.raku28
3 files changed, 53 insertions, 3 deletions
diff --git a/challenge-151/feng-chang/raku/ch-1.raku b/challenge-151/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..baaff0d101
--- /dev/null
+++ b/challenge-151/feng-chang/raku/ch-1.raku
@@ -0,0 +1,21 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $s);
+
+my @Tree;
+my UInt $depth = 0;
+$s.split('|').map({ @Tree[$depth++] = .words.map({ $_ eq '*' ?? Nil !! $_.Int }).Array });
+@Tree = [] if +@Tree[0] == 0;
+
+my $min-depth = +@Tree;
+for ^@Tree -> $row {
+ for ^@Tree[$row] -> $col {
+ next without @Tree[$row;$col];
+ with @Tree[$row+1] {
+ next with @Tree[$row+1;2*$col];
+ next with @Tree[$row+1;2*$col+1];
+ $min-depth = $row + 1 if $row + 1 < $min-depth;
+ }
+ }
+}
+put $min-depth;
diff --git a/challenge-151/feng-chang/raku/ch-2.raku b/challenge-151/feng-chang/raku/ch-2.raku
index 4fa124eac5..ffcb00fe5e 100755
--- a/challenge-151/feng-chang/raku/ch-2.raku
+++ b/challenge-151/feng-chang/raku/ch-2.raku
@@ -4,6 +4,7 @@ unit sub MAIN(*@V);
put max-gain(@V);
-multi max-gain() { 0 }
-multi max-gain(@V where *.elems == 1|2) { @V.max }
-multi max-gain(@V --> UInt:D) { max(@V[0] + max-gain(@V.tail(*-2)), max-gain(@V.tail(*-1))) }
+multi max-gain() { 0 }
+multi max-gain([$a]) { $a }
+multi max-gain([$a, $b]) { max($a, $b) }
+multi max-gain(@V) { max(@V[0] + max-gain(@V.tail(*-2)), max-gain(@V.tail(*-1))) }
diff --git a/challenge-151/feng-chang/raku/test.raku b/challenge-151/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..aed18159dc
--- /dev/null
+++ b/challenge-151/feng-chang/raku/test.raku
@@ -0,0 +1,28 @@
+#!/bin/env raku
+
+# The Weekly Challenge 151
+use Test;
+
+sub pwc-test(Str:D $script, *@input) {
+ my ($expect, $assertion) = @input.splice(*-2, 2);
+ my $p = run $script, |@input, :out;
+ is $p.out.slurp(:close).chomp, $expect, $assertion;
+}
+
+# Task 1, Binary Tree Depth
+pwc-test './ch-1.raku', '', 0, 'Binary Tree Depth: "" => 0';
+pwc-test './ch-1.raku', '1', 1, 'Binary Tree Depth: "1" => 1';
+pwc-test './ch-1.raku', '1 | 2', 2, 'Binary Tree Depth: "1 | 2" => 2';
+pwc-test './ch-1.raku', '1 | 2 3', 2, 'Binary Tree Depth: "1 | 2 3" => 2';
+pwc-test './ch-1.raku', '1 | 2 3 | 4', 2, 'Binary Tree Depth: "1 | 2 3 | 4" => 2';
+pwc-test './ch-1.raku', '1 | 2 3 | 4 5', 2, 'Binary Tree Depth: "1 | 2 3 | 4 5" => 2';
+pwc-test './ch-1.raku', '1 | 2 3 | 4 5 6', 3, 'Binary Tree Depth: "1 | 2 3 | 4 5 6" => 3';
+pwc-test './ch-1.raku', '1 | 2 3 | 4 5 * 6', 3, 'Binary Tree Depth: "1 | 2 3 | 4 5 * 6" => 3';
+
+pwc-test './ch-1.raku', '1 | 2 3 | 4 * * 5 | * 6', 3, 'Binary Tree Depth: "1 | 2 3 | 4 * * 5 | * 6" => 3';
+
+# Task 2, Rob The House
+pwc-test './ch-2.raku', |<2 4 5>, 7, 'Rob The House: (2, 4, 5) => 7';
+pwc-test './ch-2.raku', |<4 2 3 6 5 3>, 13, 'Rob The House: (4, 2, 3, 6, 5, 3) => 13';
+
+done-testing;