JavaScript(ES6),84 82 79字节
由于Cyoce,节省了3个字节:
f=([d,...s],p=parseInt,v=(26+p(s[0],36)-p(d,36))%26)=>s[0]?f(s)+(v>13?26-v:v):0
说明:
f=(
[d,...s], //Destructured input, separates first char from the rest
p=parseInt, //p used as parseInt
v=(26+p(s[0],36)-p(d,36))%26 //v is the absolute value of the difference using base 36 to get number from char
)
)=>
s[0]? //If there is at least two char in the input
f(s) //sum recursive call
+ //added to
(v>13?26-v:v) //the current shortest path
: //else
0 //ends the recursion, returns 0
示例:
呼叫:f('golf')
输出:17
先前的解决方案:
82字节归功于尼尔:
f=([d,...s],v=(26+parseInt(s[0],36)-parseInt(d,36))%26)=>s[0]?f(s)+(v>13?26-v:v):0
84个字节:
f=([d,...s],v=Math.abs(parseInt(s[0],36)-parseInt(d,36)))=>s[0]?f(s)+(v>13?26-v:v):0
æ%
几天,我在阅读内置文件时碰到了这个问题,它几乎是专门为解决这一问题而设计的:OIæ%13AS