aboutsummaryrefslogtreecommitdiff
path: root/challenge-065/simon-proctor/raku/ch-2.raku
blob: 85bc73ae4cc0fd6690ada20458e80673b5ec58b8 (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
31
#!/usr/bin/env raku

use v6;

sub MAIN (
    Str $target #= String to search for palindromes.
) {
    my $found = False;
    for ^$target.codes -> $start {
        my @palindromes = find-palindromes( $target.substr( $start ) );
        if @palindromes.grep( so * ) {
            $found = True;
            for @palindromes { .join(", ").say };
        }
    }
    say -1 if ! $found;    
}

sub infix:<|,|> (@a,@b) { |@a, |@b }

sub find-palindromes( Str $target ) {
    my @out;
    for 2..$target.codes -> $length {
        my $check = $target.substr( 0, $length );
        if $check ~~ $check.flip {
            my @rest = find-palindromes( $target.substr( $length ) );
            @out = @out |,| ( [ [ $check, ], ] X|,| @rest );
        }
    }
    return @out || [[],];
}