aboutsummaryrefslogtreecommitdiff
path: root/challenge-077
diff options
context:
space:
mode:
authorwambash <wamba@centrum.cz>2020-09-08 17:40:23 +0200
committerwambash <wamba@centrum.cz>2020-09-08 17:40:23 +0200
commit6d634caf5c73be5aa96578ce440aa45dafaaab9c (patch)
treef18cc7583b5421d557e6e4739417178c50d1bc10 /challenge-077
parentacf95d93093c0188dd0770606354eaeea0648df2 (diff)
downloadperlweeklychallenge-club-6d634caf5c73be5aa96578ce440aa45dafaaab9c.tar.gz
perlweeklychallenge-club-6d634caf5c73be5aa96578ce440aa45dafaaab9c.tar.bz2
perlweeklychallenge-club-6d634caf5c73be5aa96578ce440aa45dafaaab9c.zip
Solution challenge 077-1
Diffstat (limited to 'challenge-077')
-rwxr-xr-xchallenge-077/wambash/raku/ch-1.raku38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-077/wambash/raku/ch-1.raku b/challenge-077/wambash/raku/ch-1.raku
new file mode 100755
index 0000000000..0d3b0934b6
--- /dev/null
+++ b/challenge-077/wambash/raku/ch-1.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+
+constant @fib := 1,2 , * + * ... *;
+
+multi iter ( Capture $c ( :$sum, :@pos, :@used ) ) {
+ \( sum => $sum + @pos.head, pos => @pos.skip, used => (|@used, @pos.head) ),
+ \( |$c, pos => @pos.skip)
+};
+
+multi iter ( Seq(Any) $a ) {
+ $a.map: |*.&iter
+}
+
+
+sub fib-sum ( $sum ) {
+ state @fib-sum = \( :0sum, pos => @fib ), *.&iter.cache ... *;
+ my $stop-index = @fib.first: * > $sum, :k;
+
+ @fib-sum
+ andthen .[$stop-index]
+ andthen .grep: { .<sum> == $sum }
+}
+
+sub MAIN ( :$test!, :$log ) {
+ use Test;
+
+ my $wi = &iter.wrap: {
+ say .<sum>," ",.<used>, " ",.<pos>.cache.head when Capture;
+ callsame
+ } if $log;
+
+ is-deeply fib-sum(9).map( *.<used>), ((1,3,5),(1,8)),;
+ is-deeply fib-sum(6).map( *.<used>), ((1,2,3),(1,5)),;
+
+ $wi.restore if $log;
+
+ done-testing()
+}