Advertisement
guitarplayer616

estimateTime

Jan 16th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. function formatTime(seconds)
  2.     local hours,minutes = 0,0
  3.     if seconds > 60 then
  4.         minutes = math.floor(seconds/60)
  5.         seconds = seconds%60
  6.     end
  7.     if minutes > 60 then
  8.         hours = math.floor(minutes/60)
  9.         minutes = minutes%60
  10.     end
  11.     if hours == 0 then
  12.         if minutes == 0 then
  13.             return tostring(seconds).." seconds"
  14.         else
  15.             return tostring(minutes).." minutes "..tostring(seconds).." seconds"
  16.         end
  17.     else
  18.         if minutes == 0 then
  19.             return tostring(hours).." hours "..tostring(seconds).." seconds"
  20.         else
  21.             return tostring(hours).." hours "..tostring(minutes).." minutes "..tostring(seconds).." seconds"
  22.         end
  23.     end
  24. end
  25.  
  26. local function getCost(x1,y1,z1,x2,y2,z2)
  27.     local deltax,deltay,deltaz = math.abs(x2-x1),math.abs(y2-y1),math.abs(z2-z1)
  28.     local turnCost = 1
  29.     if deltay == 0 or deltaz == 0 then
  30.         turnCost = 0   
  31.     end
  32.     local timeCost = deltax + deltay + deltaz + turnCost
  33.     local fuelCost = deltax + deltay + deltaz
  34.     return timeCost,fuelCost
  35. end
  36.  
  37. function estimate(nObjective,instructions,x,y,z)
  38.     assert(x,y,z)
  39.     local fuelCost = 0
  40.     local timeCost = 0
  41.     --time to get from x,y,z to instructions[nObjective]
  42.     local x0,y0,z0 = unpack(instructions[nObjective])
  43.     local addTime0,addFuel0 = getCost(x,y,z,x0,y0,z0)
  44.     fuelCost = addFuel0 + fuelCost
  45.     timeCost = addTime0 + timeCost + 1
  46.    
  47.     for n = nObjective,#instructions-1 do
  48.         --time to get from instructions[nObjective] to instructions[nObjective+1]
  49.         local x1,y1,z1 = unpack(instructions[n],1,3)
  50.         local x2,y2,z2 = unpack(instructions[n+1],1,3)
  51.         local addTime,addFuel = getCost(x1,y1,z1,x2,y2,z2)
  52.         local placeCost = 1
  53.         fuelCost = fuelCost + addFuel
  54.         timeCost = timeCost + addTime + placeCost
  55.     end
  56.     return fuelCost,timeCost*0.4
  57. end
  58.  
  59. --local fuel,time = time_estimate(1,instructions,0,0,0)
  60. --print(fuel," ",formatTime(time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement