aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrcmlz <19784049+rcmlz@users.noreply.github.com>2023-11-25 15:13:20 +0100
committerrcmlz <19784049+rcmlz@users.noreply.github.com>2023-11-25 15:13:20 +0100
commit1f0ebce62d94803c9c4477273c96dbd02545c4fb (patch)
tree5d44a9cd303e286c7a8d0babd9380f76fd9a99c3
parent69019701d47cc33e5d201616ec8b990aa26dc471 (diff)
downloadperlweeklychallenge-club-1f0ebce62d94803c9c4477273c96dbd02545c4fb.tar.gz
perlweeklychallenge-club-1f0ebce62d94803c9c4477273c96dbd02545c4fb.tar.bz2
perlweeklychallenge-club-1f0ebce62d94803c9c4477273c96dbd02545c4fb.zip
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