AoC2015/day13/solution.rb

50 lines
1.2 KiB
Ruby

#!/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