From 43c9d56726b1e2a6707fc322e5eea7a74402bad2 Mon Sep 17 00:00:00 2001 From: s00ner Date: Sat, 16 Jul 2022 11:55:18 -0400 Subject: [PATCH] Solved Day 9 --- day09/input | 28 ++++++++++++++++++++++++++++ day09/input.testing | 3 +++ day09/solution.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 day09/input create mode 100644 day09/input.testing create mode 100644 day09/solution.rb diff --git a/day09/input b/day09/input new file mode 100644 index 0000000..97a6b63 --- /dev/null +++ b/day09/input @@ -0,0 +1,28 @@ +Faerun to Tristram = 65 +Faerun to Tambi = 129 +Faerun to Norrath = 144 +Faerun to Snowdin = 71 +Faerun to Straylight = 137 +Faerun to AlphaCentauri = 3 +Faerun to Arbre = 149 +Tristram to Tambi = 63 +Tristram to Norrath = 4 +Tristram to Snowdin = 105 +Tristram to Straylight = 125 +Tristram to AlphaCentauri = 55 +Tristram to Arbre = 14 +Tambi to Norrath = 68 +Tambi to Snowdin = 52 +Tambi to Straylight = 65 +Tambi to AlphaCentauri = 22 +Tambi to Arbre = 143 +Norrath to Snowdin = 8 +Norrath to Straylight = 23 +Norrath to AlphaCentauri = 136 +Norrath to Arbre = 115 +Snowdin to Straylight = 101 +Snowdin to AlphaCentauri = 84 +Snowdin to Arbre = 96 +Straylight to AlphaCentauri = 107 +Straylight to Arbre = 14 +AlphaCentauri to Arbre = 46 diff --git a/day09/input.testing b/day09/input.testing new file mode 100644 index 0000000..d8224f9 --- /dev/null +++ b/day09/input.testing @@ -0,0 +1,3 @@ +London to Dublin = 464 +London to Belfast = 518 +Dublin to Belfast = 141 \ No newline at end of file diff --git a/day09/solution.rb b/day09/solution.rb new file mode 100644 index 0000000..3085c36 --- /dev/null +++ b/day09/solution.rb @@ -0,0 +1,26 @@ +#!/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