Tool Changer
Problem statement
A milling machine has a circular tool changer containing n tools. A tool name may appear more than once.
The operator can move through the changer one position at a time, either left or right. The changer is circular, so moving past one end wraps around to the other end.
You are given:
tools, the tool names in circular order;startIndex, the index of the tool currently in use; andtarget, the required tool name.
Return the minimum number of one-position moves needed to reach any occurrence of target from startIndex.
Function
toolchanger(tools: String[], startIndex: int, target: String) → intExamples
Example 1
tools = ["ballendmill", "keywaycutter", "slotdrill", "facemill"]startIndex = 1target = "ballendmill"return = 1The current tool is keywaycutter at index 1. The target ballendmill is at index 0. Moving left takes 1 step, while moving right takes 3 steps, so the answer is 1.
Example 2
tools = ["ballendmill", "facemill", "keywaycutter", "slotdrill"]startIndex = 1target = "slotdrill"return = 2The current tool is facemill at index 1. The target slotdrill is at index 3. Moving left or right takes 2 steps, so the answer is 2.
Constraints
1 <= tools.length <= 1000 <= startIndex < tools.length1 <= tools[i].length, target.length <= 100targetappears at least once intools.