aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-25 17:42:28 +0000
committerGitHub <noreply@github.com>2023-11-25 17:42:28 +0000
commit117ccf927d99d08c0f35ec271cb928c3cffe650b (patch)
tree6b43fc03d364bfb5c07407b2e83cccd17fd826a8
parent2a408973638c99734d8a4aa883ce3466d425493c (diff)
parent1f0ebce62d94803c9c4477273c96dbd02545c4fb (diff)
downloadperlweeklychallenge-club-117ccf927d99d08c0f35ec271cb928c3cffe650b.tar.gz
perlweeklychallenge-club-117ccf927d99d08c0f35ec271cb928c3cffe650b.tar.bz2
perlweeklychallenge-club-117ccf927d99d08c0f35ec271cb928c3cffe650b.zip
Merge pull request #9131 from rcmlz/ch-244
improved ch2 solution
-rw-r--r--challenge-244/rcmlz/raku/task-two.rakumod22
1 files changed, 20 insertions, 2 deletions
diff --git a/challenge-244/rcmlz/raku/task-two.rakumod b/challenge-244/rcmlz/raku/task-two.rakumod
index 2bbc58ce4a..0992d5fe86 100644
--- a/challenge-244/rcmlz/raku/task-two.rakumod
+++ b/challenge-244/rcmlz/raku/task-two.rakumod
@@ -9,7 +9,25 @@ You are given an array of integers representing the strength.
- power is defined as the square of the largest number in a sequence, multiplied by the smallest.
]
our sub solution(@input) is export {
+
+ my @sorted = @input.sort({$^b cmp $^a});
+ my $n = @sorted.elems;
+
+ # how many sets exists in power-set with max/min
+ my @multiplicators = 1,2,4,8 ... $n;
+
+ my @sets;
+
+ for ^($n - 1) -> $i {
+ @sets.push: @sorted[$i]² * ([+] @sorted[$i+1 .. *-1] »*» @multiplicators)
+ }
+
+ # single element sets + multi elements sets
+ ([+] @sorted.map: *³) + [+] @sets
+}
+
+our sub solution-simple-but-slow(@input) is export {
[+] @input.combinations
- .grep( *.elems )
- .map( { .min * .max**2 } )
+ .race(batch => 2**15, degree => Kernel.cpu-cores)
+ .map( { .elems ?? .min * .max**2 !! 0 } )
} \ No newline at end of file