A certain well-known cable company has a somewhat clumsy 'Search for Program' feature that works through the remote. It takes muchly much button pushing; so, being an admirer of economy of motion, I thought I'd seek out a programmer's help to minimize the number of finger movements I have to make.
The Craptastic Search Feature features a layout of selectable cells, 1 row of 3 cells followed by 6 rows of 6 cells, that looks like this:
del spa sav
A B C D E F
G H I J K L
M N O P Q R
S T U V W X
Y Z 0 1 2 3
4 5 6 7 8 9
There are cells for each of the letters A-Z
, and spa
which is used to add a space to the search string. del
is supposed to be used to delete a character; and sav
is meant to be used to save the search string. We'll ignore these actual functions for this challenge; but the cells are otherwise still selectable for our purposes.
We start with an empty search string and the A
cell selected; and we use left, right, up and down arrow buttons to change the selected cell. When the central 'OK' key is pushed, the character in the selected cell is added to the search string. For convenience, we'll use <
,>
,^
,v
and _
for left,right,up,down and OK, respectively.
For the letters and numbers, the action of the directional buttons is straightforward. There is no 'wrap-around'; for example, if the current cell is G
, then <
has no effect.
So to enter the search string BIG
, we could use the sequence
>_>v_<<_
(the initial >
is required to move us from the default starting cell A
to the cell B
). Alternatively, we could of course instead use >_v>_<<_
; but note there is no shorter sequence than 8 keys that can do the job.
Now since the top row has only three cells, the action there is slightly different and complicates things a little:
First, if the selected cell is in the top letter row A-F
, the ^
key moves the selected cell directly above; so A,B
go to del
, C,D
go to spa
and E,F
go to sav
.
One the other hand, if the selected cell is 'del', the v
key moves the selected cell to A
, and the >
key makes the selected cell spa
. Similarly v
key moves the selected cell from spa
to C
, and sav
to E
.
This means that for example, if you are currently at the B
cell, the sequence ^v
does not return you to the B
cell; instead it takes you to the A
cell.
And starting at the A
cell, the sequence v>>^
moves us to the C
cell; while the sequence ^>>v
moves us to the the E
cell.
The Challenge
Given a TV show or movie title s
, consisting solely of characters in A-Z
, 0-9
and space
, your program/function should output one of the sequences of keypresses of minimal length to enter s
as the search string from the starting state in Craptastic fashion.
Your output should be a string or a list of distinct values representing a sequence of up, down, right, left, and OK; so you are not restricted to the charset <>^v_
(e.g., udrl*
or a list with integer elements 0,1,2,3,4 would be acceptable alternatives, so long as you articulate what your schema is).
This is code-golf; the usual loophole taboos apply. May the odds be ever in your favor for each language.
Test Cases
Below are inputs and an example acceptable answer (other different sequences will be correct as well, but must be of at most the length of the examples provided).
(I re-post the keypad here so one can more easily play along visually, if that is one's wont...)
del spa sav
A B C D E F
G H I J K L
M N O P Q R
S T U V W X
Y Z 0 1 2 3
4 5 6 7 8 9
BIG >_>v_<<_
THE OFFICE >vvv_^^_>>>^_^<_vvv_>>>^^__<<<v_^_>>_
FARGO ^>>v>_^<<v_^>>v>vv_<<<<<^_>>v_
BUFFY >_>vvv_>>>^^^__^<<vvvvv_
DALLAS >>>_<<<_^>>v>v__^^<<v_vvv_
THX1138 >vvv_^^_>>>>vv_<<v__>>_<v_
ON DEMAND >>vv_<_^^^>_v>_>_<<<<vv_^^_>vv_>>^^_
NEWS RADIO >vv_>>>^^_vvv_<<<<_^^^^>_>vvv>_^^^<<v_>>>_<v_v_
ROOM 909 ^>>v>vv_<<<__<<_^^^>_>vvvvvv>_<<<^_>>>v_
FA
doesn't go straight but up and down \$\endgroup\$del
andsav
useless here? \$\endgroup\$del
andsav
have no function if 'OK' is used when they are selected; but that would waste a keypress. \$\endgroup\$