1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
regex = /(\w+) would (\w+) (\d+) happiness units by sitting next to (\w+)/
happiness = Hash.new {|h,k| h[k] = Hash.new(0) }
DATA.read.scan(regex).each do |left, sign, units, right|
  units = units.to_i
  units *= (sign == "gain") ? 1 : -1
  happiness[left][right] = units
end
happiness["Alpha"] = Hash.new(0)

puts happiness.keys.permutation.map { |arrangement|
  arrangement << arrangement.first
  arrangement.each_cons(2).map { |left,right|
    happiness[left][right] + happiness[right][left]
  }.inject(:+)
}.max
__END__