blob: 3967d228576ea990b763a2750a270bf39d88d70c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/usr/bin/env ruby
deltas = {}
whos = []
File.readlines("day13.input").map(&:strip).each do |line|
who, dir, delta, neighbour = line.scan(/(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)\./)[0]
delta = delta.to_i
delta = -delta if dir == "lose"
deltas[who] = {} if deltas[who].nil?
deltas[who][neighbour] = delta
whos << who
end
whos.uniq!
num = whos.length
change = whos.permutation.map { |perm|
num.times.map { |i|
left = (i - 1) % num
right = (i + 1) % num
[deltas[perm[i]][perm[left]], deltas[perm[i]][perm[right]]]
}.flatten.sum
}.max
puts "Part 1: #{change}"
deltas["me"] = {}
whos.each do |who|
deltas["me"][who] = 0
deltas[who]["me"] = 0
end
whos << "me"
num += 1
change = whos.permutation.map { |perm|
num.times.map { |i|
left = (i - 1) % num
right = (i + 1) % num
[deltas[perm[i]][perm[left]], deltas[perm[i]][perm[right]]]
}.flatten.sum
}.max
puts "Part 2: #{change}"
|