adventofcode/2015/13/1.pl

22 lines
580 B
Perl
Raw Normal View History

2017-12-06 17:48:31 +00:00
use 5.20.0;
my %happiness;
while(<>) {
/(?<from>\S+).*(?<gain>gain|lose) (?<happiness>\d+).*?(?<to>\S+)\.$/;
$happiness{$+{from}}{$+{to}} = $+{happiness} * ( $+{gain} eq 'gain' ? 1 : -1 );
}
use Algorithm::Combinatorics qw/ circular_permutations /;
use List::AllUtils qw/ max sum /;
say max map { happiness_for($_) } circular_permutations([ keys %happiness ]);
sub happiness_for {
my @group = @{ $_[0] };
sum map {
my $who = $group[$_];
map { $happiness{$who}{$group[ $_ ]} } map { $_ % @group } $_+1, $_ + @group - 1;
} 0..$#group;
}