day13.pl (810B) - raw
1 #!/usr/local/bin/perl 2 # day 2015-13 3 4 use strict; 5 use warnings; 6 use v5.32; 7 use lib '.'; 8 use Day13 qw( calc_tot_hap_chg ); 9 10 @ARGV = "input" unless @ARGV; 11 chomp( my $input = do { local $/; <> } ); 12 my @deltas = split /\n/, $input; 13 my %people; 14 my %hap_units; 15 16 foreach ( @deltas ) { 17 my ( $person, $gain_lose, $units, $neighbor ) = 18 m/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./; 19 $hap_units{$person}{$neighbor} = $gain_lose eq 'gain' ? $units : -1 * $units; 20 $people{ $person } += 1; 21 } 22 23 my @people = sort keys %people; 24 25 say "part 1: ", calc_tot_hap_chg( \@people, \%hap_units ); 26 27 foreach my $person ( @people ) { 28 $hap_units{ me }{ $person } = 0; 29 $hap_units{ $person }{ me } = 0; 30 } 31 32 push @people => 'me'; 33 34 say "part 2: ", calc_tot_hap_chg( \@people, \%hap_units );