aboutsummaryrefslogtreecommitdiff
path: root/rb/day8.rb
blob: b9041533d630fee60394d40b0b21437bc5a76a7a (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
#!/usr/bin/env ruby

distances = {}
locations = []

File.readlines("day8.input").map(&:strip).each do |line|
  path, weight = line.split(" = ")
  from, to = path.split(" to ")
  distances[from] = {} if distances[from].nil?
  distances[from][to] = weight.to_i
  distances[to] = {} if distances[to].nil?
  distances[to][from] = weight.to_i
  locations << from
  locations << to
end

locations.uniq!

lengths = locations.permutation.map { |path|
  path.each_cons(2).map { |from, to| distances[from][to] }.sum
}

puts "Part 1: #{lengths.min}"
puts "Part 2: #{lengths.max}"