aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-21 22:16:11 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-21 22:16:11 +1100
commit1d48009058096cbfa05b830cf011af3757da5364 (patch)
tree83375f0f24f0588b0bd39c0ba930eec2af377ab6
parent6fc35c690d937c077ffc9a27161f26b7aae6eca8 (diff)
downloadperlweeklychallenge-club-1d48009058096cbfa05b830cf011af3757da5364.tar.gz
perlweeklychallenge-club-1d48009058096cbfa05b830cf011af3757da5364.tar.bz2
perlweeklychallenge-club-1d48009058096cbfa05b830cf011af3757da5364.zip
[ch-083/jeongoon] Raku Solution added.
-rw-r--r--challenge-083/jeongoon/raku/ch-1.raku18
-rw-r--r--challenge-083/jeongoon/raku/ch-2.raku25
-rw-r--r--challenge-083/jeongoon/raku/ch-2.without-race.raku23
-rw-r--r--challenge-083/jeongoon/raku/ch-2.yet-another-race.raku36
4 files changed, 102 insertions, 0 deletions
diff --git a/challenge-083/jeongoon/raku/ch-1.raku b/challenge-083/jeongoon/raku/ch-1.raku
new file mode 100644
index 0000000000..8028c88596
--- /dev/null
+++ b/challenge-083/jeongoon/raku/ch-1.raku
@@ -0,0 +1,18 @@
+#!/usr/bin/env raku
+sub MAIN ( Str \S ) {
+ S
+ ==> words() # task allow us ignore whitespace so it's safe
+ ==> my @W;
+ @W[ 1 .. * -2 ] # no check but task will give us at least 3 words
+ ==> join("")
+ ==> chars()
+ ==> my @A; # captured value is always a sequences
+ $*ERR.say( "Words: {@W.raku}" );
+ say @A[0]; # so we have to .[0]
+}
+
+=begin of-course-this-is-shorter
+
+say S.words.[ 1 .. * -2 ].join("").chars;
+
+=end of-course-this-is-shorter
diff --git a/challenge-083/jeongoon/raku/ch-2.raku b/challenge-083/jeongoon/raku/ch-2.raku
new file mode 100644
index 0000000000..45fbdcee95
--- /dev/null
+++ b/challenge-083/jeongoon/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env raku
+# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
+# vim: set et ts=4 sw=4:
+
+# personal-blog: https://dev.to/jeongoon/weekly-challenge-083-task-2-raku-2cjm
+
+use v6.d;
+
+multi MAIN ($n where * > 0) { say 0 }
+multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
+ my $s = @n.sum;
+ @n.combinations( 1..^ @n ).
+ race( :8degree:500batch ).
+ map(
+ -> \n {
+ with $s - 2 * n.sum { # same as ( $s- n.sum ) - n.sum
+ next if $_ < 0;
+ $_ => n.elems
+ }
+ } ).
+ race( :8degree:5000batch ).
+ min.
+ value.
+ say
+}
diff --git a/challenge-083/jeongoon/raku/ch-2.without-race.raku b/challenge-083/jeongoon/raku/ch-2.without-race.raku
new file mode 100644
index 0000000000..26dc21fbec
--- /dev/null
+++ b/challenge-083/jeongoon/raku/ch-2.without-race.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
+# vim: set et ts=4 sw=4:
+
+# personal-blog: https://dev.to/jeongoon/weekly-challenge-083-task-2-raku-2cjm
+
+use v6.d;
+
+multi MAIN ($n where * > 0) { say 0 }
+multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
+ my $s = @n.sum;
+ @n.combinations( 1..^ @n ).
+ map(
+ -> \n {
+ with $s - 2 * n.sum { # same as ( $s- n.sum ) - n.sum
+ next if $_ < 0;
+ $_ => n.elems
+ }
+ } ).
+ min.
+ value.
+ say
+}
diff --git a/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku b/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku
new file mode 100644
index 0000000000..911d0f2136
--- /dev/null
+++ b/challenge-083/jeongoon/raku/ch-2.yet-another-race.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*-
+# vim: set et ts=4 sw=4:
+
+# personal-blog: https://dev.to/jeongoon/weekly-challenge-083-task-2-raku-2cjm
+# I hoped that this will be faster but it wasn't :-/
+
+use v6.d;
+
+multi MAIN ($n where * > 0) { say 0 }
+multi MAIN (*@n where { @n.all ~~ Int and @n.all > 0 }) {
+ my \B = @n.Bag;
+ my \S = @n.sum;
+ my $half-len-floor = @n.elems div 2;
+ @n.combinations( 1.. $half-len-floor ).
+ race( :8degree:1000batch ).
+ map(
+ -> $G
+ {
+ my $a = $G.sum; # sum of selected group
+ my $b = (S - $G.sum);
+ given $a - $b {
+ if $_ < 0 {
+ -$_ => $G.elems
+ } elsif $_ == 0 {
+ |( 0 => $G.elems, 0 => (B (-) $G.Bag).total )
+ } else {
+ $_ => (B (-) $G.Bag).total
+ }
+ }
+ }
+ ).
+ race( :8degree:1000batch ).
+ min.
+ say
+}