aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2021-05-10 13:05:12 +0200
committerLuca Ferrari <fluca1978@gmail.com>2021-05-10 13:05:12 +0200
commit7e9b47b213406af755cff749ab520df133cff255 (patch)
tree3d1394e8b6be2da8037a2000ec36f69ac5589a6b
parentfcff2c88725e89115ed7d7899235c163e770903a (diff)
downloadperlweeklychallenge-club-7e9b47b213406af755cff749ab520df133cff255.tar.gz
perlweeklychallenge-club-7e9b47b213406af755cff749ab520df133cff255.tar.bz2
perlweeklychallenge-club-7e9b47b213406af755cff749ab520df133cff255.zip
Another possible implementation of the second task.
-rw-r--r--challenge-112/luca-ferrari/raku/ch-2.p652
1 files changed, 44 insertions, 8 deletions
diff --git a/challenge-112/luca-ferrari/raku/ch-2.p6 b/challenge-112/luca-ferrari/raku/ch-2.p6
index e56fad916e..7b37b0b587 100644
--- a/challenge-112/luca-ferrari/raku/ch-2.p6
+++ b/challenge-112/luca-ferrari/raku/ch-2.p6
@@ -1,20 +1,56 @@
#!raku
sub MAIN( Int $top where { $top > 1 } ) {
- my @haystack = (1 xx $top, 2 xx $top / 2).flat;
+ # my @haystack = (1 xx $top, 2 xx $top / 2).flat;
+ # my @solutions;
+
+ # # get all possible combinations
+ # for @haystack.permutations {
+ # # check every sub-array that gives me the sum
+ # for 0 ..^ $_.elems -> $index {
+ # @solutions.push: $_[ 0 .. $index ] if $_[ 0 .. $index ].sum == $top;
+ # }
+ # }
+
+
+
+ # my @solutions;
+ # my @haystack = 1,2;
+
+ # my @current-solution;
+ # while ( @current-solution.sum < $top ) {
+ # if @current-solution.sum + @haystack[ 1 ] <= $top {
+ # @current-solution.push: @haystack [ 1 ];
+ # }
+ # elsif @current-solution.sum + @haystack[ 0 ] <= $top {
+ # @current-solution.push: @haystack [ 0 ];
+ # }
+ # }
+
+
my @solutions;
+ @solutions.push: 1 xx $top;
+ @solutions.push: 2 xx $top / 2 if $top %% 2;
- # get all possible combinations
- for @haystack.permutations {
- # check every sub-array that gives me the sum
- for 0 ..^ $_.elems -> $index {
- @solutions.push: $_[ 0 .. $index ] if $_[ 0 .. $index ].sum == $top;
- }
+ my @current-solution;
+ while ( @current-solution.sum != $top ) {
+ @current-solution.push: 2 if @current-solution.sum + 2 <= $top;
+ @current-solution.push: 1 if @current-solution.sum + 1 <= $top;
}
+ if @current-solution.sum == $top {
+ @solutions.push: $_ for @current-solution.permutations
+ }
+
+
# print only unique values
- say @solutions.unique: as => { .Str };
+ for @solutions.unique( as => { .Str } ) -> @current-solution {
+ say "";
+ "%d step%s ".sprintf( $_, $_ > 1 ?? 's' !! '' ).print for @current-solution;
+ say "";
+ }
+
}