24
2
Disclaimer: The story told within this question is entirely fictional, and invented solely for the purpose of providing an intro.
My boss has gotten a new toy robot, and he wants me to help program it. He wants to be able to enter simple arrow instructions to get it to move. These instructions are: ^ (for move forward) < (for turn left), and > (for turn right). However, now that I've programmed the robot, he wants additional functionality. He wants me to transform any sequence of arrows he inputs, so that rather than having the robot take the path indicated, it moves to the desired location, indicated by the place it would end up if it had taken the inputted path, as efficiently as possible. I appeal to you, the members of PP&CG, to help me with this task.
Your Task:
Write a program or function to convert a string made up of arrows into a string that will get to the location indicated by the input as quickly as possible. Turning takes exactly as long as moving backwards or forwards.
Input:
A string of arrows, as indicated above. If you wish, different characters may be substituted for the arrows, but be sure to include the fact that you do so in your answer. All test cases use the arrows normally.
Output:
A string of arrows (or your equivalent characters), that take the robot to the desired destination as efficiently as possible.
Test Cases:
Note that the solutions offered are only possibilities, and that other solutions may be valid.
>^<<^^>^^ -> ^^<^
^^^^>^^^^ -> ^^^^>^^^^
>>>^^^^^^ -> <^^^^^^
>^>^>^>^ -> (empty string)
^<^^<^^<^^^^ -> >^^>^
Scoring:
The robot's memory is limited, so your program must have the lowest byte count possible.
Because in the input the robot ends up exactly where it started, so no commands are necessary to move it there as efficiently as possible. – Gryphon – 2017-08-02T16:18:30.030
Oh, misread the string. My bad. – JungHwan Min – 2017-08-02T16:18:56.867
Test case request
^<^^<^^<^^^^
->>^^>^
? – JungHwan Min – 2017-08-02T16:41:34.817@JungHwanMin, done – Gryphon – 2017-08-02T16:48:17.083
Can input use multiple characters for a single direction? i.e. -1 for < – Dave – 2017-08-02T17:38:50.720
1@pizzakingme, sorry, but my boss is very lazy and only wants to type in one character per movement. – Gryphon – 2017-08-02T18:00:00.370
There has got to be a way to get this <100. – Gryphon – 2017-08-02T21:12:21.907
Wow, this is on HNQ already. – Gryphon – 2017-08-02T23:30:16.360
So the inputs must be a single character each, but does your boss care about the characters? Perhaps 0 to turn left, 1 for straight, 2 for right? They are technically less physical buttons pressed, and therefore easier to enter for a lazy boss... – Arnold Palmer – 2017-08-03T01:35:49.673
No, my boss has a special keyboard with extra keys and no shift/caps lock so he only has to press one key per character. – Gryphon – 2017-08-03T11:57:48.883
So, he doesn't care what characters you use. – Gryphon – 2017-08-03T11:57:59.020
1I program competitive robots and I can confirm this is exactly how they work. – Joe – 2017-09-19T17:51:02.167
A little more complex perhaps, but basically the same. – Gryphon – 2017-09-19T23:34:52.597