#!/usr/local/bin/perl # day 2015-13 use strict; use warnings; use v5.32; use lib '.'; use Day13 qw( calc_tot_hap_chg ); @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); my @deltas = split /\n/, $input; my %people; my %hap_units; foreach ( @deltas ) { my ( $person, $gain_lose, $units, $neighbor ) = m/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./; $hap_units{$person}{$neighbor} = $gain_lose eq 'gain' ? $units : -1 * $units; $people{ $person } += 1; } my @people = sort keys %people; say "part 1: ", calc_tot_hap_chg( \@people, \%hap_units ); foreach my $person ( @people ) { $hap_units{ me }{ $person } = 0; $hap_units{ $person }{ me } = 0; } push @people => 'me'; say "part 2: ", calc_tot_hap_chg( \@people, \%hap_units );