Skip to content

Instantly share code, notes, and snippets.

@d12
Created October 21, 2021 02:05
Show Gist options
  • Save d12/ab3fc616f495b0960edf7b81394cf8f2 to your computer and use it in GitHub Desktop.
Save d12/ab3fc616f495b0960edf7b81394cf8f2 to your computer and use it in GitHub Desktop.
Advent 2015 day 9
require "json"
require "byebug"
mapping = {}
list = File.read("9_input.txt").lines.map(&:chomp)
list.each do |l|
a, _, b, _, number = l.split(" ")
mapping[[a, b]] = number.to_i
mapping[[b ,a]] = number.to_i
end
min = Float::INFINITY
max = -Float::INFINITY
mapping.keys.flatten.uniq.permutation.each do |permutation|
sum = permutation.each_cons(2).sum do |a, b|
mapping[[a, b]]
end
min = [min, sum].min
max = [max, sum].max
end
puts "min: #{min}"
puts "max: #{max}"
require "json"
mapping = File.read("9_input.txt").lines.map(&:chomp).inject({}) do |hash, line|
a, _, b, _, number = line.split(" ")
hash[[a, b].sort] = number.to_i
hash
end
distances = mapping.keys.flatten.uniq.permutation.map do |permutation|
permutation.each_cons(2).sum { |a,b| mapping[[a,b].sort]}
end
puts "min: #{distances.min}"
puts "max: #{distances.max}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment