aboutsummaryrefslogtreecommitdiff
path: root/challenge-030/creewick/perl5/ch-2.pl
blob: 65aadf55b860522bb4f50c3e4390e92e3e51a561 (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/perl
use strict;
use warnings;
use v5.10;

# Here you can vary the numbers count in series and the required sum

my $REQUIRED_SUM = 12;
my $NUMBERS_COUNT = 3;

# And define whether current series fits the conditions or not

sub isGoodSeries {
    my (@numbers) = @_;
    my ($sum, $evensCount) = (0, 0);

    foreach (@numbers) {
        $sum += $_;
        if ($_ % 2 == 0) {
            $evensCount++;
        }
    }

    return $sum == $REQUIRED_SUM && $evensCount > 0;
}

sub checkAllSeries {
    my @series = @_;
    my $count = @series;

    if ($count == $NUMBERS_COUNT) {
        if (isGoodSeries(@series)) {
            say "@series";
        }
    } else {
        for my $i (1..$REQUIRED_SUM) {
            my @newSeries = @series;
            push(@newSeries, $i);
            checkAllSeries(@newSeries);
        }
    }
}

checkAllSeries(());