aoc2015

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

Day15.pm (1242B) - raw


      1 package Day15;
      2 
      3 use strict;
      4 use warnings;
      5 use v5.32;
      6 use Exporter qw( import );
      7 
      8 our @EXPORT = qw( init_ingredients find_proportions );
      9 
     10 sub init_ingredients {
     11     my $input = shift;
     12     my %ingredients;
     13     foreach ( split /\n/, $input ) {
     14         my ( $ingredient, $capacity, $durability, $flavor, $texture, $calories ) =
     15           m/(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)/;
     16         $ingredients{ $ingredient }{ capacity } = $capacity;
     17         $ingredients{ $ingredient }{ durability } = $durability;
     18         $ingredients{ $ingredient }{ flavor } = $flavor;
     19         $ingredients{ $ingredient }{ texture } = $texture;
     20         $ingredients{ $ingredient }{ calories } = $calories;
     21     }
     22     return \%ingredients;
     23 }
     24 
     25 sub find_proportions {
     26     my $total = shift;
     27     my @proportions;
     28     foreach my $a ( 0 .. $total ) {
     29         foreach my $b ( 0 .. $total - $a ) {
     30             foreach my $c ( 0 .. $total - $a - $b ) {
     31                 foreach my $d ( 0 .. $total - $a - $b - $c ) {
     32                     next unless ( $a + $b + $c + $d == $total );
     33                     push @proportions => [ $a, $b, $c, $d ];
     34                 }
     35             }
     36         }
     37     }
     38     return \@proportions;
     39 }
     40 
     41 1;