aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-09-13 06:04:44 +0200
committerMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-09-13 06:04:44 +0200
commitfb891f3b1bbcd2da4c9399809737297ae4a9b087 (patch)
tree295eccabbf54501ed3571a3f459218be9e36709c
parentb068e9279dccdeef9f4cbda7a8dffbdfc367518a (diff)
downloadperlweeklychallenge-club-fb891f3b1bbcd2da4c9399809737297ae4a9b087.tar.gz
perlweeklychallenge-club-fb891f3b1bbcd2da4c9399809737297ae4a9b087.tar.bz2
perlweeklychallenge-club-fb891f3b1bbcd2da4c9399809737297ae4a9b087.zip
#1 initial
-rw-r--r--challenge-077/markus-holzer/raku/ch-1.raku37
1 files changed, 19 insertions, 18 deletions
diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku
index f4ef0295a9..054dd51cb9 100644
--- a/challenge-077/markus-holzer/raku/ch-1.raku
+++ b/challenge-077/markus-holzer/raku/ch-1.raku
@@ -1,41 +1,42 @@
use experimental :cached;
-unit sub MAIN( $N, $v = False );
+unit sub MAIN( Int $N where * > 0, Bool :$v = False );
my $start = now;
-with gather combine( zeckendorf( $N ) )
+with my @combinatons = gather combine zeckendorf $N
{
- say "# of combinations: {.elems}";
- say "Calculated in { sprintf "%.3f", now - $start } seconds";
- if $v { .say for .sort( -*.elems ) }
+ say @combinatons.map( *.join( "," ) ).join( "\n" ) if $v;
+ say "# of combinations: {+@combinatons}";
+ say "Calculated in { sprintf "%.3f", now - $start } seconds"
}
-sub combine( @Z is copy ) is cached
+sub combine( @Z ) is cached
{
- take @Z;
+ my &valid = -> $result {
+ $result.elems == $result.unique.elems && $result !~~ @Z };
+
+ my &insert = -> $where, $what {
+ my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x };
- my &insert = { my @x = @Z.clone; @x[$^i] = $^z; [@x.map: |*] };
- my &valid = { $^x.elems == $^x.unique.elems && @$^x !~~ @Z };
+ take @Z;
sink @Z
.map( &zeckendorf )
.kv.map( &insert )
.grep( &valid )
- .map( &combine );
+ .map( &combine )
}
sub zeckendorf( $n is copy ) is cached
{
state @fib = [1, 1, * + * ... * > $N];
- $n == 1
- ?? 1.List
- !! eager gather for @fib.grep( * < $n ).reverse
- {
+ my &do-zeckendorf = {
+ eager gather for @fib.grep( * < $n ).reverse {
if $_ <= $n {
take $_;
- $n -= $_;
- }
- }
-}
+ $n -= $_; }}}
+
+ $n == 1 ?? 1.List !! do-zeckendorf
+} \ No newline at end of file