diff --git a/lib/elevator/hall_orders/cost.ex b/lib/elevator/hall_orders/cost.ex index dc9cd6b..a9b91f7 100644 --- a/lib/elevator/hall_orders/cost.ex +++ b/lib/elevator/hall_orders/cost.ex @@ -100,10 +100,17 @@ defmodule Elevator.HallOrders.Cost do normalized_state = if state.direction in [:up, :down], do: state, else: %{state | direction: :down} + initial_time_ms = + if normalized_state.behavior == :door_open and elem(target, 0) != normalized_state.floor do + Elevator.door_open_duration_ms() + else + 0 + end + if target_cleared?(orders, target) do 0 else - do_simulate(orders, normalized_state, target, 0, @max_simulation_steps) + do_simulate(orders, normalized_state, target, initial_time_ms, @max_simulation_steps) end end diff --git a/test/single/cost_test.exs b/test/single/cost_test.exs index 6e680e6..02e7b2e 100644 --- a/test/single/cost_test.exs +++ b/test/single/cost_test.exs @@ -36,6 +36,13 @@ defmodule Test.Single.CostTest do @travel_duration_ms + Elevator.door_open_duration_ms() end + test "one floor away request includes current open-door delay" do + set_state(floor: 0, direction: :up, behavior: :door_open) + + assert Cost.compute_cost({1, :hall_up}, %{}) == + 2 * Elevator.door_open_duration_ms() + @travel_duration_ms + end + test "unknown floor yields unreachable cost" do set_state(floor: :unknown, direction: :down, behavior: :idle)