aboutsummaryrefslogtreecommitdiff
path: root/challenge-014/e-choroba/perl5/ch-2.pl
blob: 514b92cdfe3984a3acc9bb6c417236cc045ab22d (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/perl
use warnings;
use strict;
use feature qw{ say };

my %states;
@states{qw(AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME
           MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI
           SC SD TN TX UT VT VA WA WV WI WY)} = ();

my $dictionary = '/usr/share/dict/british';
open my $in, '<', $dictionary or die $!;
my @longest = ("");
while (my $word = <$in>) {
    chomp $word;
    next if 1 & length $word;

    my $uc_word = uc $word;
    my @pairs = $uc_word =~ /\G(..)/g;

    next if grep ! exists $states{$_}, @pairs;

    next if length($word) < length $longest[0];

    if (length($word) == length $longest[0]) {
        push @longest, $word;
    } else {
        @longest = ($word);
    }
}
say for @longest;