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

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

use List::Util qw{max};

my @examples = (
    {
        letters => [ 'R', 'E', 'P', 'L' ],
        weights => [ 3,   2,   1,   4 ],
    },
    {
        letters => [ 'A', 'U', 'R', 'K' ],
        weights => [ 2,   4,   1,   3 ],
    },
    {
        letters => [ 'O', 'H', 'Y', 'N', 'P', 'T' ],
        weights => [ 5,   4,   2,   6,   1,   3 ],
    }
);

for my $example (@examples) {
    my $output  = sort_letters($example);
    my $letters = join ', ', map { qq{'$_'} } $example->{letters}->@*;
    my $weights = join ', ', $example->{weights}->@*;
    say <<"END";
    Input:  \@letters = ($letters)
            \@weights = ($weights)
    Output: $output
END
}

sub sort_letters ($input) {
    my @output;
    for my $i ( 0..-1+scalar $input->{weights}->@*){
        my $w = $input->{weights}[$i];
        my $l = $input->{letters}[$i];
        @output[$w]= $l;
    }
    my $output = join '', grep { defined } @output;
    return $output;
}