aboutsummaryrefslogtreecommitdiff
path: root/challenge-220/deadmarshal/perl/ch-2.pl
blob: 3de51a3fcfe17941f41f226b29cbfb25d8eac120 (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
#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(permutations);
use List::MoreUtils qw(slide all);
use Data::Show;

sub is_perfect_square{
  do{
    my $sqrt = sqrt($_[0]);
    return ($sqrt * $sqrt) == $_[0];
  }if($_[0] >= 0);
  0
}

sub squareful{
  my %hash;
  map{$hash{$_}++} @{$_[0]};
  return @{$_[0]} if keys %hash == 1;
  my @ret;
  my $iter = permutations($_[0]);
  while(my $p = $iter->next)
  {
    if(all{$_ == 1} slide{is_perfect_square($a+$b)} @$p)
    {
      push @ret,$p;
    }
  }
  @ret;
}

print show squareful([1,17,8]);
print show squareful([2,2,2]);