19
3
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>
9we should make another challenge: shortest code to score answers like this. I think the scoring method is more interesting than the question. – Cyoce – 2015-12-27T02:19:27.147
2Also, please for the love of god use
===
unless for some reason you want JS to sneakily coerce your types and suck up performance. – Cyoce – 2015-12-27T02:22:30.5775Any submission in Unary/Lenguage will have a score of 0. – Dennis – 2015-12-27T02:23:19.427
1@Dennis Is it possible to answer this in those languages? I can put a cap on the max program length. – geokavel – 2015-12-27T02:27:33.117
Sure it is. I've done it in a few related challenges. – Dennis – 2015-12-27T02:30:22.313
@Cyoce Yes, i was thinking about another challenge for the scoring. Also, just looked at the SO post with 3,700 score relating to what you mentioned. – geokavel – 2015-12-27T02:42:29.923
And for the scoring, how about a minimum score of 1 per character? That should make unary et al. not nearly as viable. – Cyoce – 2015-12-27T02:44:18.727
@Cyoce sounds good. – geokavel – 2015-12-27T02:46:45.630
1The snippet says
\
]from my answer is 3 keys away, but according to the picture it can be done in 2 with
`` -><backspace>
->]
. – user81655 – 2015-12-27T07:44:15.3701Are trailing spaces allowed? – PurkkaKoodari – 2015-12-27T12:28:43.203
@Pietu Theyre allowed as long as the output appears the same – geokavel – 2015-12-27T17:49:38.063
@user81655 Fixed. – geokavel – 2015-12-27T18:38:30.680
1I think it's really not cool to change the rules to eliminate a very smart answer because you didn't like that answer. – djechlin – 2015-12-27T23:47:25.407
@djechlin That's not what happened. We were talking about changing the rule from the very beginning, even before the answer was posted. – geokavel – 2015-12-27T23:48:56.777
3idk I think it's really not in the spirit of things to eliminate a creative, valid language on the ground that it... works? does the thing the language is good at? This doesn't seem any different from eliminate CJam or Pyth because their answers are just a bit too short or eliminating Retina for text-matching questions, and yeah it struck me as weird that the top answer became invalid sometime after being posted despite nailing the challenge. I really enjoy sharing answers like this when the language is really shown off. – djechlin – 2015-12-27T23:56:25.013
@djechlin i told him right away the rule would change, and he was ok with with it. Maybe he didnt change the score for a while. – geokavel – 2015-12-28T00:01:21.680
1I understand that, I still don't think you should add on rules explicitly designed to make clever solutions invalid. – djechlin – 2015-12-28T00:06:34.450