Well, turns out Doorknob's username on GitHub, Reddit, and other sites is KeyboardFire. That gives me an idea...

The task

You work at KeyboardFire Inc., a company that makes special keyboards. And, by "special", I mean that, whenever you press a key, something in your house lights on fire! With the new KeyboardFire Doorknob series, the objects that light on fire are doorknobs.

However, because of stupid government regulations, your users need to know which doorknobs in their house wil light on fire.

Consider this ASCII art of a portion of a QWERTY keyboard:


(The |'s represent the boundaries between keys.)

We can treat this exact ASCII drawing as a "graph" of sorts, where each character in the range [a-z0-9] has an x (horizontal) and y (vertical) index, where (0,0) is 1. For instance, the letter d has the coordinates (2,6) (pipes and spaces are included in the coordinate calculation).

Now let's think about each user's house. Every house can be drawn top-down view as a 20x4 ASCII art (in this world where it's legal to sell destructive keyboards, every house is the same size). We can use D's to mark the positions of each doorknob in the house. Here's an example:

D         D  D     D

              D  D  

We'll call this the "house map". (Yes, that's a lot of doorknobs!)

Pressing any key will light the nearest doorknob on fire. For instance, if we take the previous coordinates of the letter d, the nearest doorknob (by Manhattan distance) is at the coordinates (1,4). This is the doorknob that will light on fire when the letter d is hit. If we were to mark the flaming doorknob with an F, the result would be:

D         D  D     D

              D  D  

The specs

Your program will take two inputs:

  • A string that matches the pattern [a-z0-9]+.
  • A house map. This can be a string, a list of strings, or something equivalent.

You need to go through each letter of the string and light the respective doorknob on fire (change its letter to an F). If the nearest doorknob is already on fire, leave it as-is. If there is more than 1 doorknob that could be lit on fire using the this method, you can light whichever one you want.

After the whole string is processed in this manner, you need to print the resulting house map.

Code-golf, so shortest program wins. Standard loopholes banned as usual.




House map:

D    D       D     D

              D  D  

Possible results:

F    F       F     D

              D  F


F    D       F     D

              D  F


F    F       D     D

              F  F


F    D       D     D

              F  F

EDIT: Uh...is there a reason I have one answer, even with a +50 bounty? If you find the directions complicated/vague, I'd be glad if you posted in the comments or something...or of I'm doing anything wrong...

EDIT 2: Bounty expires in under a day! Post something else! Please! PLEASE!!!! :(

  • 1
    \$\begingroup\$ There are a few things I find confusing: 1) Why is d's coords (2, 6) and not (2, 2)? 2) Why does the example have so many possible answers? 3) When you explain how things will light on fire, why are you even talking about d? Why not just straight up say that pressing a will light some house on fire? Does d do it, too? \$\endgroup\$
    – Quelklef
    Commented Nov 23, 2015 at 2:45
  • \$\begingroup\$ @Quelklef Is this a bit better? Thanks for the feedback! \$\endgroup\$ Commented Nov 23, 2015 at 23:24
  • \$\begingroup\$ If 'h' ends up exactly between two doorknobs and 'h' is called twice, should both doorknobs be on fire? or can the program choose to fire the same doorknob? \$\endgroup\$ Commented Nov 24, 2015 at 15:59
  • \$\begingroup\$ @GrantDavis The program can choose to fire the same doorknob. \$\endgroup\$ Commented Nov 24, 2015 at 16:05

2 Answers 2


Ruby, 229 bytes

s.chars{|h|x='1234567890qwertyuiop*asdfghjkl*zxcvbnm'.index h

Not very good, but I just had to get the first answer in.

Slightly ungolfed / commented version:


f = -> s, m {
    # get knob coords
    c = m.flat_map.with_index {|x, i| x.size.times.select{|j| x[j] == ?D }.map{|y| [i, y] } }
    # for each char in the string
    s.chars {|h|
        # note the asterisks to correct for offsets
        x = '1234567890qwertyuiop*asdfghjkl*zxcvbnm'.index h
        # get un-"staggered" x and y coords
        x, y = (x % 10) * 2, x / 10
        # add one to x for every other row to fit keyboard
        x += 1 if y % 2 > 0
        # find closest knob by Manhattan distance
        a, b = c.min_by{|a, b| (y - a).abs + (x - b).abs }
        # LIGHT IT ON FIRE!
        m[a][b] = 'F'
    # return new map

puts f['helloworld123', ['D    D       D     D', '    D', '', '              D  D']]

JavaScript (ES6), 204 bytes


Fine, I'll answer it. ;)


  [...s].map(c=>(                     // iterate through each character of the input

    // Get X and Y coordinates of the character input

    // Find the nearest doorknob
    n=                                // n = Manhattan distance to nearest doorknob
    h.map((k,i)=>                     // iterate through each character of the house
      k.match(/\s/)||                 // do not check distance to whitespace characters
        (d=a(x-i%21)+a(y-i/21|0))>n|| // d = distance to the current doorknob
          (n=d,                       // set the nearest doorknob to this one
          p=i)                        // p = position of the doorknob
    h[p]="F"                          // update the doorknob to "F" in the house string
  ),h=[...h])&&h.join``               // return the house map as a string


<input type="text" id="input" value="helloworld123" /><br />
<textarea id="house" rows="4" cols="20">D    D       D     D
              D  D  </textarea><br />
<button onclick='result.innerHTML=(


<pre id="result"></pre>

  • 2
    \$\begingroup\$ I finally got another answer!!! :D \$\endgroup\$ Commented Nov 27, 2015 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.