#!/usr/local/bin/perl # day 2015-15 use strict; use warnings; use v5.32; use lib '.'; use Day15; use List::Util qw( max ); @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my $TOTAL = 100; my $CALORIES = 500; my %ingredients = %{ init_ingredients ( $input ) }; my @ingredients = sort keys %ingredients; my @proportions = @{ find_proportions( $TOTAL ) }; my @scores1; my @scores2; foreach my $proportion ( @proportions ) { # array ref my ( $capacity, $durability, $flavor, $texture, $calories ) = ( 0, 0, 0, 0, 0 ); foreach my $ingredient ( 0 .. $#ingredients ) { $capacity += $ingredients{ $ingredients[ $ingredient ] }{ capacity } * $proportion->[ $ingredient ]; $durability += $ingredients{ $ingredients[ $ingredient ] }{ durability } * $proportion->[ $ingredient ]; $flavor += $ingredients{ $ingredients[ $ingredient ] }{ flavor } * $proportion->[ $ingredient ]; $texture += $ingredients{ $ingredients[ $ingredient ] }{ texture } * $proportion->[ $ingredient ]; $calories += $ingredients{ $ingredients[ $ingredient ] }{ calories } * $proportion->[ $ingredient ]; } $capacity = ( $capacity < 0 ) ? 0 : $capacity; $durability = ( $durability < 0 ) ? 0 : $durability; $flavor = ( $flavor < 0 ) ? 0 : $flavor; $texture = ( $texture < 0 ) ? 0 : $texture; $calories = ( $calories < 0 ) ? 0 : $calories; my $score = $capacity * $durability * $flavor * $texture; push @scores1 => $score; push @scores2 => $score if ( $calories == $CALORIES ); } say "part 1: ", max @scores1; say "part 2: ", max @scores2;