13
1
INPUT: Any string consisting exclusively of lowercase letters via function argument, command line argument, STDIN, or similar.
OUTPUT: Print or return a number that will represent the sum of the distances of the letters according to the following metric:
You take the first and second letter and count the distance between them. The distance is defined by the QWERTY keyboard layout, where every adjacent letter in the same row has distance 1 and every adjacent letter in the same column has distance 2. To measure the distance between letters that aren't adjacent, you take the shortest path between the two.
Examples:
q->w is 1 distance apart
q->e is 2 distance
q->a is 2 distance
q->s is 3 distance (q->a->s or q->w->s)
q->m is 10 distance
Then you take the second and third letter, then the third and fourth, etc., until you reach the end of the input. The output is the sum of all those distances.
Example input and output:
INPUT: qwer
OUTPUT: 3
INPUT: qsx
OUTPUT: 5
INPUT: qmq
OUTPUT: 20
INPUT: tttt
OUTPUT: 0
Here is an image showing which letters are in the same column:
This is code golf, so the shortest code in bytes wins!
1I think q->m is only 8 keys... – SuperJedi224 – 2015-06-30T12:22:44.703
2if you go down one row it counts as 2 distance, you dont count the key distance – Vajura – 2015-06-30T12:24:08.463
its up there too :) – Vajura – 2015-06-30T12:25:42.943
Can we assume that the input string will always be nonempty? – Alex A. – 2015-06-30T14:35:58.830
This is quite similar to http://codegolf.stackexchange.com/questions/50722/clarence-the-slow-typist. Except that this uses letters, while the other one used numbers.
– Reto Koradi – 2015-06-30T14:46:49.860Can we assume that there will be at least two letters? – Dennis – 2015-06-30T16:31:38.190
Yea lets say there will always be atleast 2 letters – Vajura – 2015-07-02T05:19:12.540