Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panda level submission #12

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
220 changes: 220 additions & 0 deletions benchmark_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
CITIES = ["Acton, CA",
"Adelanto, CA",
"Agoura Hills, CA",
"Agua Dulce, CA",
"Alameda, CA",
"Alamo, CA",
"Albany, CA",
"Alhambra, CA",
"Aliso Viejo, CA",
"Alpine, CA",
"Alta Loma, CA",
"Altadena, CA",
"Alturas, CA",
"Amador City, CA",
"Anaheim, CA",
"Anaheim Hills, CA",
"Anderson, CA",
"Angels Camp, CA",
"Angelus Oaks, CA",
"Antelope, CA",
"Antioch, CA",
"Anza, CA",
"Apple Valley, CA",
"Applegate, CA",
"Aptos, CA",
"Arcadia, CA",
"Arcata, CA",
"Arleta, CA",
"Armona, CA",
"Arnold, CA",
"Aroura Hills, CA",
"Arroyo Grande, CA",
"Atascadero, CA",
"Atwater, CA",
"Atwood, CA",
"Auberry, CA",
"Auburn, CA",
"Avalon, CA",
"Avery, CA",
"Avila Beach, CA",
"Azusa, CA",
"Bakersfield, CA",
"Baldwin Park, CA",
"Bangor, CA",
"Banning, CA",
"Barstow, CA",
"Bay Point, CA",
"Bayside, CA",
"Bel Air, CA",
"Bell, CA",
"Bellflower, CA",
"Belmont, CA",
"Belmont Shores, CA",
"Belvedere, CA",
"Ben Lomond, CA",
"Benicia, CA",
"Berkeley, CA",
"Berry Creek, CA",
"Beverly Hills, CA",
"Big Bear City, CA",
"Big Bear Lake, CA",
"Big Sur, CA",
"Bishop, CA",
"Blue Jay, CA",
"Blue Lake, CA",
"Blythe, CA",
"Bolinas, CA",
"Bonita, CA",
"Borrego Springs, CA",
"Bothell, CA",
"Boulder Creek, CA",
"Brea, CA",
"Breenbrae, CA",
"Brentwood, CA",
"Brisbane, CA",
"Buellton, CA",
"Buena Park, CA",
"Burbank, CA",
"Burlingame, CA",
"Burlington, CA",
"Calabasas, CA",
"California City, CA",
"Calimesa, CA",
"Calistoga, CA",
"Camarillo, CA",
"Cambria, CA",
"Cameron Park, CA",
"Campbell, CA",
"Campo, CA",
"Canoga Park, CA",
"Canyon Country, CA",
"Canyon Lake, CA",
"Capistrano Beach, CA",
"Capitola, CA",
"Cardiff, CA",
"Cardiff-by-the-Sea, CA",
"Carlsbad, CA",
"Carmel, CA",
"Carmel Valley, CA",
"Carmichael, CA",
"Carpinteria, CA",
"Carson, CA",
"Castaic, CA",
"Castro Valley, CA",
"Catalina Island, CA",
"Cathedral City, CA",
"Cayucos, CA",
"Cedar Glen, CA",
"Cedar Ridge, CA",
"Ceres, CA",
"Cerritos, CA",
"Chatsworth, CA",
"Cherry Valley, CA",
"Chester, CA",
"Chico, CA",
"China Lake, CA",
"Chino, CA",
"Chino Hills, CA",
"Chula Vista, CA",
"Citrus Heights, CA",
"City of Industry, CA",
"Claremont, CA",
"Clayton, CA",
"Clearlake Oaks, CA",
"Clearlake, CA",
"Cloverdale, CA",
"Clovis, CA",
"Coarsegold, CA",
"Colfax, CA",
"Coloma, CA",
"Colton, CA",
"Columbia, CA",
"Colusa, CA",
"Commerce, CA",
"Compton, CA",
"Concord, CA",
"Cordelia, CA",
"Corning, CA",
"Corona, CA",
"Corona del Mar, CA",
"Coronado, CA",
"Corralitos, CA",
"Corte Madera, CA",
"Costa Mesa, CA",
"Cotati, CA",
"Coto de Caza, CA",
"Covina, CA",
"CresceCutten, CA",
"Cypress, CA",
"Daly City, CA",
"Dana Point, CA",
"Danville, CA",
"Davis, CA",
"Del Mar, CA",
"Desert Hot Springs, CA",
"Diamond Bar, CA",
"Diamond Springs, CA",
"Dobbins, CA",
"Dominguez Hills, CA",
"Dove Canyon, CA",
"Downey, CA",
"Duarte, CA",
"Dublin, CA",
"Hemet, CA",
"Hercules, CA",
"Hermosa Beach, CA",
"Hesperia, CA",
"Highland, CA",
"Hinkley, CA",
"Hollister, CA",
"Hollywood, CA",
"Homeland, CA",
"Honcut, CA",
"Humboldt, CA",
"Huntington Beach, CA",
"Huntington Park, CA",
"Idyllwild, CA",
"Imperial, CA",
"Imperial Beach, CA",
"Independence, CA",
"Indian Wells, CA",
"Indio, CA",
"Inglewood, CA",
"Inverness, CA",
"Ione, CA",
"Irvine, CA",
"Irwindale, CA",
"Isla Vista, CA",
"Isleton, CA",
"Jackson, CA",
"Jacumba, CA",
"Jamestown, CA",
"Jamul, CA",
"Jenner, CA",
"Joshua Tree, CA",
"Julian, CA",
"Kelseyville, CA",
"Kensington, CA",
"Kentfield, CA",
"Kenwood, CA",
"King City, CA",
"Klamath, CA",
"Klamath River, CA",
"La Canada, CA",
"La Canada Flintridge, CA",
"La Crescenta, CA",
"La Habra, CA",
"La Honda, CA",
"Lake Elsinore, CA",
"Lake Forest, CA",
"Lake Isabella, CA",
"Lakeport, CA",
"Lakeside, CA",
"Vineburg, CA",
"Visalia, CA",
"Vista, CA",
"Walnut, CA",
"Walnut Creek, CA",
"Warm Springs, CA",
"Watsoncca Valley, CA"]
36 changes: 36 additions & 0 deletions benchmark_salesperson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Dir["./lib/*.rb"].each {|file| require file }

