Skip to content

Instantly share code, notes, and snippets.

@Xion Xion/truck_time.py
Created Nov 3, 2011

Embed
What would you like to do?
Very simple optimization problem
# The wreck of a plane in a desert is 15 miles from the nearest point A on a straight road.
# A rescue truck starts for the wreck at a point on the road that is 30 miles distant from A.
# If the truck can travel at 80 mph on the road and at 40 mph on a straight path in the desert,
# how far from A should the truck leave the road to reach the wreck in a minimum time?
from math import sqrt
def truck_time(turn_point):
road_distance = turn_point
skipped_road_distance = 30 - turn_point
desert_distance = sqrt(skipped_road_distance ** 2 + 15 ** 2)
road_time = road_distance / 80.0
desert_time = desert_distance / 40
return road_time + desert_time
for i in xrange(0, 30):
print "Turning at %s results in %s h" % (i, truck_time(i))
# Minimum is around 22, which agrees with intuitive guess 30 * (1/sqrt(2))
@djanatyn

This comment has been minimized.

Copy link

djanatyn commented Nov 5, 2011

cool :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.