aoc2015

Advent of Code 2015 solutions in Perl.
git clone git://git.samirparikh.com/aoc2015
Log | Files | Refs | README

day17.pl (676B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2015-17
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 use List::Util qw ( min );
      8 use Algorithm::Knapsack; # I know, this is cheating
      9 
     10 @ARGV = "input" unless @ARGV;
     11 chomp( my $input = do { local $/; <> } );
     12 
     13 my @containers = split /\n/, $input;
     14 my $LITERS = 150;
     15 my @num_containers;
     16 
     17 my $knapsack = Algorithm::Knapsack->new(
     18     capacity => $LITERS,
     19     weights  => \@containers,
     20 );
     21 
     22 $knapsack->compute();
     23 
     24 foreach my $solution ($knapsack->solutions()) {
     25     push @num_containers => scalar ( map { $containers[$_] } @{$solution} );
     26 }
     27 
     28 say "part 1: ", scalar $knapsack->solutions();
     29 say "part 2: ", scalar grep { $_ == min @num_containers } @num_containers;