aboutsummaryrefslogtreecommitdiff
path: root/challenge-030/dave-cross/perl5/ch-2.pl
blob: fce3b840dedce45031f17d7235e1906a7a50be63 (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
#!/usr/bin/perl

use strict;
use warnings;
use feature qw[say state];

use List::Util 'sum';

# If three numbers add to 12 then the
# highest number you can have is 10.
my $max = 10;

for my $x (1 .. $max) {
  for my $y (1 .. $max) {
    for my $z (1 .. $max) {
      say join ', ', $x, $y, $z if valid($x, $y, $z);
    }
  }
}

sub valid {
  state $seen;

  @_ = sort { $a <=> $b } @_;

  return if $seen->{join ',', @_};
  return unless sum(@_) == 12;
  return unless grep { ! ($_ % 2) } @_;
  $seen->{join ',', sort @_} = 1;
  return 1;
}