Normally, challenges are scored in bytes, or sometimes Levenshtein distance, but for this one we're using keyboard distance -- the number of keys between the keys used to type the program (use the above keyboard as the definitive reference). For example, the distance between A and F is 3, because the path is A=>S=>D=>F. The distance between N and 5 is 4, because no matter what path you take, it requires at least 4 steps. Your task is to output the following (not including trailing spaces), with as small a keyboard distance as possible:
Q W E R T Y U I O P
A S D F G H J K L
Z X C V B N M
Wrapping:
To make your life easier, certain keys can wrap around the keyboard. Left Shift wraps to Right Shift, Caps Lock wraps to Enter, Tab wraps to \ and ~ wraps to Backspace. For example, the distance between Q and P is 5, because Q=>Tab=>\=>]=>[=>P.
Note: Wrapping only works horizontally -- you can not step from, say, \ to Caps Lock
Scoring:
Score = Keyboard distance + byte count
Example Calculation:
print(5);
- p=>r == 6
- r=>i == 4
- i=>n == 2
- n=>t == 3
- t=>( == 4
- (=>5 == 4
- 5=>) == 5
- )=>; == 2
Total: 30 + 9 = 39.
Notes:
- Lowercase and uppercase letters count as the same key. If a key has two symbols on it (like 7 and &), they also count as the same key, no need to include pushing shift.
- Unfortunately, if your code requires symbols that are not on the keyboard you cannot use it.
- On the keyboard image, the top row can be ignored. The only key you can use on the bottom row is Space
- Keys must be inputted in order, you can't use the arrow keys to move the caret and then input a key.
Score Calculator:
- Updated on 12/27 to fix `=>] and related miscalculations. Check your scores again, and they will likely be smaller (hopefully not bigger!)
Paste in your code here to calculate the score. Let me know if you ever get an error or it prints out the wrong number.
var keys = ["~1234567890-=←","\tqwertyuiop[]\\","↑asdfghjkl;\'\n","Lzxcvbnm,./R",
"AB CDEF"];
var con =["`!@#$%^&*()_+{}|:\"<>?","~1234567890-=[]\\;\',./"];
function hexagon(k) {
if(k === " ") return ["x","c","v","b","n","m",","];
var p = pos(k);
if(p === -1) return false;
var row = p[0],col = p[1];
var hexagon = [char(row,col-1,1),char(row-1,col),char(row-1,col+1),char(row,col+1,1),char(row+1,col),char(row+1,col-1)];
return hexagon;
}
function char(r,c,wrap) {
if(r < 0 || r >= keys.length) return "";
if(r === keys.length-1 && 1 < c && c < 8) return " ";
if(wrap) {
if(c === -1) c = keys[r].length-1;
if(c === keys[r].length) c = 0;
}
return keys[r].charAt(c);
}
function pos(c) {
var row = -1, col = -1;
for(var i = 0;i<keys.length;i++) {
col = keys[i].indexOf(c)
if( col != -1) { row = i; break;}
}
if(row === -1) return -1;
return [row,col];
}
function dist(a,b,s,w) {
if(typeof a === "object") {
var list = [];
for(var i = 0;i<a.length;i++) {
list[i] = dist(a[i],b,s,w);
}
return list;
}
if(a==="") return Infinity;
if(a===b) return 0;
var p = pos(a);
var q = pos(b);
if(!w && a!==" ") {
var chars = keys[p[0]].length;
var opp = char(p[0],p[1] < chars/2 ? chars-1 : 0);
return Math.min(dist(a,b,s,true),dist(a,opp,s,true)+dist(opp,b,s,true));
}
if(!s) { return Math.min(dist(a,b,true,w),dist(a," ",true,w)+dist(" ",b,true,w));}
var h = hexagon(a);
if(a === " ") return 1 + Math.min(...dist(h,b,true,w));
if(p[0]<q[0]) {
return 1 + Math.min(dist(h[4],b,s,w),dist(h[5],b,s,w));
}
else if(p[0] > q[0]) {
return 1 + Math.min(dist(h[1],b,s,w),dist(h[2],b,s,w));
}
if(b===" ") return Math.min(Math.abs(p[1]-7),Math.abs(2 - p[1]));
var d = Math.abs(p[1]-q[1]);
return Math.min(d,keys[p[0]].length-d);
}
function getTotalDistance(str) {
for(var i = 0;i<con[0].length;i++)
str = str.replace(new RegExp("\\"+con[0].charAt(i),"g"),con[1].charAt(i));
str = str.toLowerCase();
var total = 0;
for(var i = 0;i<str.length-1;i++) {
total += dist(str[i],str[i+1]);
}
return total;
}
enter.onclick = function() {
var a = getTotalDistance(program.value);
var b = program.value.length;
len.textContent = a;
count.textContent = b;
total.textContent = a+b;
};
<textarea rows=15 cols=40 id="program"></textarea>
<input type="submit" id="enter"/>
<div>
<div>Key distance: <span id="len"></span></div>
<div>Byte count: <span id="count"></span></div>
<div>Total: <span id="total"></span></div>
</div>
===
unless for some reason you want JS to sneakily coerce your types and suck up performance. \$\endgroup\$