| Bytes | Lang | Time | Link |
|---|---|---|---|
| 145 | R | 250720T164052Z | Kirill L |
| 211 | Python3 | 250718T162805Z | Ajax1234 |
| 049 | Pyth | 150612T151031Z | izzyg |
| 062 | CJam | 150611T220256Z | Dennis |
R, 145 bytes
\(s,`!`=utf8ToInt,w=which(outer(!"qwertyuiopasdfghjkl zxcvbnm",!tolower(s),`==`),T)[,1]-1,x=diff(w%/%10+w%%10*1i))sum(rle(Arg(x)[Mod(x)>0])$l>0)
Commented
\(s,`!`=utf8ToInt) {
w=which(
# Which positions on the keyboard match lowercased input chars
outer(!"qwertyuiopasdfghjkl zxcvbnm", !tolower(s), `==`)
, arr.ind=T # Output as matrix coordinates
)[,1]-1 # Convert to 0-indexed format
# Encode each key's row and column as complex numbers
d=diff(w%/%10+w%%10*1i) # and take the diffs
# A swype is formed by diffs pointing to the same direction Arg(d)
# Filter out repeating letters with zero distance Mod(d)==0
# Take the Run Length Encoding and count the runs
sum(rle(Arg(d)[Mod(d)>0])$l>0)
}
Python3, 211 bytes
M=lambda x:[1,-1][x<0]*(abs(x)>0)
def f(w,b):
w=[i.lower()for i in w if not i.isdigit()]
(x,y),X,Y,c=b[w[0]],0,0,0
for i in w[1:]:j,k=b[i];c+=(X,Y)!=(T:=(M(x-j),M(y-k)))and(x,y)!=(j,k);x,y=j,k;X,Y=T
return c
Pyth, 53 50 49 bytes
lrPMfT-VJm.jF.D@jC"XÖ;¿ìÇ×;¤ð$ _"28CdT@GrzZtJ8
Keyboard compression format thanks to @Dennis.
This answer contains some unprintable characters. See the links below for the correct code.
Explanation:
lrPMfT-VJm.jF.D@jC"..."28CdT@GrzZtJ8
rzZ Convert input to lowercase.
@G Take the intersection with G.
This removes the numbers.
m Map over the characters
C"..." Treat the string as a base 256 number.
j 28 Convert this number to base 28, giving:
[15, 7, 16, 17, 18, 27, 26, ... ]
which is the character positions
from top left, rotated by 97 places.
@ Cd Index this list by ord(character)
.D T divmod by 10, giving the x-y position.
.jF Convert this to a complex number.
J Save the result to J.
-VJ tJ Vectorize - over J and J[1:]. This gives
pairwise diffeences between J's elements.
fT Filter the differences on being truthy.
This removes letter pairs with no movement.
PM Map the remaining complex numbers to their
phases, which indicates their directions.
r 8 Run-length encode the result.
l Print out the number of separate RLE groups.
CJam, 78 76 73 68 62 bytes
relA,s-:_2ew{:-},{{i"x8LÑejPG ÀÏi"225b28b=A+Ab}/.-:ma}%e`,
Note that the code contains unprintable characters.
Borrowing @isaacg's clever idea of using RLE to count the paths saved 6 bytes.
Try it online in the CJam interpreter. If the link doesn't work, copy the code from this paste.
How it works
rel e# Read a token from STDIN and cast it to lowercase.
A,s- e# Remove all digits.
:_ e# Duplicate each element of the argument (string/array).
2ew e# Push all overlapping slices of length 2.
{:-}, e# Filter out slices where both elements are equal.
{ e# For each slice:
{ e# For each character of the slice:
i e# Push its code point.
"x8LÑejPG ÀÏi"225b28b
e# Convert the string from base 225 to base 28, pushing the array
e# [15 7 16 17 18 27 26 8 9 0 3 11 4 6 24 1 22 5 21 10 25 23 12 2 13 14].
e# These are the positions on the QWERTY keyboard, starting from the
e# upper left corner and going right.
= e# Select the element that corresponds to the code point.
e# Arrays wrap around in CJam. Since 'a' has code point 97, the array has
e# length 26 and 97 % 26 == 19, 'a' corresponds to the 20th element.
A+Ab e# Add 10 and convert to base 10. Example: 8 -> [1 8]
}/ e#
.- e# Vectorized subtraction. [a b] [c d] -> [a-c b-d]
:ma e# atan2. Pushes the angle of the direction. [y x] -> arctan(y/x)
}% e#
e` e# Apply run-length encoding.
, e# Count the runs.