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

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

use List::Util qw{ first };

my @examples = (

    [ 2, 5, 3, 4 ],
    [ 9, 4, 1, 3, 6, 4, 6, 1 ],
    [ 1, 2, 2, 3 ],
);

for my $example (@examples) {
    my @output = number_game( $example->@* );
    my $output = join ', ', @output;
    my $ints = join ', ', $example->@*;

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

sub number_game (@ints) {
    my @sorted = sort { $a <=> $b } @ints;
    my @output;
    while (@sorted) {
        my @cache;
        push @cache, shift @sorted;
        push @cache, shift @sorted;
        @cache = reverse @cache;
        push @output, @cache;
    }
    return @output;
}