aoc2015

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

commit e40379e780201e726da4fa85f1157bc58e280c18
parent 0324c287a598a24a5d1b59a2e267ee7767845762
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue,  8 Nov 2022 16:02:08 +0000

solve part 1 of day15

Diffstat:
Aday15/Day15.pm | 41+++++++++++++++++++++++++++++++++++++++++
Aday15/day15.pl | 43+++++++++++++++++++++++++++++++++++++++++++
Aday15/input | 4++++
3 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/day15/Day15.pm b/day15/Day15.pm @@ -0,0 +1,41 @@ +package Day15; + +use strict; +use warnings; +use v5.32; +use Exporter qw( import ); + +our @EXPORT = qw( init_ingredients find_proportions ); + +sub init_ingredients { + my $input = shift; + my %ingredients; + foreach ( split /\n/, $input ) { + my ( $ingredient, $capacity, $durability, $flavor, $texture, $calories ) = + m/(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)/; + $ingredients{ $ingredient }{ capacity } = $capacity; + $ingredients{ $ingredient }{ durability } = $durability; + $ingredients{ $ingredient }{ flavor } = $flavor; + $ingredients{ $ingredient }{ texture } = $texture; + $ingredients{ $ingredient }{ calories } = $calories; + } + return \%ingredients; +} + +sub find_proportions { + my $total = shift; + my @proportions; + foreach my $a ( 0 .. $total ) { + foreach my $b ( 0 .. $total - $a ) { + foreach my $c ( 0 .. $total - $a - $b ) { + foreach my $d ( 0 .. $total - $a - $b - $c ) { + next unless ( $a + $b + $c + $d == $total ); + push @proportions => [ $a, $b, $c, $d ]; + } + } + } + } + return \@proportions; +} + +1; diff --git a/day15/day15.pl b/day15/day15.pl @@ -0,0 +1,43 @@ +#!/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 %ingredients = %{ init_ingredients ( $input ) }; +my @ingredients = sort keys %ingredients; +my @proportions = @{ find_proportions( $TOTAL ) }; +my @scores; + +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 @scores => $score; +} + +say "part 1: ", max @scores; diff --git a/day15/input b/day15/input @@ -0,0 +1,4 @@ +Frosting: capacity 4, durability -2, flavor 0, texture 0, calories 5 +Candy: capacity 0, durability 5, flavor -1, texture 0, calories 8 +Butterscotch: capacity -1, durability 0, flavor 5, texture 0, calories 6 +Sugar: capacity 0, durability 0, flavor -2, texture 2, calories 1