require 'benchmark'
require_relative "benchmark_helper"

phil = SalesPerson.new

Benchmark.bm do |x|
x.report("2 cities") do
phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true)
1.upto(2) do |city|
phil.schedule_city(Place.build(CITIES[city]))
end
end

x.report("10 cities") do
phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true)
1.upto(10) do |city|
phil.schedule_city(Place.build(CITIES[city]))
end
end

x.report("50 cities") do
phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true)
1.upto(50) do |city|
phil.schedule_city(Place.build(CITIES[city]))
end
end

x.report("200 cities") do
phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true)
1.upto(200) do |city|
phil.schedule_city(Place.build(CITIES[city]))
end
end
end
22 changes: 0 additions & 22 deletions lib/calculates_route.rb

This file was deleted.

41 changes: 41 additions & 0 deletions lib/route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Route
attr_reader :miles_traveled

def initialize
@miles_traveled = 0
end

def calculate(points)
remaining_points = points
route = []
route << remaining_points.slice!(0)
until remaining_points == [] do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally a prefence, but I like

while remaining_points.any? do
end

next_point = shortest_distance(route.last, remaining_points)
route << remaining_points.slice!(remaining_points.index(next_point))
end
route
end

def shortest_distance(from, possible)
distances = possible.map do |point|
{point: point, distance: Map.distance_between(from, point)}
end
get_the_shortest_distance_from(distances)
end

def get_the_shortest_distance_from(distances)
shortest_distance = distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}
add_to_miles_traveled(shortest_distance.first.fetch(:distance))
shortest_distance.first.fetch(:point)
end

def add_to_miles_traveled(miles)
@miles_traveled += miles
end

def time_traveled
@miles_traveled / 55
end
end


19 changes: 16 additions & 3 deletions lib/sales_person.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
class SalesPerson

attr_reader :cities
attr_reader :cities, :starting_city
def initialize
@cities = []
@route = Route.new
end

def schedule_city(city)
def schedule_city(city, options={})
if options[:starting_city] == true
@starting_city ||= city
@cities.unshift(@starting_city)
end
@cities << city unless @cities.include?(city)
end

def route
CalculatesRoute.calculate(cities)
@route.calculate(cities)
end

def miles_traveled
@route.miles_traveled
end

def time_traveled
@route.time_traveled
end
end
17 changes: 11 additions & 6 deletions salesperson.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Dir["./lib/*.rb"].each {|file| require file }


phil = SalesPerson.new
phil.schedule_city(Place.build("Dallas, TX"))
phil.schedule_city(Place.build("El Paso, TX"))
phil.schedule_city(Place.build("Austin, TX"))
phil.schedule_city(Place.build("Lubbock, TX"))
jason = SalesPerson.new
jason.schedule_city(Place.build("Whittier, CA"))
jason.schedule_city(Place.build("Chino Hills, CA"), starting_city: true)
jason.schedule_city(Place.build("Brea, CA"))
jason.schedule_city(Place.build("Irvine, CA"))

puts phil.route
puts "-----------------------------"
puts "Here's my route:"
puts jason.route
puts "-----------------------------"
puts "This route will cover #{jason.miles_traveled.round(1)} miles, as the crow flies."
puts "If that crow flew 60 mph it'd take #{jason.time_traveled.round(2)} hours."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe you calculate at 55 mph though :)

Loading