require "letters" distances = Hash.new {|h,k| h[k] = {} } DATA.read.scan(/^(.*) to (.*) = (.*)$/).each do |from, to, distance| distance = distance.to_i distances[to][from] = distance distances[from][to] = distance end locations = distances.keys puts locations.permutation.map {|p| p.each_cons(2).map {|a,b| distances[a][b] }.inject(&:+) }.max __END__ 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