aboutsummaryrefslogtreecommitdiff
path: root/challenge-083/daniel-mita/perl/ch-2.pl
blob: 9883d94635ba92858414bcb8d874b712aef24af7 (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
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw<say>;

use List::Util qw<sum>;
use Scalar::Util qw<looks_like_number>;

if ( @ARGV == 0 || grep { !looks_like_number($_) || $_ < 1 } @ARGV ) {
    say 'List of positive numbers required.';
    exit 1;
}

my @combinations;
for ( my $i = 0; $i < @ARGV; $i++ ) {
    for ( my $j = $i; $j < @ARGV; $j++ ) {
        push @combinations, [ @ARGV[ $i .. $j ] ];
    }
}

say scalar(
    @{  [   sort { sum( @{$b} ) <=> sum( @{$a} ) || @{$a} <=> @{$b} }
            grep { sum( @{$_} ) <= sum(@ARGV) / 2 } @combinations
        ]->[0]
    }
);