aboutsummaryrefslogtreecommitdiff
path: root/challenge-055/simon-proctor/raku/ch-2.p6
blob: 40d1f60451f60e0fe6686ecefcc39126e6845e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env raku

use v6;

#| Make a random list of length N integers between -100 and 100 then give the wave sorted versions
multi sub MAIN (
    UInt $n where 2 <= * <= 20 #= Positive integer 2 <= N <= 20
) {
    MAIN( |(-100..100).roll($n) );
}

multi sub MAIN( *@ ) is hidden-from-USAGE { say "Please give a list of 2 or more items or a single integer greater then 1";say $*USAGE; }                                                               

#| Given a list of integers return all the wave sorted lists
multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) {
    .say for unique-perms( @input ).race.grep( -> @l { is-wave( 'gte', |@l ) } );
}

sub unique-perms ( @input ) {
    @input.permutations.unique(:as(*.join(",")));
}

multi sub is-wave( 'lte', Int $a, Int $b where { $a <= $b } )          { True }
multi sub is-wave( 'lte', Int $a, Int $b where { $a > $b } )           { False }
multi sub is-wave( 'gte', Int $a, Int $b where { $a >= $b } )          { True }
multi sub is-wave( 'gte', Int $a, Int $b where { $a < $b } )           { False }
multi sub is-wave( 'gte', Int $a, Int $b where { $b > $a }, *@ )       { False }
multi sub is-wave( 'lte', Int $a, Int $b where { $a > $b }, *@ )       { False }
multi sub is-wave( 'gte', Int $a, Int $b where { $a >= $b }, $c, *@r ) { True && is-wave( 'lte', $b, $c, |@r ); }
multi sub is-wave( 'lte', Int $a, Int $b where { $a <= $b }, $c, *@r ) { True && is-wave( 'gte', $b, $c, |@r ); }