Skip to content

Instantly share code, notes, and snippets.

@fogonthedowns
Created July 28, 2014 08:10
Show Gist options
  • Save fogonthedowns/83728c359551d0503a23 to your computer and use it in GitHub Desktop.
Save fogonthedowns/83728c359551d0503a23 to your computer and use it in GitHub Desktop.
Lost in the US
class City
attr_accessor :cities, :connections, :path, :total_time, :next_city, :count, :run_count, :nodes
PathBegins = "sf"
EndingTime = 1440 # 24 hours * 60
def initialize
@cities = ["bos", "nyc", "la", "sea", "jac", "chi", "phx", "med", "min", "gre", "oma", "dc", "mem", "den",
"abq", "dal", "dtw", "cha", "slc" ]
@connections = {
"sf"=>[["bos", 110], ["gre",32], ["slc", 23], ["la", 12], ["med",10]],
"bos"=>[["sf", 110], ["nyc", 7]],
"gre"=>[["sf", 32], ["min", 39], ["oma", 35], ["slc", 14], ["den", 21], ["sea", 24]],
"slc"=>[["sf", 23], ["gre", 14], ["abq", 16], ["den", 15]],
"la" =>[["sf", 12], ["chi", 67], ["phx", 13], ["nyc", 96]],
"med"=>[["sf", 10], ["min", 63], ["oma", 57], ["sea", 11]],
"nyc"=>[["bos", 10], ["la", 96], ["dc", 8], ["dtw", 20]],
"min"=>[["gre", 39], ["med", 63], ["mem", 22], ["chi", 14], ["oma", 10], ["dtw", 22]],
"oma"=>[["gre", 35], ["med", 57], ["min", 10], ["dal", 18], ["chi", 18], ["den", 19]],
"den"=>[["gre", 21], ["slc", 15], ["oma", 19], ["dal", 23], ["abq", 10]],
"sea"=>[["gre", 24], ["med", 11], ["jac", 94], ["phx", 37]],
"abq"=>[["slc", 16], ["den", 10], ["dal", 21], ["phx", 12]],
"chi"=>[["la", 67], ["min", 14], ["oma", 18], ["dtw", 10], ["mem", 15], ["cha", 20]],
"phx"=>[["la", 13], ["sea", 37], ["abq", 12], ["jac", 65]],
"dc"=>[["nyc", 8], ["mem", 29], ["cha", 11], ["dtw", 15]],
"dtw"=>[["nyc", 20], ["min", 22], ["dc", 15], ["chi", 10]],
"mem"=>[["min", 22], ["dc", 29], ["jac", 20], ["cha", 19], ["dal", 15], ["chi", 15]],
"dal"=>[["oma", 18], ["den", 23], ["abq", 21], ["mem", 15]],
"jac"=>[["sea", 94], ["mem", 20], ["cha", 11], ["phx", 65]],
"cha"=>[["dc", 11], ["mem", 19], ["jac", 11], ["chi", 20]]
}
@connection_map = {
"sf"=> ["bos", "gre", "slc", "la", "med"],
"bos"=>["sf", "nyc"],
"gre"=>["sf", "min", "oma", "slc", "den", "sea"],
"slc"=>["sf", "gre", "abq", "den"],
"la" =>["sf", "chi", "phx", "nyc"],
"med"=>["sf", "min", "oma", "sea"],
"nyc"=>["bos", "la", "dc", "dtw"],
"min"=>["gre", "med", "mem", "chi", "oma", "dtw"],
"oma"=>["gre", "med", "min", "dal", "chi", "den"],
"den"=>["gre", "slc", "oma", "dal", "abq"],
"sea"=>["gre", "slc", "oma", "dal", "abq"],
"abq"=>["slc", "den", "dal", "phx"],
"chi"=>["la", "min", "oma", "dtw", "mem", "cha"],
"phx"=>["la", "sea", "abq", "jac"],
"dc"=> ["nyc", "mem", "cha", "dtw"],
"dtw"=>["nyc", "min", "dc", "chi"],
"mem"=>["min", "dc", "jac", "cha", "dal", "chi"],
"dal"=>["oma", "den", "abq", "mem"],
"jac"=>["sea", "cha", "phx"],
"cha"=>["dc", "mem", "jac", "chi"]
}
@total_time = 195 #start at 3 hours 15 minutes, expressed in minutes
@count = 0
@run_count = 0
@nodes = self.connections[City::PathBegins]
@path = []
end
def brute
pick_a_city_to_travel_to
@pick_a_city_to_travel_to
self.total_time += @next_city[1] # Adds travel time. Gets travel time from [city, travel_time]
self.path << @next_city[0] # log the journey
@cities.delete(@next_city[0]) # removes the city from the visit list
self.brute if self.total_time < EndingTime && !cities.empty?
end
def run_program
until (self.cities.empty? || @run_count == 10000) do
puts "."
puts self.cities.count
# puts self.cities
self.reset_values
self.brute
end
end
def pick_a_city_to_travel_to
@next_city = @nodes.sample # get a random city
@nodes = self.connections[@next_city[0]] # get the new connections from city string
end
def reset_values
@cities = ["bos", "nyc", "la", "sea", "jac", "chi", "phx", "med", "min", "gre", "oma", "dc", "mem", "den",
"abq", "dal", "dtw", "cha", "slc" ]
@total_time = 195 #start at 3 hours 15 minutes, expressed in minutes
@count = 0
@nodes = self.connections[City::PathBegins]
@path = []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment