aboutsummaryrefslogtreecommitdiff
path: root/challenge-237/sgreen/perl/ch-2.pl
blob: 1a5428cef857b690db1a4cb5eb22037eaa99e7d5 (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
#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';
use experimental 'signatures';

use List::Util qw(min max);
use List::MoreUtils 'firstidx';

sub main (@ints) {
    my @sorted_ints = sort { $b <=> $a } @ints;
    my $count = 0;

    foreach my $i (@sorted_ints) {
        # If there isn't a solution, exit the loop
        if (min(@ints) >= $i) {
            last;
        }

        # Find the position of the maximum value < i, and delete it
        my $m = max(grep {$_ < $i} @ints);
        my $idx = firstidx { $_ == $m } @ints;
        splice(@ints, $idx, 1);

        ++$count;
    }
    
    say $count;
}

main(@ARGV);