32
2
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 "BKSP" and "ENTER" 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
-> C
= 2
C
-> ^
= 6 (moving to the left)
^
-> o
= 5
o
-> d
= 2
d
-> e
= 1
e
-> +
= 5
+
-> _
= 1
_
-> +
= 1
+
-> G
= 3
G
-> o
= 3
o
-> l
= 3
l
-> f
= 5
f
-> =
= 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.
12Nice challenge! Just one point, 48 hours is probably too little. That's how long it takes for bounties to be allowed, so it should be more around a week+. – Maltysen – 2015-07-26T03:20:37.120
@Maltysen thanks for the suggestion, I've updated the challenge – Tas – 2015-07-26T03:44:32.747
1Can you wrap vertically, too, or just horizontally? – Alex Reinking – 2015-07-31T23:49:47.950
2@AlexReinking that's a great point! Yes you can. – Tas – 2015-08-01T01:45:05.430
Great! My implementation does that, so I just wanted to double-check. – Alex Reinking – 2015-08-01T01:54:01.557
For those testing the hyphen:
A-Z
is 11 – Alex Reinking – 2015-08-01T02:02:03.980Sorry, but why did the bounty go to the Swift submission? Mine was 300 bytes shorter. – Alex Reinking – 2015-08-07T00:01:35.067
The problem of "finding the shortest path" is a good candidate for being solved with state space search, eg "A Star search" algorithm. Hipster4j is a great Java library for this. – gahrae – 2015-08-09T20:46:05.437
Does
+
really switch the case for the next letter, even if you’re in Caps Lock mode? So I could type “STEvEN” by going “STE+vEN”? – Timwi – 2015-10-11T12:44:54.570@Timwi AFAIK that is correct. – J Atkin – 2015-10-14T15:45:00.830