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

19

3

enter image description here

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]);
  }
  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>

Related:

geokavel

Posted 2015-12-27T02:11:06.560

Reputation: 6 352

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.577

5Any 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.370

1Are 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

Answers

5

Pyth, score 107 106 102

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

Try it online.

PurkkaKoodari

Posted 2015-12-27T02:11:06.560

Reputation: 16 699

2Your score has gone down by 2! – geokavel – 2015-12-27T18:29:42.107

Cngrt vry gd job. – geokavel – 2016-01-05T01:00:41.503

33

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

Downgoat

Posted 2015-12-27T02:11:06.560

Reputation: 27 116

3Lol, i dont get how this works, but this is going to be invalid soon. – geokavel – 2015-12-27T02:50:19.140

1@geokavel aww :( but not invalid, it would just have a very high score – Downgoat – 2015-12-27T02:51:26.287

Yes, ill give you an upvote. – geokavel – 2015-12-27T02:52:09.267

3 minutes? You need a better converter. :P

– Dennis – 2015-12-27T02:56:48.623

I think this is valid? how is it not valid? – djechlin – 2015-12-27T07:43:22.570

Sorry to rain on your parade (I really like this answer), but look at the scoring section. – PyRulez – 2015-12-27T13:00:13.003

2I scrolled to the end of your code block. I thought the code was 61032477390907355804...., not 61032477390907355804... zeros. :P – Rɪᴋᴇʀ – 2015-12-27T16:24:42.577

6

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

user81655

Posted 2015-12-27T02:11:06.560

Reputation: 10 181

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). – ETHproductions – 2015-12-27T16:06:57.220

2Your score is 1 point smaller now.. – geokavel – 2015-12-27T18:27:28.203

6

Japt, score 123 119 118 116 106

42 41 40 bytes + 81 78 77 75 66 distance

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

(proper output in the "output" box)

nicael

Posted 2015-12-27T02:11:06.560

Reputation: 4 585

1Your score stays the same. – geokavel – 2015-12-27T18:31:51.777

@geo already recalced :) – nicael – 2015-12-27T18:32:10.080

@geo Btw, you can edit the scores right away. – nicael – 2015-12-27T18:33:21.033

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. – ETHproductions – 2015-12-27T19:00:28.667

@Eth Great, thanks! As for the line break, don't think I'm a noob in html ;D – nicael – 2015-12-27T19:16:58.080

@Eth Look, the shortest there now :) – nicael – 2015-12-27T20:38:53.867

Woah, awesome! Nice job optimizing for score rather than byte count. :) – ETHproductions – 2015-12-27T21:06:26.053

5

Bash + Sed, 151

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

Alexander Vogt

Posted 2015-12-27T02:11:06.560

Reputation: 211

Congrats, your score is 1 point lower with the new counting fix. – geokavel – 2015-12-27T18:28:54.843

1@geokavel Thanks - fixed. – Alexander Vogt – 2015-12-27T18:33:28.637

5

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?

Rɪᴋᴇʀ

Posted 2015-12-27T02:11:06.560

Reputation: 7 410

3Your score is 1 lower now. – geokavel – 2015-12-27T18:30:38.680

Ooh, cool. Thanks @geokavel. What changed in the rules? – Rɪᴋᴇʀ – 2015-12-27T22:54:03.570

Not rules, there was a bug in the calculator – geokavel – 2015-12-27T22:55:02.200

This prints one too many spaces in the start of the third line. – PurkkaKoodari – 2015-12-28T12:54:38.513

You have crushed my hopes with your true words. But thanks for pointing that out. – Rɪᴋᴇʀ – 2015-12-28T15:13:07.023

5

JavaScript, score 156 187

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

Not bad for JavaScript

Try it online


With alert, score 186

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

Downgoat

Posted 2015-12-27T02:11:06.560

Reputation: 27 116

But it doesn't print the string, no? – nicael – 2015-12-27T17:09:22.463

@nicael I can claim I'm using this environment which has implicit printing/output.

– Downgoat – 2015-12-27T17:10:35.143

1Score is 1 lower now. – geokavel – 2015-12-27T18:31:40.513

@geokavel thanks fixed – Downgoat – 2015-12-27T18:41:25.210

3

Inventing your own environment to get around output, hmm? Anyways, it's possible to to shorten this by a byte.

– ETHproductions – 2015-12-27T19:03:59.280

Oops, I guess that's 2 points more... Huh :/ Nice job minimizing the score! – ETHproductions – 2015-12-27T19:05:21.080

4

Bash + sed, score 202 200

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

user2064000

Posted 2015-12-27T02:11:06.560

Reputation: 270

1Your score went down by 2! – geokavel – 2015-12-27T18:32:52.577

@geokavel, thanks, and it's fixed now. – user2064000 – 2015-12-28T08:55:24.340

4

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

Conor O'Brien

Posted 2015-12-27T02:11:06.560

Reputation: 36 228

0

Python, score 185

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

JuanPotato

Posted 2015-12-27T02:11:06.560

Reputation: 399