aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/sgreen/perl/ch-2.pl
blob: dcdbfcfc614171cafab215c3ef07a30558bd94e6 (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 perl

use strict;
use warnings;
use feature 'say';
use experimental 'signatures';

use Math::Combinatorics;

sub main ($word) {
    # Convert to lower case, and sort the letters alphabetically
    my @letters = sort ( split //, lc($word) );
    my $count   = 0;

    # Go through each sorted unique permutation and count where <= $word
    my $c = Math::Combinatorics->new(
        data      => \@letters,
        frequency => [ (1) x length($word) ]
    );

    while ( my @perm = $c->next_string() ) {
        if ( join( '', @perm ) le $word ) {
            $count++;
        }
    }

    # Return the rank
    say $count;
}

main(@ARGV);