5
\$\begingroup\$

This question already has an answer here:

I think about if I was turned into an ant, I'd want my loved ones to know, so I thought of a plan- I'd gather twigs and leaves and write my name on the ground, Then I'd wait for my family and friends to crowd round But now I'm thinkin'- that when they saw it They'd think the human me wrote it and an ant was crawlin on it Then not realizing' the ant-me was really the real-me In a strange twist of fate I think they'd probably kill me.

--Thoughtful Guy, Rhett and Link

Your worst nightmare has come true. You have been turned into an ant. You have to write a message to your family to tell them this horrible news. But, gathering twigs and leaves is so old-school. Let's write it on a computer! If you climb up on your families "QWERTY" keyboard, you're just strong enough to push down the key you're standing on. However, it takes a lot of energy to jump from key to key, so you want to know how hard it will be to write out a message. For reference, here is the US standard for the QWERTY keyboard:

enter image description here

Your message will be entirely made up of letters and space so all punctuation and key-modifiers (shift, system keys, control, tab, enter, numbers, etc.) are invalid. Ignore case completely. You need to determine what the "Ant-Difficulty" of a given message is before you start typing it. The "Ant-Difficulty" is how many times you (as an ant) have to jump from one key to another key. Ant-Difficulty does not include the effort required to push a key down once you get to that key. Since you are a tiny creature, you want write a program or function that is as small as possible to determine the ant difficulty of a string.

Input and output can be in any reasonable format.

Clarifications

For the first row (QWERTYUIOP) you can only jump straight down. Q to A (or A to Q) is one jump, W to S is one jump, and so on up until O to L. From P, you have to first jump to O before you can go down a row.

Since the third row (ZXCVBNM) is slightly off-kilter, you can jump from second to third and to the side at the same time. So A only goes to Z, S goes to Z and X, D goes to X and C, etc. K only goes to M, and L goes to nothing on the third row. Everything on the third row except for Z goes to the spacebar.

This brings up a possible shortcut. To go from X to M, you could jump to C, then V, then B, then N, then M, which is 5 jumps, or you could jump to the spacebar, walk across it, then jump to M, which is 2 jumps. The same works on the second and third row. S to K can be S-D-F-G-H-J-K, which is 6 jumps, or S-X-Space-M-K, which is 4.

Move List:

Here is a list of all possible jumps, each counting as 1.

Q: AW
W: QSE
E: WDR
R: EFT
T: RGY
Y: THU
U: YJI
I: UKO
O: ILP
P: O
A: QWSZ
S: WADXZ
D: ESFXC 
F: RDGCV
G: TFHVB
H: YGJBN
J: UHKNM
K: IJLM
L: KO
Z: AX
X: SDZC <SpaceBar>
C: DFXV <SpaceBar>
V: FGCB <SpaceBar>
B: GHVN <SpaceBar>
N: HJBM <SpaceBar>
M: JKN <SpaceBar>
<SpaceBar>: XCVBNM

Test I/O

Message  --> Difficulty
hello    --> 12 (H to E is G-F-D-E for 4, E to L is E-R-T-Y-U-I-O-L for 7, 
                L is 0 since we're already on L, and L to O is 1)
world    --> 22 (W to O is W-S-X-Space-M-K-O for 6, O to R is O-I-U-Y-T-R for 5, 
                R to L is R-T-Y-U-I-O-L for 6, and L to D is L-K-M-Space-C-D for 5)
ant      --> 7 (A-S-X-Space-N is 4, plus N-H-Y-T is 3)
ant-difficulty --> Undefined behavior. ('-' is not a valid character.)
CaSe     --> 6 (C-D-S-A is 3, A-S is 1, S-D-E is 2)
a fad    --> 10 (A-S-X-Space is 3, Space-C-F is 2, F-D-S-A is 3, A-S-D is 2.)

The shortest answer in bytes gets the checkmark.

\$\endgroup\$

marked as duplicate by Addison Crump, Geobits, ETHproductions, Doorknob code-golf Feb 29 '16 at 1:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.