]> www.fi.muni.cz Git - aoc.git/blob - 2015/29.pl
Day 25: examining the input
[aoc.git] / 2015 / 29.pl
1 #!/usr/bin/perl -w
2
3 use v5.16;
4 use strict;
5
6 my %prop;
7 while (<>) {
8         my ($name) = /\A(\w+):/;
9         my @vals = /(-?\d+)/g;
10         $prop{$name} = \@vals;
11 }
12
13 my $max;
14
15 sub addprop {
16         my ($amounts, $rem, $rest) = @_;
17
18         if (!@$rest) {
19                 for my $p (keys %prop) {
20                         print "$p=$amounts->{$p}, ";
21                 }
22                 print "\n";
23                 my $mul = 1;
24                 for my $t (0 ..3) {
25                         my $sum = 0;
26                         for my $p (keys %prop) {
27                                 $sum += $prop{$p}->[$t] * $amounts->{$p};
28                         }
29                         $sum = 0 if $sum < 0;
30                         $mul *= $sum;
31                 }
32                 $max = $mul if !$max || $mul > $max;    
33         } else {
34                 for my $i (0 .. $#$rest) {
35                         my @nr = @$rest;
36                         my ($ing) = splice @nr, $i, 1;
37                         my $min = @nr ? 0 : $rem;
38                         for my $a ($min .. $rem) {
39                                 my %na = %$amounts;
40                                 $na{$ing} = $a;
41                                 addprop(\%na, $rem-$a, \@nr);
42                         }
43                 }
44         }
45 }
46
47 addprop({}, 100, [ keys %prop ]);
48
49 say $max;