AoC2015/day09/solution.rb

27 lines
738 B
Ruby

#!/bin/env ruby
require 'pry'
distances = File.readlines('./input').map { |line| line.split(/\sto\s|=/).map(&:strip) }
locations = distances.map { |i| i[0..1] }.flatten.uniq.freeze
def travel(loc1, loc2, distances)
distances.filter_map do |pair|
pair if pair.include?(loc1) && pair.include?(loc2)
end.flatten[2].to_i
end
routes = locations.permutation.to_a
routes.each do |route|
total = 0
route.each_cons(2) do |start, finish|
total += travel(start, finish, distances)
end
route.push(total)
end
shortest = routes.flatten.reject { |item| item.is_a?(String) }.min
longest = routes.flatten.reject { |item| item.is_a?(String) }.max
puts "Part 1 Solution: #{shortest}"
puts "Part 2 Solution: #{longest}"
# binding.pry