aboutsummaryrefslogtreecommitdiff
path: root/challenge-075
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-09-07 17:24:04 +0800
committer冯昶 <seaker@qq.com>2020-09-07 17:24:04 +0800
commitedf6471d905ff6305ff216d3dcdc5e598484aee6 (patch)
tree90634890d87571a4ba175d18cfa1f807bdfdd759 /challenge-075
parentfcd02aeeda87c122867e4173ad34ac8e4c752aa2 (diff)
parent404d4e54bf6da59ce8a0ae57599c831d180b83f9 (diff)
downloadperlweeklychallenge-club-edf6471d905ff6305ff216d3dcdc5e598484aee6.tar.gz
perlweeklychallenge-club-edf6471d905ff6305ff216d3dcdc5e598484aee6.tar.bz2
perlweeklychallenge-club-edf6471d905ff6305ff216d3dcdc5e598484aee6.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-075')
-rw-r--r--challenge-075/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-075/arne-sommer/raku/ch-1.raku28
-rwxr-xr-xchallenge-075/arne-sommer/raku/ch-2.raku68
-rwxr-xr-xchallenge-075/arne-sommer/raku/coins-sum21
-rwxr-xr-xchallenge-075/arne-sommer/raku/coins-sum228
-rwxr-xr-xchallenge-075/arne-sommer/raku/lrh39
-rwxr-xr-xchallenge-075/arne-sommer/raku/lrh-histogram68
7 files changed, 253 insertions, 0 deletions
diff --git a/challenge-075/arne-sommer/blog.txt b/challenge-075/arne-sommer/blog.txt
new file mode 100644
index 0000000000..9bf039d28c
--- /dev/null
+++ b/challenge-075/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/coins-rectangles.html
diff --git a/challenge-075/arne-sommer/raku/ch-1.raku b/challenge-075/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..1bad0aeb26
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,28 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (NonNegativeInt $S where $S >= 1,
+ *@C where @C.elems >= 1 &&
+ all(@C) ~~ NonNegativeInt &&
+ all(@C) <= $S,
+ :v(:$verbose));
+
+my @coins = @C.unique;
+my @source;
+
+for @coins -> $coin
+{
+ @source.append: $coin xx ($S div $coin);
+}
+
+if $verbose
+{
+ say ": Sum: $S";
+ say ": Coins: " ~ @C.join(", ");
+ say ": Unique coins: " ~ @coins.join(", ");
+ say ": Source: " ~ @source.join(", ");
+}
+
+.join(", ").say for @source.combinations(1..$S).grep({ .sum == $S }).unique(:with(&[eqv]));
+
diff --git a/challenge-075/arne-sommer/raku/ch-2.raku b/challenge-075/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..7bfef17a7b
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,68 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ NonNegativeInt, :v(:$verbose), :h(:$histogram));
+
+my $end = @A.end;
+
+my @solutions;
+my $max = -1;
+
+for 0 .. $end -> $from
+{
+ for $from .. $end -> $to
+ {
+ my $height = min(@A[$from .. $to]);
+ my $width = $to - $from +1;
+ my $size = $height * $width;
+ say ": \@A[$from .. $to] -> ({ @A[$from .. $to] }) w:$width h:$height s:$size" if $verbose;
+
+ if $size >= $max
+ {
+ if $size > $max
+ {
+ @solutions = ();
+ $max = $size;
+ say ": New max: $max" if $verbose;
+ }
+ @solutions.push: @A[$from .. $to].join(", ");
+ }
+ }
+}
+
+if $verbose
+{
+ say ": columns: $_" for @solutions;
+}
+
+say $max;
+
+if $histogram
+{
+ say '';
+ my $height = @A.max;
+ my $width = $height.chars;
+
+ for $height ... 1 -> $row
+ {
+ print "{ $row.fmt("%{$width}d") } ";
+ for 0 .. $end -> $index
+ {
+ print @A[$index] >= $row
+ ?? ('#' x $width ~ " ")
+ !! ' ' x $width +1;
+ }
+ say '';
+ }
+
+ say "-" x 6 + $width * ($end +2);
+
+ print ' ' x $width +1;
+
+ for 0 .. $end -> $index
+ {
+ print @A[$index].fmt("%{$width}d") ~ " ";
+ }
+ say '';
+}
diff --git a/challenge-075/arne-sommer/raku/coins-sum b/challenge-075/arne-sommer/raku/coins-sum
new file mode 100755
index 0000000000..3e04ac87ce
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/coins-sum
@@ -0,0 +1,21 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (NonNegativeInt $S where $S >= 1,
+ *@C where @C.elems >= 1 &&
+ all(@C) ~~ NonNegativeInt &&
+ all(@C) <= $S,
+ :v(:$verbose));
+
+my @source;
+
+for @C -> $coin
+{
+ @source.push: $coin for ^($S div $coin);
+}
+
+say ": " ~ @source.join(", ") if $verbose;
+
+.join(", ").say for @source.combinations(1..$S).grep({ .sum == $S }).unique(:with(&[eqv]));
+
diff --git a/challenge-075/arne-sommer/raku/coins-sum2 b/challenge-075/arne-sommer/raku/coins-sum2
new file mode 100755
index 0000000000..1bad0aeb26
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/coins-sum2
@@ -0,0 +1,28 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (NonNegativeInt $S where $S >= 1,
+ *@C where @C.elems >= 1 &&
+ all(@C) ~~ NonNegativeInt &&
+ all(@C) <= $S,
+ :v(:$verbose));
+
+my @coins = @C.unique;
+my @source;
+
+for @coins -> $coin
+{
+ @source.append: $coin xx ($S div $coin);
+}
+
+if $verbose
+{
+ say ": Sum: $S";
+ say ": Coins: " ~ @C.join(", ");
+ say ": Unique coins: " ~ @coins.join(", ");
+ say ": Source: " ~ @source.join(", ");
+}
+
+.join(", ").say for @source.combinations(1..$S).grep({ .sum == $S }).unique(:with(&[eqv]));
+
diff --git a/challenge-075/arne-sommer/raku/lrh b/challenge-075/arne-sommer/raku/lrh
new file mode 100755
index 0000000000..76a45978bf
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/lrh
@@ -0,0 +1,39 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ NonNegativeInt, :v(:$verbose));
+
+my $end = @A.end;
+
+my @solutions;
+my $max = -1;
+
+for 0 .. $end -> $from
+{
+ for $from .. $end -> $to
+ {
+ my $height = min(@A[$from .. $to]);
+ my $width = $to - $from +1;
+ my $size = $height * $width;
+ say ": \@A[$from .. $to] -> ({ @A[$from .. $to] }) w:$width h:$height s:$size" if $verbose;
+
+ if $size >= $max
+ {
+ if $size > $max
+ {
+ @solutions = ();
+ $max = $size;
+ say ": New max: $max" if $verbose;
+ }
+ @solutions.push: @A[$from .. $to].join(", ");
+ }
+ }
+}
+
+if $verbose
+{
+ say ": columns: $_" for @solutions;
+}
+
+say $max;
diff --git a/challenge-075/arne-sommer/raku/lrh-histogram b/challenge-075/arne-sommer/raku/lrh-histogram
new file mode 100755
index 0000000000..7bfef17a7b
--- /dev/null
+++ b/challenge-075/arne-sommer/raku/lrh-histogram
@@ -0,0 +1,68 @@
+#! /usr/bin/env raku
+
+subset NonNegativeInt of Int where * >= 0;
+
+unit sub MAIN (*@A where @A.elems >= 1 && all(@A) ~~ NonNegativeInt, :v(:$verbose), :h(:$histogram));
+
+my $end = @A.end;
+
+my @solutions;
+my $max = -1;
+
+for 0 .. $end -> $from
+{
+ for $from .. $end -> $to
+ {
+ my $height = min(@A[$from .. $to]);
+ my $width = $to - $from +1;
+ my $size = $height * $width;
+ say ": \@A[$from .. $to] -> ({ @A[$from .. $to] }) w:$width h:$height s:$size" if $verbose;
+
+ if $size >= $max
+ {
+ if $size > $max
+ {
+ @solutions = ();
+ $max = $size;
+ say ": New max: $max" if $verbose;
+ }
+ @solutions.push: @A[$from .. $to].join(", ");
+ }
+ }
+}
+
+if $verbose
+{
+ say ": columns: $_" for @solutions;
+}
+
+say $max;
+
+if $histogram
+{
+ say '';
+ my $height = @A.max;
+ my $width = $height.chars;
+
+ for $height ... 1 -> $row
+ {
+ print "{ $row.fmt("%{$width}d") } ";
+ for 0 .. $end -> $index
+ {
+ print @A[$index] >= $row
+ ?? ('#' x $width ~ " ")
+ !! ' ' x $width +1;
+ }
+ say '';
+ }
+
+ say "-" x 6 + $width * ($end +2);
+
+ print ' ' x $width +1;
+
+ for 0 .. $end -> $index
+ {
+ print @A[$index].fmt("%{$width}d") ~ " ";
+ }
+ say '';
+}