Enter your name via a D-pad The puzzle:
Consider a console/hand-held game with a d-pad where you are required to enter a name of sorts. This appeared in many older games before the use of QWERTY was popularised in consoles (e.g. I believe the Wii uses a QWERTY keyboard layout for input). Typically, the on-screen keyboard looks to the effect of*:
Default:
0 1 2 3 4 5 6 7 8 9
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 _ + ^ =

With the case switched:
0 1 2 3 4 5 6 7 8 9
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 - + ^ =

That is, all alphanumeric keys and the following:
_: A single space
-: A hyphen
+: Switch case for the next letter only
^: Toggle caps lock (that is, switch the case of all letters)
=: Enter, complete
*Obviously I replaced keys like &quot;BKSP&quot; and &quot;ENTER&quot; with shorter versions
And then the hardware would include a d-pad (or some form of control where you could go up, down, left and right)
The screen also typically let you move from one side directly to the other. That is, if you were focussed on the letter J, pressing right would allow you to move to the letter A.
Whenever I was entering my name, I'd always try to work out the quickest way to do so.
Goal:
Your program will take string input which may include any alphanumeric character including a space and hyphen, and your goal is to output the shortest amount of key presses on the d-pad to output the required string.
Considerations:
You do not need to include the key pressed for pressing the actual character.
Focus always starts at the A
Enter = must be pressed at the end
Example:
input: Code Golf
output: 43
Explained:
A -&gt; C = 2
C -&gt; ^ = 6 (moving to the left)
^ -&gt; o = 5
o -&gt; d = 2
d -&gt; e = 1
e -&gt; + = 5
+ -&gt; _ = 1
_ -&gt; + = 1
+ -&gt; G = 3
G -&gt; o = 3
o -&gt; l = 3
l -&gt; f = 5
f -&gt; = = 6
Note that it is quicker to hit the + twice for a _ and a G than it is to hit ^ once, then swap back.
The winning submission (I'll allow at least 1w) will be the shortest solution (in bytes). As this is my first question, I hope this is clear and not too hard.
Swift 1.2, 812 588 670 bytes

Edit: Removed 224 bytes by replacing the large arrays of numbers with a Range and converting it to an Array instead.

Edit2: Added looping vertically

typealias S=String
typealias I=Int
var A:(I)-&gt;S={S(UnicodeScalar($0))},B:(I)-&gt;(I,I)={a in(a%10,a/10)},a=Array(48...57).map{A($0)},b=[a+(Array(65...90)+[32,43,94,61]).map{A($0)},a+(Array(97...122)+[45,43,94,61]).map{A($0)}],z=Process.arguments
z.removeAtIndex(0)
func C(e:I,f:I)-&gt;I{let(a,b)=B(e),(c,d)=B(f)
return min(abs(d-b), abs(4-(d-b)))+min(abs(c-a),abs(10-(c-a)))}
func D(c:S,_ e:I=10,_ f:Bool=false,_ g:Bool=false)-&gt;I{if count(c)==0{return C(e,39)}
let h=c.startIndex,i=c.endIndex,j=S(c[h])
if let k=find(b[f ?1:0],j){return C(e,k)+D(c[advance(h,1)..&lt;i],k,(g ?(!f):f),false)}else{return min(C(e,37)+D(c,37,!f,true),C(e,38)+D(c,38,!f,false))}}
print(D(" ".join(z)))


To run, put the code in a .swift file and run it with swift &lt;filename&gt; &lt;your name&gt;



This uses the simple approach where the two 'keyboards' are stored as arrays.

B:(I)-&gt;(I,I)={a in(a%10,a/10)}
Converts an index from the array to an x,y position on the virtual keyboard.

func C(e:I,f:I)-&gt;I{let(a,b)=B(e),(c,d)=B(f)
 return abs(d-b)+min(abs(c-a),abs(10-(c-a)))} Takes a start/end index and returns the minimum number of moves to get from once to the other (accounting for horizontal wrap)

func D(c:S,_ e:I=10,_ f:Bool=false,_ g:Bool=false)-&gt;I Is the main recursive function that does most of the calculations.  It calculates the distance from the current position to the target character, unless the case should change, then it calculates both the shift and the caps lock methods and takes the smallest.

Running swift codegolf.swift Code Golf prints 43