aboutsummaryrefslogtreecommitdiff
path: root/rb/day13.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-11-06 20:28:53 +0000
committerNat Lasseter <user@4574.co.uk>2024-11-06 20:28:53 +0000
commitd7d62da5e9c2ca991885641e0a55c2907eb0fb7e (patch)
treed7b6fca43226fe6affc57f5ef52ce461e13d18b7 /rb/day13.rb
parent46a7ef78253879ca3b5147fdd42f8789983b0b30 (diff)
Do a bunch in ruby
Diffstat (limited to 'rb/day13.rb')
-rwxr-xr-xrb/day13.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/rb/day13.rb b/rb/day13.rb
new file mode 100755
index 0000000..3967d22
--- /dev/null
+++ b/rb/day13.rb
@@ -0,0 +1,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}"