forked from manwar/perlweeklychallenge-club
/
ch-2.pl
executable file
·46 lines (35 loc) · 921 Bytes
/
ch-2.pl
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
45
46
#!/usr/bin/perl
use 5.030;
use warnings;
sub combinations {
my @list = @{$_[0]};
my $length = $_[1];
if ($length <= 1) {
return map [$_], @list;
}
my @combos;
for (my $i = 0; $i + $length <= scalar @list; $i++) {
my $val = $list[$i];
my @rest = @list[$i + 1 .. $#list];
for my $c (combinations(\@rest, $length - 1)) {
push @combos, [$val, @{$c}] ;
}
}
return @combos;
}
my @ints = @ARGV;
my $longest = 0;
for my $n (3 .. scalar @ints) {
COMBINATION: for my $combo (combinations(\@ints, $n)) {
my $delta = $combo->[1] - $combo->[0];
for my $i (2 .. scalar @{$combo} - 1) {
if ($combo->[$i] - $combo->[$i - 1] != $delta) {
next COMBINATION;
}
}
if (scalar @{$combo} > $longest) {
$longest = scalar @$combo;
}
}
}
say $longest;