commit fe8dff04d75dff3a58583c6277b131a38df93441
parent 29df2e335cb55dba93041e45c05e50a2a9ce0fd6
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sat, 5 Nov 2022 01:34:39 +0000
solve part 1 of day13
Diffstat:
M | day13/day13.pl | | | 61 | +++++++++++++++++++++++++++++++++++-------------------------- |
A | day13/input | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 91 insertions(+), 26 deletions(-)
diff --git a/day13/day13.pl b/day13/day13.pl
@@ -3,49 +3,58 @@
use strict;
use warnings;
+use diagnostics;
use v5.32;
-
-@ARGV = "input" unless @ARGV;
-
-chomp( my $input = do { local $/; <> } );
-
-use List::Util qw( min sum product );
+use List::Util qw( max );
use Algorithm::Permute;
use Data::Dumper;
+@ARGV = "input" unless @ARGV;
+chomp( my $input = do { local $/; <> } );
my @deltas = split /\n/, $input;
my %people;
my %hap_units;
foreach ( @deltas ) {
-# say;
-# my @matches =
-# m/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./;
my ( $person, $gain_lose, $units, $neighbor ) =
m/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)./;
-# say join " | ", @matches;
-# $hap_units{ $matches[0] }{ $matches[3] } = $matches[1] eq 'gain' ? $matches[2] : -1 * $matches[2];
- $hap_units{ $person }{ $neighbor } =
- $gain_lose eq 'gain' ? $units : -1 * $units;
+ $hap_units{$person}{$neighbor} = $gain_lose eq 'gain' ? $units : -1 * $units;
$people{ $person } += 1;
}
my @people = sort keys %people;
-#say "part 1: ";
-#say "part 2: ";
+my @answers;
-#say Dumper \%hap_units;
+#foreach my $person ( keys %hap_units ) {
+# foreach my $neighbor ( keys %{ $hap_units{ $person } } ) {
+# say "sitting $person next to $neighbor would net ",
+# $hap_units{ $person }{ $neighbor }, " points.";
+# }
+#}
-foreach my $person ( keys %hap_units ) {
- foreach my $neighbor ( keys %{ $hap_units{ $person } } ) {
- say "sitting $person next to $neighbor would net ",
- $hap_units{ $person }{ $neighbor }, " points.";
- }
-}
-
-say @people;
-say "-------------";
+#say @people;
+#say "-------------";
Algorithm::Permute::permute {
- say @people;
+ # since we are already iterating on @people, it is a read-only variable
+ # within this loop and thus we cannot unshift or push on it. Therefore,
+ # we have to copy it into a new variable, @seat_assign
+ my @seat_assign = @people;
+ my $hap_change = 0;
+ print @seat_assign, " ---> ";
+ unshift @seat_assign => $seat_assign[ -1 ];
+ push @seat_assign => $seat_assign[ 1 ];
+ say @seat_assign;
+ foreach my $position ( 1 .. scalar @people ) {
+ my $person = $seat_assign[ $position ];
+ my $left = $seat_assign[ $position - 1 ];
+ my $right = $seat_assign[ $position + 1 ];
+ $hap_change += $hap_units{ $person }{ $left };
+ $hap_change += $hap_units{ $person }{ $right };
+ }
+ push @answers => $hap_change;
} @people;
+
+
+say "part 1: ", max @answers;
+#say "part 2: ";
diff --git a/day13/input b/day13/input
@@ -0,0 +1,56 @@
+Alice would lose 2 happiness units by sitting next to Bob.
+Alice would lose 62 happiness units by sitting next to Carol.
+Alice would gain 65 happiness units by sitting next to David.
+Alice would gain 21 happiness units by sitting next to Eric.
+Alice would lose 81 happiness units by sitting next to Frank.
+Alice would lose 4 happiness units by sitting next to George.
+Alice would lose 80 happiness units by sitting next to Mallory.
+Bob would gain 93 happiness units by sitting next to Alice.
+Bob would gain 19 happiness units by sitting next to Carol.
+Bob would gain 5 happiness units by sitting next to David.
+Bob would gain 49 happiness units by sitting next to Eric.
+Bob would gain 68 happiness units by sitting next to Frank.
+Bob would gain 23 happiness units by sitting next to George.
+Bob would gain 29 happiness units by sitting next to Mallory.
+Carol would lose 54 happiness units by sitting next to Alice.
+Carol would lose 70 happiness units by sitting next to Bob.
+Carol would lose 37 happiness units by sitting next to David.
+Carol would lose 46 happiness units by sitting next to Eric.
+Carol would gain 33 happiness units by sitting next to Frank.
+Carol would lose 35 happiness units by sitting next to George.
+Carol would gain 10 happiness units by sitting next to Mallory.
+David would gain 43 happiness units by sitting next to Alice.
+David would lose 96 happiness units by sitting next to Bob.
+David would lose 53 happiness units by sitting next to Carol.
+David would lose 30 happiness units by sitting next to Eric.
+David would lose 12 happiness units by sitting next to Frank.
+David would gain 75 happiness units by sitting next to George.
+David would lose 20 happiness units by sitting next to Mallory.
+Eric would gain 8 happiness units by sitting next to Alice.
+Eric would lose 89 happiness units by sitting next to Bob.
+Eric would lose 69 happiness units by sitting next to Carol.
+Eric would lose 34 happiness units by sitting next to David.
+Eric would gain 95 happiness units by sitting next to Frank.
+Eric would gain 34 happiness units by sitting next to George.
+Eric would lose 99 happiness units by sitting next to Mallory.
+Frank would lose 97 happiness units by sitting next to Alice.
+Frank would gain 6 happiness units by sitting next to Bob.
+Frank would lose 9 happiness units by sitting next to Carol.
+Frank would gain 56 happiness units by sitting next to David.
+Frank would lose 17 happiness units by sitting next to Eric.
+Frank would gain 18 happiness units by sitting next to George.
+Frank would lose 56 happiness units by sitting next to Mallory.
+George would gain 45 happiness units by sitting next to Alice.
+George would gain 76 happiness units by sitting next to Bob.
+George would gain 63 happiness units by sitting next to Carol.
+George would gain 54 happiness units by sitting next to David.
+George would gain 54 happiness units by sitting next to Eric.
+George would gain 30 happiness units by sitting next to Frank.
+George would gain 7 happiness units by sitting next to Mallory.
+Mallory would gain 31 happiness units by sitting next to Alice.
+Mallory would lose 32 happiness units by sitting next to Bob.
+Mallory would gain 95 happiness units by sitting next to Carol.
+Mallory would gain 91 happiness units by sitting next to David.
+Mallory would lose 66 happiness units by sitting next to Eric.
+Mallory would lose 75 happiness units by sitting next to Frank.
+Mallory would lose 99 happiness units by sitting next to George.