aboutsummaryrefslogtreecommitdiff
path: root/rb/day8.rb
diff options
context:
space:
mode:
Diffstat (limited to 'rb/day8.rb')
-rwxr-xr-xrb/day8.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/rb/day8.rb b/rb/day8.rb
new file mode 100755
index 0000000..b904153
--- /dev/null
+++ b/rb/day8.rb
@@ -0,0 +1,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}"