diff options
| author | Mark <53903062+andemark@users.noreply.github.com> | 2023-11-21 12:28:16 +0000 |
|---|---|---|
| committer | Mark <53903062+andemark@users.noreply.github.com> | 2023-11-21 12:28:16 +0000 |
| commit | 791e1812b7c9be94434f3921c065afd1e5366b9b (patch) | |
| tree | 3c987536c94cc3e85bcb33e1fe545ac203f6bf0b | |
| parent | 6d6ad78bb6758a941bf8bacaca01b6e2135b7cd9 (diff) | |
| download | perlweeklychallenge-club-791e1812b7c9be94434f3921c065afd1e5366b9b.tar.gz perlweeklychallenge-club-791e1812b7c9be94434f3921c065afd1e5366b9b.tar.bz2 perlweeklychallenge-club-791e1812b7c9be94434f3921c065afd1e5366b9b.zip | |
ch-2.raku improved
| -rw-r--r-- | challenge-244/mark-anderson/raku/ch-2.raku | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/challenge-244/mark-anderson/raku/ch-2.raku b/challenge-244/mark-anderson/raku/ch-2.raku index 1377532928..a652d03802 100644 --- a/challenge-244/mark-anderson/raku/ch-2.raku +++ b/challenge-244/mark-anderson/raku/ch-2.raku @@ -6,32 +6,21 @@ is group-hero(2,1,4), 141; benchmark(); -sub postfix:<!>($num where * >= 0) { [*] 1..$num } -sub C($n, $r) { $n! / ($r! * ($n - $r)!) } +# say group-hero((1..20).roll(10000)); +# takes about 1 minute and 40 seconds on my computer. sub group-hero(*@a) { - my $b = @a.Bag; - my %h; + my $b = @a.Bag; - for $b.keys -> $k - { - %h{$k} = sum map { C($b{$k}, $_) }, 1..$b{$k} - } - - sum gather for $b.keys.combinations.skip - { - take ([*] %h{$_}) * .max² * .min - } + $b.keys.map(-> $k { %h{$k} = 2 ** $b{$k} - 1 }); + sum $b.keys.combinations.skip.map({ ([*] %h{$_}) * .max² * .min }) } sub group-hero-slow(*@a) { - sum gather for @a.combinations.skip - { - take .max² * .min - } + sum @a.combinations.skip.map({ .max² * .min }) } sub benchmark @@ -42,7 +31,7 @@ sub benchmark { say group-hero-slow(@a) }, { say group-hero(@a) } - #Old: 44.378420463s - #New: 0.036338161s - #NEW version is 1221.26x faster + # Old: 45.791410967s + # New: 0.020774281s + # NEW version is 2204.24x faster } |
