Browse Source

Add solutions to day 13

Olyol95 8 years ago
parent
commit
6ae14302dd
3 changed files with 119 additions and 0 deletions
  1. 51 0
      day13/day13
  2. 56 0
      day13/input.txt
  3. 12 0
      day13/test.txt

+ 51 - 0
day13/day13

@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+use Algorithm::Permute;
+
+use strict;
+use warnings;
+
+my %model;
+
+open (my $fh, '<', shift) or die "error opening input file: $!";
+
+while (<$fh>) {
+    $model{$1}->{$3} = "-$2" if ($_ =~ /^(\w+) would lose (\d+) happiness units by sitting next to (\w+)/i);
+    $model{$1}->{$3} = "$2"  if ($_ =~ /^(\w+) would gain (\d+) happiness units by sitting next to (\w+)/i);
+}
+
+close $fh;
+
+my @keys = keys %model;
+print calculate_optimum(\@keys) . "\n";
+
+foreach (keys %model) {
+    $model{ 'Me' }->{ $_ } = 0;
+    $model{ $_ }->{ 'Me' } = 0;
+}
+
+@keys = keys %model;
+print calculate_optimum(\@keys) . "\n";
+
+sub calculate_optimum {
+    my $optimum = 0;
+    my $permutation = new Algorithm::Permute(\@keys);
+    while (my @result = $permutation->next) {
+        my $score = calculate_arrangement_score(\@result);
+        $optimum = $score if $score > $optimum;
+    }
+    return $optimum;
+}
+
+sub calculate_arrangement_score {
+    my $arrangement = shift;
+    my $score = 0;
+    for (0 .. scalar(@$arrangement) - 1) {
+        $score += $model{ $arrangement->[$_] }->{ $arrangement->[$_+1] } if $_ <  (scalar(@$arrangement) -1);
+        $score += $model{ $arrangement->[$_+1] }->{ $arrangement->[$_] } if $_ <  (scalar(@$arrangement) -1);
+        $score += $model{ $arrangement->[$_] }->{ $arrangement->[0]    } if $_ == (scalar(@$arrangement) -1);
+        $score += $model{ $arrangement->[0]  }->{ $arrangement->[$_]   } if $_ == (scalar(@$arrangement) -1);
+    }
+    return $score;
+}
+

+ 56 - 0
day13/input.txt

@@ -0,0 +1,56 @@
+Alice would gain 2 happiness units by sitting next to Bob.
+Alice would gain 26 happiness units by sitting next to Carol.
+Alice would lose 82 happiness units by sitting next to David.
+Alice would lose 75 happiness units by sitting next to Eric.
+Alice would gain 42 happiness units by sitting next to Frank.
+Alice would gain 38 happiness units by sitting next to George.
+Alice would gain 39 happiness units by sitting next to Mallory.
+Bob would gain 40 happiness units by sitting next to Alice.
+Bob would lose 61 happiness units by sitting next to Carol.
+Bob would lose 15 happiness units by sitting next to David.
+Bob would gain 63 happiness units by sitting next to Eric.
+Bob would gain 41 happiness units by sitting next to Frank.
+Bob would gain 30 happiness units by sitting next to George.
+Bob would gain 87 happiness units by sitting next to Mallory.
+Carol would lose 35 happiness units by sitting next to Alice.
+Carol would lose 99 happiness units by sitting next to Bob.
+Carol would lose 51 happiness units by sitting next to David.
+Carol would gain 95 happiness units by sitting next to Eric.
+Carol would gain 90 happiness units by sitting next to Frank.
+Carol would lose 16 happiness units by sitting next to George.
+Carol would gain 94 happiness units by sitting next to Mallory.
+David would gain 36 happiness units by sitting next to Alice.
+David would lose 18 happiness units by sitting next to Bob.
+David would lose 65 happiness units by sitting next to Carol.
+David would lose 18 happiness units by sitting next to Eric.
+David would lose 22 happiness units by sitting next to Frank.
+David would gain 2 happiness units by sitting next to George.
+David would gain 42 happiness units by sitting next to Mallory.
+Eric would lose 65 happiness units by sitting next to Alice.
+Eric would gain 24 happiness units by sitting next to Bob.
+Eric would gain 100 happiness units by sitting next to Carol.
+Eric would gain 51 happiness units by sitting next to David.
+Eric would gain 21 happiness units by sitting next to Frank.
+Eric would gain 55 happiness units by sitting next to George.
+Eric would lose 44 happiness units by sitting next to Mallory.
+Frank would lose 48 happiness units by sitting next to Alice.
+Frank would gain 91 happiness units by sitting next to Bob.
+Frank would gain 8 happiness units by sitting next to Carol.
+Frank would lose 66 happiness units by sitting next to David.
+Frank would gain 97 happiness units by sitting next to Eric.
+Frank would lose 9 happiness units by sitting next to George.
+Frank would lose 92 happiness units by sitting next to Mallory.
+George would lose 44 happiness units by sitting next to Alice.
+George would lose 25 happiness units by sitting next to Bob.
+George would gain 17 happiness units by sitting next to Carol.
+George would gain 92 happiness units by sitting next to David.
+George would lose 92 happiness units by sitting next to Eric.
+George would gain 18 happiness units by sitting next to Frank.
+George would gain 97 happiness units by sitting next to Mallory.
+Mallory would gain 92 happiness units by sitting next to Alice.
+Mallory would lose 96 happiness units by sitting next to Bob.
+Mallory would lose 51 happiness units by sitting next to Carol.
+Mallory would lose 81 happiness units by sitting next to David.
+Mallory would gain 31 happiness units by sitting next to Eric.
+Mallory would lose 73 happiness units by sitting next to Frank.
+Mallory would lose 89 happiness units by sitting next to George.

+ 12 - 0
day13/test.txt

@@ -0,0 +1,12 @@
+Alice would gain 54 happiness units by sitting next to Bob.
+Alice would lose 79 happiness units by sitting next to Carol.
+Alice would lose 2 happiness units by sitting next to David.
+Bob would gain 83 happiness units by sitting next to Alice.
+Bob would lose 7 happiness units by sitting next to Carol.
+Bob would lose 63 happiness units by sitting next to David.
+Carol would lose 62 happiness units by sitting next to Alice.
+Carol would gain 60 happiness units by sitting next to Bob.
+Carol would gain 55 happiness units by sitting next to David.
+David would gain 46 happiness units by sitting next to Alice.
+David would lose 7 happiness units by sitting next to Bob.
+David would gain 41 happiness units by sitting next to Carol.