aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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