aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/dave-jacoby/perl/ch-2.pl
blob: e82b4e5f299122a768c164f67e655ec7152fde54 (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
45
46
47
48
49
50
#!/usr/bin/env perl

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

use List::Util qw{ min };

my @examples = (

    [ 1, 2, 3, 4, 5 ],
    [ 5, 7, 1, 7 ],
);

for my $input (@examples) {
    my @output = strong_pair( $input->@* );
    my $ints   = join ', ', $input->@*;
    my $output = scalar @output;
    my $pairs = join ', ', map { qq{($_)} } map { join ', ', $_->@* } @output;

    say <<"END";
        Input:  \@ints = ($ints)
        Output: $output

        Strong Pairs: $pairs
END
}

sub strong_pair (@ints) {
    my @output;
    my $top = -1 + scalar @ints;
    for my $i ( 0 .. $top ) {
        my $x = $ints[$i];
        for my $j ( $i + 1 .. $top ) {
            my $y   = $ints[$j];
            my $abs = abs $x - $y;
            my $min = min $x, $y;
            if ( $abs > 0 && $abs < $min ) {
                my @pair = sort $x, $y;
                push @output, \@pair;
            }
        }
    }

    # array cleanup - put in order, avoid duplicates
    my %hash;
    map { my $x = join ',', @$_; $hash{$x} = $_ } @output;
    return sort { $a->[0] <=> $b->[0] }
        sort { $a->[1] <=> $b->[1] } values %hash;
}