Skip to content

Instantly share code, notes, and snippets.

@moserrya
Created March 21, 2013 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moserrya/5217408 to your computer and use it in GitHub Desktop.
Save moserrya/5217408 to your computer and use it in GitHub Desktop.
class CalculatesRoute
def self.calculate(points)
remaining_points = points
route = []
route << remaining_points.slice!(0)
until remaining_points == [] do
next_point = shortest_distance(route.last, remaining_points)
route << remaining_points.slice!(remaining_points.index(next_point))
end
route
end
def self.shortest_distance(from, possible)
distances = possible.map do |point|
{point: point, distance: Map.distance_between(from, point)}
end
distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment