aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarnesom <arne@bbop.org>2020-10-25 21:03:30 +0100
committerarnesom <arne@bbop.org>2020-10-25 21:03:30 +0100
commit5ab6de2c14b0d85e87f275aad3a12ea04bb8effb (patch)
tree93478411ff6e6ed7be50c484bb527ad2b5814421
parent6bcc61fd124f3f54cc1c0c55881b48f0a0aa05e6 (diff)
downloadperlweeklychallenge-club-5ab6de2c14b0d85e87f275aad3a12ea04bb8effb.tar.gz
perlweeklychallenge-club-5ab6de2c14b0d85e87f275aad3a12ea04bb8effb.tar.bz2
perlweeklychallenge-club-5ab6de2c14b0d85e87f275aad3a12ea04bb8effb.zip
083
-rw-r--r--challenge-083/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-083/arne-sommer/raku/ch-1.p611
-rwxr-xr-xchallenge-083/arne-sommer/raku/ch-2.p643
-rwxr-xr-xchallenge-083/arne-sommer/raku/flip-array43
-rwxr-xr-xchallenge-083/arne-sommer/raku/words-length11
5 files changed, 109 insertions, 0 deletions
diff --git a/challenge-083/arne-sommer/blog.txt b/challenge-083/arne-sommer/blog.txt
new file mode 100644
index 0000000000..f56945a2da
--- /dev/null
+++ b/challenge-083/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/wordly-array.html
diff --git a/challenge-083/arne-sommer/raku/ch-1.p6 b/challenge-083/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..1f70965a6f
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,11 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $S where $S.words.elems >= 3,
+ :v($verbose));
+
+my @words = $S.words;
+my $s = @words[1.. @words.end -1].join;
+
+say ": '{ $s }'" if $verbose;
+
+say $s.chars;
diff --git a/challenge-083/arne-sommer/raku/ch-2.p6 b/challenge-083/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..30e1fd58fb
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+subset Positive of Numeric where * > 0;
+
+unit sub MAIN (*@A where @A.elems > 0 && all(@A) ~~ Positive, :v($verbose));
+
+my %sum;
+
+do_it( (), @A);
+
+my $lowest-sum = %sum.keys.grep( * >= 0).min;
+
+say ": Lowest non-negative sum: $lowest-sum" if $verbose;
+
+my @flip;
+
+for @(%sum{$lowest-sum}) -> @candidate
+{
+ state $index = 0;
+ @flip[$index] = @candidate.grep( * < 0).elems;
+ say ": Match $index: [{ @candidate.join(", ") }] with @flip[$index] flip(s)" if $verbose;
+ $index++;
+}
+
+say ": Flips: [{ @flip.join(", ") }]" if $verbose;
+
+say @flip.min;
+
+sub do_it (@left, @right is copy)
+{
+ my $current = @right.shift;
+
+ unless defined $current
+ {
+ say ": Candidate [{ @left.join(", ") }] with sum {@left.sum }" if $verbose;
+
+ %sum{@left.sum}.push: @left;
+ return;
+ }
+
+ do_it((@left, $current).flat, @right);
+ do_it((@left, -$current).flat, @right);
+}
diff --git a/challenge-083/arne-sommer/raku/flip-array b/challenge-083/arne-sommer/raku/flip-array
new file mode 100755
index 0000000000..30e1fd58fb
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/flip-array
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+subset Positive of Numeric where * > 0;
+
+unit sub MAIN (*@A where @A.elems > 0 && all(@A) ~~ Positive, :v($verbose));
+
+my %sum;
+
+do_it( (), @A);
+
+my $lowest-sum = %sum.keys.grep( * >= 0).min;
+
+say ": Lowest non-negative sum: $lowest-sum" if $verbose;
+
+my @flip;
+
+for @(%sum{$lowest-sum}) -> @candidate
+{
+ state $index = 0;
+ @flip[$index] = @candidate.grep( * < 0).elems;
+ say ": Match $index: [{ @candidate.join(", ") }] with @flip[$index] flip(s)" if $verbose;
+ $index++;
+}
+
+say ": Flips: [{ @flip.join(", ") }]" if $verbose;
+
+say @flip.min;
+
+sub do_it (@left, @right is copy)
+{
+ my $current = @right.shift;
+
+ unless defined $current
+ {
+ say ": Candidate [{ @left.join(", ") }] with sum {@left.sum }" if $verbose;
+
+ %sum{@left.sum}.push: @left;
+ return;
+ }
+
+ do_it((@left, $current).flat, @right);
+ do_it((@left, -$current).flat, @right);
+}
diff --git a/challenge-083/arne-sommer/raku/words-length b/challenge-083/arne-sommer/raku/words-length
new file mode 100755
index 0000000000..1f70965a6f
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/words-length
@@ -0,0 +1,11 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $S where $S.words.elems >= 3,
+ :v($verbose));
+
+my @words = $S.words;
+my $s = @words[1.. @words.end -1].join;
+
+say ": '{ $s }'" if $verbose;
+
+say $s.chars;