# Print the QWERTY keyboard using keys that are as close together as possible

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:

1. 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.
2. Unfortunately, if your code requires symbols that are not on the keyboard you cannot use it.
3. On the keyboard image, the top row can be ignored. The only key you can use on the bottom row is Space
4. 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]);
}
}
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>

### Related:

• we should make another challenge: shortest code to score answers like this. I think the scoring method is more interesting than the question. Dec 27 '15 at 2:19
• Also, please for the love of god use === unless for some reason you want JS to sneakily coerce your types and suck up performance. Dec 27 '15 at 2:22
• Any submission in Unary/Lenguage will have a score of 0. Dec 27 '15 at 2:23
• @Dennis Is it possible to answer this in those languages? I can put a cap on the max program length. Dec 27 '15 at 2:27
• idk 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. Dec 27 '15 at 23:56

# Unary, score ~6.1*10618

6103247739090735580402225797524292167653462388595033897325606983093527722629493568418069722646005695215642120674994001348606253869287599178270707482456199630901069511698694317195626565008736452130034232375778047932461822258369348260249011643486476832847755830117284465136723525376668555270734061914837886192012601522703308221225195058283657800958507281265116257152529161080096092081620384043514820427911786442536988705847468796481108000358361636640985892696216392434604543586511103835032034494033598102606339253132146827455065586119645920456668064941286708686113567081095434338440184737976711767750474398662381256908308 zeros

Not the most "creative" solution but it took my computer ~3 minutes to convert the base 2 representation of this to base 10

This used to have a score of 0, but the scoring rules changed.

Code Length: ~6.1*10618

Key Distance: 0

• Lol, i dont get how this works, but this is going to be invalid soon. Dec 27 '15 at 2:50
• @geokavel aww :( but not invalid, it would just have a very high score Dec 27 '15 at 2:51
• Yes, ill give you an upvote. Dec 27 '15 at 2:52
• 3 minutes? You need a better converter. :P Dec 27 '15 at 2:56
• I scrolled to the end of your code block. I thought the code was 61032477390907355804...., not 61032477390907355804... zeros. :P Dec 27 '15 at 16:24

# Japt, score 123119118116 106

42 41 40 bytes + 81 78 77 75 66 distance

"QWERTYUIOP
ASDFGHJKL
'ZXCVBNM"q qS r''"


(proper output in the "output" box)

• Your score stays the same. Dec 27 '15 at 18:31
• @geo already recalced :) Dec 27 '15 at 18:32
• @geo Btw, you can edit the scores right away. Dec 27 '15 at 18:33
• Nice again! You could do "QWERTYUIOP<br>ASDFGHJKL<br>ZXCVBNM"¬¸r'Z" Z (<br> represents a line break), but that requires two non-keyboard chars, so I guess it isn't legal. But at least you can remove the comma, as it is automatically inserted. Dec 27 '15 at 19:00
• @Eth Great, thanks! As for the line break, don't think I'm a noob in html ;D Dec 27 '15 at 19:16

# JavaScript (ES6), score 188

alert([...QWERTYUIOP
ASDFGHJKL
," Z",..."XCVBNM"].join )


Only just barely gets a better score than alerting the output string but it's the best approach I could find... : /

Bytes: 60

Key Distance: 128

• I tried a modified version of this apporach: alert(QWERTYUIOP<br>ASDFGHJKL<br> ZXCVBNM.match(/\n| ?./g).join ) While this works, it's 65 bytes (score 231). Dec 27 '15 at 16:06
• Your score is 1 point smaller now.. Dec 27 '15 at 18:27

# Pyth, score 107106 102

:jd"QWERTYUIOP
ASDFGHJKL
]XCVBNM""]"" Z


Try it online.

• Your score has gone down by 2! Dec 27 '15 at 18:29
• Cngrt vry gd job. Jan 5 '16 at 1:00

# Bash + Sed, 151

sed 'sb *.b& bg'<<<'QWERTYUIOP
ASDFGHJKL
ZXCVBNM'

• Congrats, your score is 1 point lower with the new counting fix. Dec 27 '15 at 18:28
• @geokavel Thanks - fixed. Dec 27 '15 at 18:33

# Python, 157, 156, 211, 221 points

Key Distance: 157

Bytes: 64

Ergo, total score is 221.

l=' '.join('QWERTYUIOP\nASDFGHJKL\nZXCVBNM')
print l[:42],l[42:]


Prints the string but has to add an extra space. :( now longer.

Why @Pietu, why did you do this to me?

• Your score is 1 lower now. Dec 27 '15 at 18:30
• Ooh, cool. Thanks @geokavel. What changed in the rules? Dec 27 '15 at 22:54
• Not rules, there was a bug in the calculator Dec 27 '15 at 22:55
• This prints one too many spaces in the start of the third line. Dec 28 '15 at 12:54
• You have crushed my hopes with your true words. But thanks for pointing that out. Dec 28 '15 at 15:13

# JavaScript, score 156 187

[...QWERTYUIOP
ASDFGHJKL].join +
Z X C V B N M


Try it online

alert([...QWERTYUIOP
ASDFGHJKL].join +
Z X C V B N M)

• But it doesn't print the string, no? Dec 27 '15 at 17:09
• @nicael I can claim I'm using this environment which has implicit printing/output. Dec 27 '15 at 17:10
• Score is 1 lower now. Dec 27 '15 at 18:31
• @geokavel thanks fixed Dec 27 '15 at 18:41
• Inventing your own environment to get around output, hmm? Anyways, it's possible to to shorten this by a byte. Dec 27 '15 at 19:03

# Jolf, 118 + 51 = 169

Try it here! (underscores in explanation used to denote a used space)

R m{"QWERTYUIOP'ASDFGHJKL'ZXCVBNM"#DN+*S' RH' }"\n"
_m                                                 map
{"QWERTYUIOP'ASDFGHJKL'ZXCVBNM"#                  that array
DN         }      with this function
+*S'_            that concats (index many) spaces
RH'_         with the inner array joined by spaces
R                                                    and join that
"\n"  with newlines


# Jolf, update post-question, 76 + 21 = 97

Try it here! Again, I don't often update my code until relevant. Still fun.

R mpHDN+*S' RH' }"\n"
_m                   map
pH                 the keyboard array [["Q","W",...,"P"],["A",...,"L"],["Z",...,"M"]]
DN         }      with this function
+*S'_            that concats (index many) spaces
RH'_         with the inner array joined by spaces
R                     and join that
"\n"  with newlines


# Bash + sed, score 202 200

sed 's/$$[^ ]$$/\1 /g'<<F
QWERTYUIOP
ASDFGHJKL
ZXCVBNM
F

• Your score went down by 2! Dec 27 '15 at 18:32
• @geokavel, thanks, and it's fixed now. Dec 28 '15 at 8:55

# Python, score 185

print" ".join("QWERTYUIOP\nASDFGHJKL\n")+"  Z X C V B N M"