From ab083f75b8ff6ea6b0b115a15ff39795dfbcfaa3 Mon Sep 17 00:00:00 2001 From: s00ner Date: Sun, 17 Jul 2022 17:33:26 -0400 Subject: [PATCH] Solved Day 13 --- day13/input | 56 +++++++++++++++++++++++++++++++++++++++++++++ day13/input.testing | 12 ++++++++++ day13/solution.rb | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 day13/input create mode 100644 day13/input.testing create mode 100644 day13/solution.rb diff --git a/day13/input b/day13/input new file mode 100644 index 0000000..83755b0 --- /dev/null +++ b/day13/input @@ -0,0 +1,56 @@ +Alice would lose 57 happiness units by sitting next to Bob. +Alice would lose 62 happiness units by sitting next to Carol. +Alice would lose 75 happiness units by sitting next to David. +Alice would gain 71 happiness units by sitting next to Eric. +Alice would lose 22 happiness units by sitting next to Frank. +Alice would lose 23 happiness units by sitting next to George. +Alice would lose 76 happiness units by sitting next to Mallory. +Bob would lose 14 happiness units by sitting next to Alice. +Bob would gain 48 happiness units by sitting next to Carol. +Bob would gain 89 happiness units by sitting next to David. +Bob would gain 86 happiness units by sitting next to Eric. +Bob would lose 2 happiness units by sitting next to Frank. +Bob would gain 27 happiness units by sitting next to George. +Bob would gain 19 happiness units by sitting next to Mallory. +Carol would gain 37 happiness units by sitting next to Alice. +Carol would gain 45 happiness units by sitting next to Bob. +Carol would gain 24 happiness units by sitting next to David. +Carol would gain 5 happiness units by sitting next to Eric. +Carol would lose 68 happiness units by sitting next to Frank. +Carol would lose 25 happiness units by sitting next to George. +Carol would gain 30 happiness units by sitting next to Mallory. +David would lose 51 happiness units by sitting next to Alice. +David would gain 34 happiness units by sitting next to Bob. +David would gain 99 happiness units by sitting next to Carol. +David would gain 91 happiness units by sitting next to Eric. +David would lose 38 happiness units by sitting next to Frank. +David would gain 60 happiness units by sitting next to George. +David would lose 63 happiness units by sitting next to Mallory. +Eric would gain 23 happiness units by sitting next to Alice. +Eric would lose 69 happiness units by sitting next to Bob. +Eric would lose 33 happiness units by sitting next to Carol. +Eric would lose 47 happiness units by sitting next to David. +Eric would gain 75 happiness units by sitting next to Frank. +Eric would gain 82 happiness units by sitting next to George. +Eric would gain 13 happiness units by sitting next to Mallory. +Frank would gain 77 happiness units by sitting next to Alice. +Frank would gain 27 happiness units by sitting next to Bob. +Frank would lose 87 happiness units by sitting next to Carol. +Frank would gain 74 happiness units by sitting next to David. +Frank would lose 41 happiness units by sitting next to Eric. +Frank would lose 99 happiness units by sitting next to George. +Frank would gain 26 happiness units by sitting next to Mallory. +George would lose 63 happiness units by sitting next to Alice. +George would lose 51 happiness units by sitting next to Bob. +George would lose 60 happiness units by sitting next to Carol. +George would gain 30 happiness units by sitting next to David. +George would lose 100 happiness units by sitting next to Eric. +George would lose 63 happiness units by sitting next to Frank. +George would gain 57 happiness units by sitting next to Mallory. +Mallory would lose 71 happiness units by sitting next to Alice. +Mallory would lose 28 happiness units by sitting next to Bob. +Mallory would lose 10 happiness units by sitting next to Carol. +Mallory would gain 44 happiness units by sitting next to David. +Mallory would gain 22 happiness units by sitting next to Eric. +Mallory would gain 79 happiness units by sitting next to Frank. +Mallory would lose 16 happiness units by sitting next to George. diff --git a/day13/input.testing b/day13/input.testing new file mode 100644 index 0000000..82e9d8a --- /dev/null +++ b/day13/input.testing @@ -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. \ No newline at end of file diff --git a/day13/solution.rb b/day13/solution.rb new file mode 100644 index 0000000..f9e1ba5 --- /dev/null +++ b/day13/solution.rb @@ -0,0 +1,49 @@ +#!/bin/env ruby +require 'pry' + +input = File.readlines('./input').map(&:chomp) +data = {} + +input.each do |line| + line = line.split + data[line[0].to_sym] = {} unless data.has_key?(line[0].to_sym) + num = line[2] == 'lose' ? -line[3].to_i : line[3].to_i + data[line[0].to_sym][line[-1].delete('.').to_sym] = num +end + +names = input.map { |line| line.split[0].to_sym }.uniq.freeze +arrangments = [] +names.permutation { |perm| arrangments.push(perm) } +arrangments.map! { |arrangment| arrangment.push(arrangment[0]) } +happiness = [] + +happiness = arrangments.map do |arrangment| + arrangment.each_cons(2).map do |name1, name2| + data[name1][name2] + data[name2][name1] + end.sum +end + +puts "Part 1 Solution: #{happiness.max}" +# Part 2 (copy/pasting code, too lazy to make a method) + +data[:Jeff] = {} +names.each do |name| + data[:Jeff][name] = 0 + data[name][:Jeff] = 0 +end +new_names = names.dup +new_names.push(:Jeff).freeze +arrangments = [] +new_names.permutation { |perm| arrangments.push(perm) } +arrangments.map! { |arrangment| arrangment.push(arrangment[0]) } +happiness = [] + +happiness = arrangments.map do |arrangment| + arrangment.each_cons(2).map do |name1, name2| + data[name1][name2] + data[name2][name1] + end.sum +end + +puts "Part 2 Solution: #{happiness.max}" + +# binding.pry