aboutsummaryrefslogtreecommitdiff
path: root/challenge-053/mohammad-anwar/perl/ch-2.pl
blob: 6e1224baf2ffc9fb35954ddd3a9427913d59860d (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
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl

use strict;
use warnings;

use Algorithm::Combinatorics qw(combinations);

my $count = $ARGV[0] // 2;
die "ERROR: Invalid count $count.\n"
    unless (($count >= 1) && ($count <= 5));

my $chars = [qw(a e i o u)];

if ($count == 1) {
    print join "\n", @$chars;
    print "\n";
    exit;
}

my $iter = combinations($chars, $count);

my $char_sets = [];
while (my $char = $iter->next) {
    push @$char_sets, join "", @$char;
}

my $rules = [
    qr/a(?=[ie])/,
    qr/e(?=[i])/,
    qr/i(?=[aeou])/,
    qr/o(?=[au])/,
    qr/u(?=[oe])/
];

foreach my $char_set (@$char_sets) {
    my $pass = 0;
    foreach my $rule (@$rules) {
        if ($char_set =~ /$rule/) {
            $pass = 1;
        }
    }
    print "$char_set\n" if ($pass);
}