challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

level3.pl (506B)


      1 #!/usr/bin/perl
      2 
      3 use strict;
      4 use warnings;
      5 use List::Util qw(sum);
      6 
      7 open(my $file, 'level3.csv');
      8 my $text = <$file>;
      9 close($file);
     10 
     11 my @numbers = map(int, split(', ', $text));
     12 
     13 my $l = scalar(@numbers);
     14 my $p = 2 ** $l;
     15 
     16 my $count = 0;
     17 
     18 for (my $i = 1; $i < $p; $i++) {
     19   my @set;
     20   for (my $j = 0; $j < $l; $j++) {
     21     push(@set, $numbers[$j]) if (($i >> $j) & 1);
     22   }
     23   if (scalar(@set) > 1) {
     24     my $highest = pop(@set);
     25     if ($highest == sum(@set)) {
     26       $count++;
     27     }
     28   }
     29 }
     30 
     31 print "$count\n";