aboutsummaryrefslogtreecommitdiff
path: root/challenge-075
diff options
context:
space:
mode:
authorJason Messer <jasoncmesser@gmail.com>2020-08-27 17:15:14 -0700
committerJason Messer <jasoncmesser@gmail.com>2020-08-27 17:15:14 -0700
commit6cb074e6b94e2dc94912ad8861f069730da2d00c (patch)
tree52745ff678104fcc2b7e13883297919fd18acff1 /challenge-075
parenta33ba82d4bf61cc6e6f028c5bac893de78c161a6 (diff)
downloadperlweeklychallenge-club-6cb074e6b94e2dc94912ad8861f069730da2d00c.tar.gz
perlweeklychallenge-club-6cb074e6b94e2dc94912ad8861f069730da2d00c.tar.bz2
perlweeklychallenge-club-6cb074e6b94e2dc94912ad8861f069730da2d00c.zip
weekly challenge 75 raku solution by Jason Messer
Diffstat (limited to 'challenge-075')
-rw-r--r--challenge-075/jason-messer/raku/ch-1.p614
-rw-r--r--challenge-075/jason-messer/raku/ch-2.p638
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-075/jason-messer/raku/ch-1.p6 b/challenge-075/jason-messer/raku/ch-1.p6
new file mode 100644
index 0000000000..003931441e
--- /dev/null
+++ b/challenge-075/jason-messer/raku/ch-1.p6
@@ -0,0 +1,14 @@
+#! /usr/bin/env rakudo
+
+sub coin-combinations( Int :$sum, :@coins where .all > 0 ) {
+ my @combinations = 1, |(0 xx $sum);
+
+ for @coins -> $coin {
+ loop (my $i = $coin; $i < @combinations.elems; ++$i) {
+ @combinations[$i] += @combinations[$i - $coin];
+ }
+ }
+ return @combinations.tail;
+}
+
+say coin-combinations :sum(6), :coins([1, 2, 4]);
diff --git a/challenge-075/jason-messer/raku/ch-2.p6 b/challenge-075/jason-messer/raku/ch-2.p6
new file mode 100644
index 0000000000..c466853fab
--- /dev/null
+++ b/challenge-075/jason-messer/raku/ch-2.p6
@@ -0,0 +1,38 @@
+#! /usr/bin/env rakudo
+
+sub largest-rectangle( @a ) {
+ my @r = gather loop (my $i = 0; $i < @a.elems; ++$i) {
+
+ my $height = @a[$i];
+ my $width = 1;
+ loop (my $back = $i - 1; $back >= 0; --$back) {
+ last if @a[$back] < $height;
+ $width++;
+ }
+ loop (my $forward = $i + 1; $forward < @a.elems; ++$forward) {
+ last if @a[$forward] < $height;
+ $width++;
+ }
+ take $width * $height;
+ }
+ return max(@r);
+}
+
+sub print-histogram( @a ) {
+ loop (my $height = max(@a); $height > 0; --$height) {
+ my Str @parts = $height.Str xx 1;
+ @parts.append: @a.map( {$_ >= $height ?? '#' !! ' '} );
+ say @parts.join: ' ';
+ }
+ say "- " x @a.elems + 1;
+ say ' ', @a.join: ' ';
+}
+
+my @A = [2, 1, 4, 5, 3, 7];
+my @B = [3, 2, 3, 5, 7, 5];
+
+print-histogram(@A);
+say "area: ", largest-rectangle(@A);
+
+print-histogram(@B);
+say "area: ", largest-rectangle(@B);