Skip to content

Instantly share code, notes, and snippets.

@dougdroper
Last active August 29, 2015 14:02
Show Gist options
  • Save dougdroper/c497cdd7b56cf636948a to your computer and use it in GitHub Desktop.
Save dougdroper/c497cdd7b56cf636948a to your computer and use it in GitHub Desktop.
plot
# A -> B
# C -> D
# B -> E
# A -> E
# E -> Y
# Y -> Z
# Z -> X
def routes
{a: [:b, :e], c: [:d], b: [:e], e: [:y], y: [:z], z: [:x] }
end
def find(routes, start, finish, path)
path += [start]
return [path] if start == finish
return unless routes.has_key?(start)
routes[start].map { |e| find(routes, e, finish, path) unless path.include?(e) }
end
def plot(start, finish)
find(routes, start, finish, []).map {|p| p.flatten.compact.join(" -> ") }
end
puts plot(:a, :x) # ->
# a -> b -> e -> y -> z -> x
# a -> e -> y -> z -> x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment