aoc2015

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

day15.pl (1777B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2015-15
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 use lib '.';
      8 use Day15;
      9 use List::Util qw( max );
     10 
     11 @ARGV = "input" unless @ARGV;
     12 chomp( my $input = do { local $/; <> } );
     13 
     14 my $TOTAL       = 100;
     15 my $CALORIES    = 500;
     16 my %ingredients = %{ init_ingredients ( $input ) };
     17 my @ingredients = sort keys %ingredients;
     18 my @proportions = @{ find_proportions( $TOTAL ) };
     19 my @scores1;
     20 my @scores2;
     21 
     22 foreach my $proportion ( @proportions ) { # array ref
     23     my ( $capacity, $durability, $flavor, $texture, $calories ) = ( 0, 0, 0, 0, 0 );
     24     foreach my $ingredient ( 0 .. $#ingredients ) {
     25         $capacity   += $ingredients{ $ingredients[ $ingredient ] }{ capacity } *
     26                        $proportion->[ $ingredient ];
     27         $durability += $ingredients{ $ingredients[ $ingredient ] }{ durability } *
     28                        $proportion->[ $ingredient ];
     29         $flavor     += $ingredients{ $ingredients[ $ingredient ] }{ flavor } *
     30                        $proportion->[ $ingredient ];
     31         $texture    += $ingredients{ $ingredients[ $ingredient ] }{ texture } *
     32                        $proportion->[ $ingredient ];
     33         $calories   += $ingredients{ $ingredients[ $ingredient ] }{ calories } *
     34                        $proportion->[ $ingredient ];
     35     }
     36     $capacity    = ( $capacity   < 0 ) ? 0 : $capacity;
     37     $durability  = ( $durability < 0 ) ? 0 : $durability;
     38     $flavor      = ( $flavor     < 0 ) ? 0 : $flavor;
     39     $texture     = ( $texture    < 0 ) ? 0 : $texture;
     40     $calories    = ( $calories   < 0 ) ? 0 : $calories;
     41     my $score    = $capacity * $durability * $flavor * $texture;
     42     push @scores1 => $score;
     43     push @scores2 => $score if ( $calories == $CALORIES );
     44 }
     45 
     46 say "part 1: ", max @scores1;
     47 say "part 2: ", max @scores2;