aboutsummaryrefslogtreecommitdiff
path: root/challenge-328/dave-jacoby/perl/ch-1.pl
blob: a42a09283639917f8a151469b49df304246998b3 (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
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say state postderef signatures };

my @examples = (

    qw{ a?z pe?k gra?te b?a?c?d?a }
);

for my $input (@examples) {
    my $output = replace_all_Qs($input);
    say <<"END";
    Input:  \@str = "$input"
    Output:        "$output"
END
}

sub replace_all_Qs($example) {
    my @qs =
        grep { '?' eq substr( $example, $_, 1 ) } 0 .. -1 + length $example;
    for my $q (@qs) {
        for my $l ( 'A' .. 'Z' ) {
            my $ll = $l . $l;
            my $c = $example;
            substr( $c, $q, 1 ) = $l; 
            if ( $c !~ /$ll/mix ) {
                $example = $c;
                last;
            }
        }
    }
    return $example;
}