Fix the First Robot Instruction
Problem statement
A robot starts at (0,0), facing right. Moving right increases x; moving up increases y. It follows these commands:
FORWARD: move one step in the direction it faces.BACK: move one step backward without changing the direction it faces.TURN LEFT: turn left by 90 degrees without moving.TURN RIGHT: turn right by 90 degrees without moving.
The supplied sequence ends at the wrong position. Replace only its first command so that the final position equals target. Passing through the target before the sequence ends does not count.
Return the exact message Replace instruction 1 with COMMAND, replacing COMMAND with the selected command. The index is one-based. For this exercise, assume exactly one command different from the original first command works.
Function
fixFirstInstruction(instructions: String[], target: int[]) → StringExamples
Example 1
instructions = ["FORWARD","TURN RIGHT","FORWARD","TURN RIGHT","FORWARD","FORWARD","FORWARD","FORWARD"]target = [-1,4]return = "Replace instruction 1 with TURN RIGHT"The original sequence ends at (-3,-1). Replacing its first command with TURN RIGHT makes the robot face down, then the suffix moves it one step left and four steps up. It ends at (-1,4).
Example 2
instructions = ["BACK","FORWARD"]target = [2,0]return = "Replace instruction 1 with FORWARD"Replace BACK with FORWARD. The two forward steps move right from (0,0) to (2,0).
Constraints
1 <= instructions.length <= 10000target.length == 2-10000 <= target[i] <= 10000Each instruction is FORWARD, BACK, TURN LEFT, or TURN RIGHT.The original sequence misses target, and exactly one different first instruction makes its final position equal target.