aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-06-29 13:43:40 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-06-29 13:43:40 +0200
commit14e647fc20118e2813d7e3aa3b31af54677b6aa5 (patch)
tree495f2bf894e24d73ca89f8f8976dd7b1a518f8e7
parente278df37b0c49be8f048866a815aa43a13e0760a (diff)
downloadperlweeklychallenge-club-14e647fc20118e2813d7e3aa3b31af54677b6aa5.tar.gz
perlweeklychallenge-club-14e647fc20118e2813d7e3aa3b31af54677b6aa5.tar.bz2
perlweeklychallenge-club-14e647fc20118e2813d7e3aa3b31af54677b6aa5.zip
Brute force at task 1.
-rw-r--r--challenge-067/luca-ferrari/raku/ch-1.p628
1 files changed, 6 insertions, 22 deletions
diff --git a/challenge-067/luca-ferrari/raku/ch-1.p6 b/challenge-067/luca-ferrari/raku/ch-1.p6
index f9d123fe1d..a408701f34 100644
--- a/challenge-067/luca-ferrari/raku/ch-1.p6
+++ b/challenge-067/luca-ferrari/raku/ch-1.p6
@@ -12,31 +12,15 @@ sub MAIN( Int :$m where { $m > 2 } = 5,
# found combinations
my @combinations;
- for 0 ..^ $m {
- # cannot get negative numbers!
- next if $m - $_ < $n;
-
- # current combination
- my @combination;
- # push the last digit of this combination, for example m=5, n=3 -> 5
- @combination.push: $m - $_;
-
- # fill up the combination with decreasing order
- @combination.push( @combination[ *-1 ] - 1 ) while ( @combination.elems < $n );
- # push this initial combination, e.g., 5,4,3
- @combinations.push: Array.new( @combination.reverse );
-
-
- # now go backwards all the digits until we reach '1' with the last digit
- # e.g. 5,4,2; 5,4,1
- while ( @combination[ *-1 ] > 1 ) {
- @combination[ *-1 ] -= 1;
- @combinations.push: Array.new( @combination.reverse );
- }
+ for ( 1 x $n ).Int ^..^ ( $m x $n ).Int {
+ my @digits = $_.comb;
+ next if @digits.elems != $n;
+ my $ok = True;
+ $ok = False if ( @digits[ $_ ] >= @digits[ $_ + 1 ] ) for 0 ..^ @digits.elems - 1;
+ @combinations.push: @digits if $ok;
}
-
@combinations.sort.join( ", " ).say;
}