title: It's a Bit of a Stretch․․․

text:
# Question
Title: It's a Bit of a Stretch․․․
## Input:

We take two inputs:

* An input `b` with two distinct values: `Left` and `Right`.
* And a positive integer `n`.

## Output:

Based on the Left/Right input, we output either of the following two sequences in the range of `1-n` (in the sequences below the first 125 items are displayed):

```
Left:
1, 6, 7, 56, 57, 62, 63, 960, 961, 966, 967, 1016, 1017, 1022, 1023, 31744, 31745, 31750, 31751, 31800, 31801, 31806, 31807, 32704, 32705, 32710, 32711, 32760, 32761, 32766, 32767, 2064384, 2064385, 2064390, 2064391, 2064440, 2064441, 2064446, 2064447, 2065344, 2065345, 2065350, 2065351, 2065400, 2065401, 2065406, 2065407, 2096128, 2096129, 2096134, 2096135, 2096184, 2096185, 2096190, 2096191, 2097088, 2097089, 2097094, 2097095, 2097144, 2097145, 2097150, 2097151, 266338304, 266338305, 266338310, 266338311, 266338360, 266338361, 266338366, 266338367, 266339264, 266339265, 266339270, 266339271, 266339320, 266339321, 266339326, 266339327, 266370048, 266370049, 266370054, 266370055, 266370104, 266370105, 266370110, 266370111, 266371008, 266371009, 266371014, 266371015, 266371064, 266371065, 266371070, 266371071, 268402688, 268402689, 268402694, 268402695, 268402744, 268402745, 268402750, 268402751, 268403648, 268403649, 268403654, 268403655, 268403704, 268403705, 268403710, 268403711, 268434432, 268434433, 268434438, 268434439, 268434488, 268434489, 268434494, 268434495, 268435392, 268435393, 268435398, 268435399, 268435448, 268435449

Right:
1, 4, 7, 32, 39, 56, 63, 512, 527, 624, 639, 896, 911, 1008, 1023, 16384, 16415, 16864, 16895, 19968, 19999, 20448, 20479, 28672, 28703, 29152, 29183, 32256, 32287, 32736, 32767, 1048576, 1048639, 1050560, 1050623, 1079296, 1079359, 1081280, 1081343, 1277952, 1278015, 1279936, 1279999, 1308672, 1308735, 1310656, 1310719, 1835008, 1835071, 1836992, 1837055, 1865728, 1865791, 1867712, 1867775, 2064384, 2064447, 2066368, 2066431, 2095104, 2095167, 2097088, 2097151, 134217728, 134217855, 134225792, 134225919, 134471680, 134471807, 134479744, 134479871, 138149888, 138150015, 138157952, 138158079, 138403840, 138403967, 138411904, 138412031, 163577856, 163577983, 163585920, 163586047, 163831808, 163831935, 163839872, 163839999, 167510016, 167510143, 167518080, 167518207, 167763968, 167764095, 167772032, 167772159, 234881024, 234881151, 234889088, 234889215, 235134976, 235135103, 235143040, 235143167, 238813184, 238813311, 238821248, 238821375, 239067136, 239067263, 239075200, 239075327, 264241152, 264241279, 264249216, 264249343, 264495104, 264495231, 264503168, 264503295, 268173312, 268173439, 268181376, 268181503, 268427264, 268427391

```

**How are these sequences generated you ask?**

A default sequence from 1 through `n=10` would be:

```
As integer:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

As binary:
1 10 11 100 101 110 111 1000 1001 1010

```

When we stretch left, the binary will become this:

```
1, 110, 111, 111000, 111001, 111110, 111111, 1111000000, 1111000001, 1111000110

```

Why? The last bit is used once; the single-last is used twice; the second-last is used three times; etc.

```
So `1010` will become (spaces added as clarification): `1111 000 11 0`

```

And these new left-stretched binary strings are converted back to integers:

```
1, 6, 7, 56, 57, 62, 63, 960, 961, 966

```

---

As for stretched right, the first bit is used once; second twice; third three times; etc. Like this:

```
As binary:
1, 100, 111, 100000, 100111, 111000, 111111, 1000000000, 1000001111, 1001110000

As integer:
1, 4, 7, 32, 39, 56, 63, 512, 527, 624

```

## Challenge rules:

* You can take any two distinct values, but state which one you use. So it can be `1/0`, `true/false`, `null/undefined`, `"left"/"right"`, etc.
* `n` is always larger than 0.
* You should support a maximum output of at least your language's default integer (which is 32-bit for most languages).
* Output format is flexible. Can be printed or returned as array/list. Can be with a space, comma, pipe, and alike as delimiter. Your call. (Again, please state what you've used.)

## General rules:

* This is , so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
* Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
* Default Loopholes are forbidden.
* If possible, please add a link with a test for your code.
* Also, please add an explanation if necessary.

# Answer
> 7 votes
# Jelly, 9 bytes

```
BmxJ$mḄð€

```

Try it online!

`-1` for left, `1` for right.

# Answer
> 6 votes
# Python 2, ~~102~~ 96 bytes

```
lambda d,n:[int(''.join(c*-~i for i,c in enumerate(bin(x+1)[2:][::d]))[::d],2)for x in range(n)]

```

-1 for left, 1 for right

Try it online!

# Answer
> 5 votes
# 05AB1E, ~~14~~ 13 bytes

Saved 1 byte thanks to *Erik the Outgolfer*

```
LbεIiRƶRëƶ}JC

```

`1` for left.
`0` (or anything else) for right.

Try it online!

**Explanation**

```
L                # push range [1 ... first_input]
 b               # convert to binary
  ε              # apply to each
   Ii            # if second_input is true
     RƶR         # reverse, multiply each element with its 1-based index and reverse again
        ëƶ       # else, multiply each element with its 1-based index
          }      # end if
           J     # join
            C    # convert to base-10

```

# Answer
> 3 votes
# Japt, ~~19~~ ~~18~~ 17 bytes

`0` for "left", `1` for "right". (It can actually take any falsey or truthy values in place of those 2.)

```
õÈ¤ËpV©EÄªEnFlÃn2

```

Test it

---

## Explanation

Implicit input of integers `U` & `V`.

```
õ

```

Create an array of integers from 1 to `U`, inclusive.

```
È

```

Pass each through a function.

```
¤

```

Convert the current integer to a binary string

```
Ë           Ã

```

Map over the string, passing each character through a function, where `E` is the current index and `F` is the full string.

```
p

```

Repeat the current character

```
V©  ª

```

`©` is logical AND (`&&`) and `ª` is logical OR `||`, so here we're checking if `V` is truthy (non-zero) or not.

If `V` is truthy then `X` gets repeated `Y+1` times.

```
YnZl

```

If `V` is falsey then `X` gets repeated `Y` subtracted from (`n`) the length (`l`) of `Z` times.

```
n2

```

Convert back to a base 10 integer.

Implicitly output resulting array.

# Answer
> 3 votes
## Husk, 13 bytes

```
mȯḋṠṘo?ḣṫ⁰Lḋḣ

```

That's a lot of dotted letters...

Takes first `b` (`0` for left and `1` for right), then `n`. Try it online!

## Explanation

```
mȯḋṠṘo?ḣṫ⁰Lḋḣ  Inputs b=1 and n=5 (implicit).
            ḣ  Range to n: [1,2,3,4,5]
mȯ             Map function over the range:
                 Argument: number k=5
           ḋ     Binary digits: [1,0,1]
   ṠṘ            Repeat each digit with respect to the list
     o    L      obtained by applying to the length (3) the function
      ?  ⁰       if b is truthy
       ḣ         then increasing range: [1,2,3]
        ṫ        else decreasing range: [3,2,1].
                 We get [1,0,0,1,1,1].
  ḋ              Convert back to integer: 39
               Final result [1,4,7,32,39], implicitly print.

```

# Answer
> 2 votes
# Gaia, 15 bytes

```
⟪¤bw¦¤;ċ%×†_b⟫¦

```

Uses `-1` for left and `1` for right.

Try it online!

### Explanation

```
⟪¤bw¦¤;ċ%×†_b⟫¦  Map this block over the range 1..n, with direction as an extra parameter:
 ¤                Swap, bring current number to the top
  b               List of binary digits
   w¦             Wrap each in a list
     ¤            Bring direction to the top
      ;ċ          Push the range 1..len(binary digts)
        %         Select every direction-th element of it (-1 reverses, 1 does nothing)
         ×†       Element-wise repetition
           _      Flatten
            b     Convert from binary to decimal

```

# Answer
> 2 votes
# Proton, 79 bytes

```
n=>d=>[int(''.join(b[x]*[l-x,x-1][d]for x:2..(l=len(b=bin(i)))),2)for i:1..n+1]

```

`0` is left, `1` is right.

Try it online!

### Ungolfed

```
f=n=>d=>                        # Curried function
[                               # Collect the results in a list
 int(                           # Convert from a binary string to an int:
  ''.join(                       # Join together
   b[x]                           # Each character of the binary string of n
   *[l-x,x-1][d]                  # Repeated by its index or its index from the end, depending on d
   for x:2..(l=len(b=bin(i))))
 ,2)
 for i:1..n+1                   # Do this for everything from 1 to n inclusive
]

```

# Answer
> 2 votes
# C# (.NET Core), ~~192~~ 187 + 23 bytes

*-5 bytes thanks to TheLethalCoder*

```
b=>n=>new int[n].Select((_,a)=>{var g=Convert.ToString(a+1,2);return(long)g.Reverse().SelectMany((x,i)=>Enumerable.Repeat(x,b?i+1:g.Length-i)).Select((x,i)=>x>48?Math.Pow(2,i):0).Sum();})

```

Byte count also includes:

```
namespace System.Linq{}

```

Try it online!

Input: `left` is `true`, `right` is `false`

Explanation:

```
b => n =>                                   // Take two inputs (bool and int)
    new int[n].Select((_, a) => {           // Create new collection the size of n
        var g = Convert.ToString(a + 1, 2); // Take every number in sequence 1..n and convert to base 2 (in a string)
        return (long)g.Reverse()            // Reverse the bits
               .SelectMany((x, i) =>        // Replace every bit with a collection of its repeats, then flatten the result
                   Enumerable.Repeat(x, b ? i + 1 : g.Length - i))
               .Select((x, i) => x > 48 ? Math.Pow(2, i) : 0)
               .Sum();                      // Replace every bit with a corresponding power of 2, then sum
    })

```

# Answer
> 2 votes
# JavaScript (ES6), 113 bytes

Oh, this is just far too long! This is what happens when you spend the day writing "real" JavaScript, kiddies; you forget how to golf properly!

Uses any truthy or falsey values for `b`, with `false` being "left" and `true` being "right".

```
n=>b=>[...Array(n)].map(_=>eval("0b"+[...s=(++e).toString(2)].map((x,y)=>x.repeat(b?++y:s.length-y)).join``),e=0)

```

---

## Try it

```
o.innerText=(f=
n=>b=>[...Array(n)].map(_=>eval("0b"+[...s=(++e).toString(2)].map((x,y)=>x.repeat(b?++y:s.length-y)).join``),e=0)
)(i.value=10)(j.value=1);oninput=_=>o.innerText=f(+i.value)(+j.value)
```

```
label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
```

```
<label for=i>Size: </label><input id=i type=number><label for=j>Direction: </label><input id=j min=0 max=1 type=number><pre id=o>
```

# Answer
> 2 votes
# JavaScript (ES6), 131 bytes

This is significantly longer than Shaggy's answer, but I wanted to try a purely bitwise approach.

Due to the 32-bit limit of JS bitwise operations, this works only for ***n \< 128***.

Takes input in currying syntax `(n)(r)`, where ***r*** is falsy for left / truthy for right.

```
n=>r=>[...Array(n)].map((_,n)=>(z=n=>31-Math.clz32(n),g=n=>n&&(x=z(b=n&-n),r?2<<z(n)-x:b*2)-1<<x*(r?2*z(n)+3-x:x+1)/2|g(n^b))(n+1))

```

### Formatted and commented

```
n => r => [...Array(n)]       // given n and r
  .map((_, n) => (            // for each n in [0 .. n - 1]
    z = n =>                  //   z = helper function returning the
      31 - Math.clz32(n),     //       0-based position of the highest bit set
    g = n =>                  //   g = recursive function:
      n && (                  //     if n is not equal to 0:
        x = z(b = n & -n),    //       b = bitmask of lowest bit set / x = position of b
        r ?                   //       if direction = right:
          2 << z(n) - x       //         use 2 << z(n) - x
        :                     //       else:
          b * 2               //         use b * 2
      ) - 1                   //       get bitmask by subtracting 1
      << x * (                //       left-shift it by x multiplied by:
        r ?                   //         if direction = right:
          2 * z(n) + 3 - x    //           2 * z(n) + 3 - x
        :                     //         else:
          x + 1               //           x + 1
      ) / 2                   //       and divided by 2
      | g(n ^ b)              //     recursive call with n XOR b
    )(n + 1)                  //   initial call to g() with n + 1
  )                           // end of map()

```

### Demo

```
let f =

n=>r=>[...Array(n)].map((_,n)=>(z=n=>31-Math.clz32(n),g=n=>n&&(x=z(b=n&-n),r?2<<z(n)-x:b*2)-1<<x*(r?2*z(n)+3-x:x+1)/2|g(n^b))(n+1))

console.log(JSON.stringify(f(10)(false)))
console.log(JSON.stringify(f(10)(true)))
```

# Answer
> 2 votes
# Dyalog APL, 23 bytes

```
{2⊥b/⍨⌽⍣⍺⍳≢b←2⊥⍣¯1⊢⍵}¨⍳

```

left is `1` right is `0` (passed in as left argument of function)

is index generator

`{`... apply function in braces to each item on the right

`b←2⊥⍣¯1⊢⍵` b is ⍵ encoded as binary (using the inverse of decode to get the minimum number of bits required to represent  in base 2)

`⍳≢b` generate indexes for vector b (`≢b` is length of b)

`⌽⍣⍺` reverse  times (used conditionally here for left or right stretch)

`b/⍨` b replicated by (replicates the bits as per the (reverse)index)

`2⊥` decode from base 2

TryAPL online

# Answer
> 1 votes
# Jelly, 11 bytes

```
B€xJṚ⁹¡$$€Ḅ

```

Try it online!

Argument #1: `n`
Argument #2: `1` for left, `0` for right.

# Answer
> 1 votes
# Retina, 111 bytes

```
\d+
$*
1
$`1¶
+`(1+)\1
${1}0
01
1
.
$.%`$*R$&$.%'$*L
+s`(R?)(\d)(L?)(.*¶\1\3)$
$2$2$4
¶*[RL]

1
01
+`10
011
%`1

```

Try it online! Takes the number and either `L` or `R` as a suffix (or on a separate line). Explanation:

```
\d+
$*
1
$`1¶

```

Convert from decimal to unary and count from 1 to `n`.

```
+`(1+)\1
${1}0
01
1

```

Convert from unary to binary.

```
.
$.%`$*R$&$.%'$*L

```

Wrap each bit in `R` and `L` characters according to its position in the line.

```
+s`(R?)(\d)(L?)(.*¶\1\3)$
$2$2$4

```

Replace the relevant `R` or `L` characters with the appropriate adjacent digit.

```
¶*[RL]

1
01
+`10
011
%`1

```

Remove left-over characters and convert from binary to decimal.

# Answer
> 1 votes
# Ruby, 98 bytes

```
->n,r{t=->a{r ?a:a.reverse};(1..n).map{|k|i=0;t[t[k.to_s(2).chars].map{|d|d*(i+=1)}].join.to_i 2}}

```

# Answer
> 1 votes
# JavaScript (ES6), ~~130~~ 127 bytes

*3 bytes, thanks Kevin*

I sure don't know enough ES6 for this site, but I tried! Loop through each number, and loop through each binary representation for that number, repeating each character however many times needed.

```
d=>n=>{i=0;while(i++<n){b=i.toString(2),s="",k=-1,l=b.length;while(++k<l)s+=b[k].repeat(d?k+1:l-k);console.log(parseInt(s,2))}}

```

```
f=d=>n=>{i=0;while(i++<n){b=i.toString(2),s="",k=-1,l=b.length;while(++k<l)s+=b[k].repeat(d?k+1:l-k);console.log(parseInt(s,2))}}
f(1)(10)
f(0)(10)
```

# Answer
> 1 votes
# Java 8, 136 bytes

Lambda (curried) from `Boolean` to a consumer of `Integer`. The boolean parameter indicates whether to stretch left (values `true`, `false`). Output is printed to standard out, separated by newlines, with a trailing newline.

```
l->n->{for(int i=0,o,c,d,s=0;i++<n;System.out.println(o)){while(i>>s>0)s++;for(o=c=0;c++<s;)for(d=0;d++<(l?s-c+1:c);o|=i>>s-c&1)o<<=1;}}

```

## Ungolfed lambda

```
l ->
    n -> {
        for (
            int i = 0, o, c, d, s = 0;
            i++ < n;
            System.out.println(o)
        ) {
            while (i >> s > 0)
                s++;
            for (o = c = 0; c++ < s; )
                for (
                    d = 0;
                    d++ < (l ? s - c + 1 : c);
                    o |= i >> s - c & 1
                )
                    o <<= 1;
        }
    }

```

Try It Online

## Limits

Because they're accumulated in `int`s, outputs are limited to 31 bits. As a result, inputs are limited to 7 bits, so the maximum input the program supports is 127.

## Explanation

This solution builds up each stretched number using bitwise operations. The outer loop iterates `i` over the numbers to be stretched, from 1 to *n*, and prints the stretched value after each iteration.

The inner `while` loop increments `s` to the number of bits in `i`, and the subsequent `for` iterates `c` over each bit position. Within that loop, `d` counts up to the number of times to repeat the current bit, which depends on input `l`. At each step, `o` is shifted left and the appropriate bit of `i` is masked off and OR'd in.

---
Tags: code-golf, number, sequence, integer, binary
---


title: Generate unseen numbers

text:
# Question
Title: Generate unseen numbers
Let us say a substring is any continuous section of an original string. For example `cat` is a substring of `concatenate`. We will say that a *proper* substring is a substring that is not equal to the original string. For example `concatenate` is a substring of `concatenate` but not a proper substring. (single character strings have no proper substrings)

We will now define a sequence using these terms. The **n**th term in this sequence will be the smallest number such that there is a proper substring of its binary representation that is not a substring of any earlier term in the sequence. The first term is `10`.

As an exercise lets generate the first 5 terms. I will work in binary to make things easier.

The first term is `10`. Since `11`, the next smallest number, has only one proper substring, `1` which is also a substring of `10`, `11` is not in the sequence. `100` however does contain the proper substring `00` which is not a substring of `10` so `100` is our next term. Next is `101` which contains the unique proper substring `01` adding it to the sequence, then `110` contains the proper substring `11` which is new adding it to the sequence.

Now we have

```
10, 100, 101, 110

```

`111` is up next but it contains only the substrings `1` and `11` making it not a term. `1000` however contains `000` adding it to the sequence.

Here are the first couple terms in decimal

```
2, 4, 5, 6, 8, 9, 10, 11, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 50, 54, 56, 58

```

## Task

Either

* Take **n** as input and generate the **n**th term in this sequence (either 0 or 1 indexed)
* Continuously output terms of the sequence

This is answers are scored in bytes with less bytes being better.

# Answer
# Python 3, ~~88~~ ~~80~~ ~~78~~ 75 bytes

-6 bytes thanks to Wheat Wizard
-2 bytes thanks to RootTwo
-3 bytes thanks to notjagan

```
s={0}
n=1
while 1:n+=1;b=f"{n:b}";p={b[1:],b[:-1]};s|=p-s and{b,print(n)}|p

```

Try it online!
> 5 votes

# Answer
# Jelly, 22 bytes

```
BẆṖ
ṄÇ;ð⁹Çḟ¥?⁹⁸‘¤ß
2ç⁸

```

Try it online!
> 2 votes

# Answer
## Mathematica, 116 110 bytes

```
x={};f=Subsequences[#~IntegerDigits~2]&;Do[MemberQ[Most@f@n,s_/;FreeQ[f/@x,s,2]]&&x~AppendTo~Echo@n,{n,2,∞}]

```

Infinitely outputs terms of the sequence.

# Explanation

```
x={};

```

`x` is the list of terms of the sequence so far.

```
f=Subsequences[#~IntegerDigits~2]&

```

`f` is a `Function` which takes an integer and returns all `Subsequences` of its base `2` representation (including the empty list `{}` and the full list of `IntegerDigits` itself).

```
Do[...,{n,2,∞}]

```

Evaluate `...` for value of `n` from `2` to .

```
...&&x~AppendTo~Echo@n

```

If `...` is `False`, then the second argument to `And` (`&&`) is never evaluated. If `...` is `True`, then `Echo@n` prints and returns `n`, which we then `AppendTo` the list `x`.

```
MemberQ[Most@f@n,s_/;FreeQ[f/@x,s,2]]

```

We want to check that some proper substring of `n` is not a substring of any previous term in the sequence. `Most@f@n` is the list of proper substrings of `n`, we then check whether there are any substrings `s_` which is a `MemberQ` of that list such that the list `f/@x` of lists of substrings of previous terms of the sequence is `FreeQ` of `s` at level `2`.
> 1 votes

# Answer
# Mathematica, ~~109~~ 94 bytes

```
s={};Do[!SubsetQ[s,(t=Subsequences@IntegerDigits[i,2])[[2;;-2]]]&&(s=s~Join~t;Echo@i),{i,∞}]

```

Continuously output terms of the sequence

*Special thanx to **@ngenisis** for -15 bytes*

# Mathematica, 123 bytes

```
(s=r={};For[i=2,i<2#,i++,If[!ContainsAll[s,(t=Subsequences@IntegerDigits[i,2])[[2;;-2]]],s=s~Join~t;r~AppendTo~i]];r[[#]])&

```

Take n as input and generate the nth term in this sequence (1 indexed)

**input**

> \[1000\]

**output**

> 1342
> 1 votes

# Answer
# Pyth, 20 bytes

```
u+G
fP-Fm.:.Bd)+TG1Y

```

This prints the sequence infinitely. It can only be used offline as a consequence.

Explanation (The space is a newline):

```
u+G fP-Fm.:.Bd)+TG1Y
u                  Y    Apply the following function to the previous output
                        until it stops changing (or forever, in this case),
                        starting with the empty list
    f             1     Find the first positive integer where
               +TG      The integer prepended to the current list
        m               Map to
           .Bd          Convert to binary
         .:   )         Form all subsequences
      -F                Fold the filter-out function over the list
                        This iteratively removes all subsequences already seen
                        from the candidate
     P                  Remove the last subsequence which is the whole number.
   (newline)            Print that first integer
 +G                     Prepend that first integer to the list

```
> 0 votes

# Answer
# Pyth, 20 bytes

```
.V2I-=ZP.:jb2)Yb=+YZ

```

Try it online!
> 0 votes

# Answer
# Haskell,  172 bytes

```
import Data.List
b 0=""
b n=b(n`div`2)++(show$n`mod`2)
s=nub.(tails=<<).inits
p x=s x\\[x]
n(_,l)x|(p.b)x\\l/=[]=(x,l++(s.b)x)|1<2=(0,l)
filter(>1)$fst<$>scanl n(1,[])[1..]

```

Try it online.

### Explanation

The code generates the sequence continuously.

* `b` returns the binary representation of an `Int` as a `String`
* `s` returns all the substrings of a string
* `p` returns all the proper substrings of a string
* `n` is a function that is applied iteratively and returns a tuple containing:
    + the current element, if it is a member of the sequence, otherwise 0
    + a list of all substrings to check against for all following numbers
* finally, `scanl` is used to call `n` over and over again and its output is filtered to only contain elements greater than 1

Here's a slightly more readable version, before golfing:

```
import Data.List

binary :: Int -> String
binary 0=""
binary n|(d,r)<-divMod n 2=binary d++["01"!!r]

substrings :: String -> [String]
substrings xs = nub$inits xs>>=tails

properSubstrings :: String -> [String]
properSubstrings xs = substrings xs\\[xs]

sb  = substrings.binary
psb = properSubstrings.binary

g = scanl step (1,[]) [1..]
  where step (_,l) x | psb x \\ l /= [] = (x,l++sb x)
                     | otherwise        = (0,l)

f=filter(>1)$fst<$>g

```
> 0 votes

# Answer
# JavaScript, 57 bytes

```
for(x=1;;x++)/^10|10(00)*$/.test(x.toString(2))&&alert(x)

```

```
alert=x=>{if(x>100)throw 'Let we stop here.';document.write(x+'<br/>');};

for(x=1;;x++)/^10|10(00)*$/.test(x.toString(2))&&alert(x)
```

Let we write the given number *n* in binary form, then:

* If the number is starts with `10`, *n* must in the sequence:
    + remove the first `1` in it, the remaining binary string must not be seen, since *n* is the smallest number which may contain such string
* If the number is starts with `11`:
    + By removing the first `1` in it, the remaining binary string (let we donate it as `1x` must be seen since:
        - number `1x` is in the sequence, or
        - number `1x0` is in the sequence, since it contain unique sub string `1x`
    + If it is odd (ends with 1), it must not in the sequence, since:
        - (*n* \- 1) / 2 in the sequence, or
        - (*n* \- 1) in the sequence, since it contain unique sub string (*n* \- 1) / 2
    + If it is even (ends with 0), it is in the sequence iff *n* / 2 is not in the sequence
        - with the same idea, *n* / 2 is *not* in the sequence iff *n* / 2 is odd, or n / 4 is in the sequence

Conclusion:

the binary form of the number starts with `10` or ends with `1` followed by odd number of `0`. Or describe in regex: x match `/^10|10(00)*$/`.
> 0 votes

---
Tags: code-golf, number, sequence, binary
---


# Question
Title: Simple DTMF decoding: find the phone key!
This is a simple challenge which hopefully may lead to some creative answers.

Quoting Wikipedia: *"Dual-tone multi-frequency signaling (DTMF) is an in-band telecommunication signaling system using the voice-frequency band over telephone lines between telephone equipment and other communications devices and switching centers."*

## Task

Given two integers representing the column and row frequencies in the following table, your task is to output the corresponding key:

```
       | 1209 Hz | 1336 Hz | 1477 Hz
-------+---------+---------+---------
697 Hz |    1    |    2    |    3
770 Hz |    4    |    5    |    6
852 Hz |    7    |    8    |    9
941 Hz |    *    |    0    |    #

```

## Examples

* If the input is `[ 1209, 852 ]`, the expected output is "7".
* If the input is `[ 1477, 941 ]`, the expected output is "#".

## Rules

* You must take input as *integers* in any reasonable format, such as two separate variables or an array of two variables. Please specify in which order your program is expecting them (either *column\_freq, row\_freq* or *row\_freq, column\_freq*).
* The input is guaranteed to be valid.
* You must print or output a *character*. However, you're also allowed to output an integer for digit keys.
* This is , so the shortest answer in bytes wins!

# Answer
> 2 votes
# Jelly, 19 bytes

```
DḢ×3++6ị9R;“*0#  ”¤

```

Try it online!

Takes input as `row`, `column` in two arguments.

This uses `3*<first digit of row> + <column> + 6 % 14` to give a different value for each. This is indexed into `[1,2,3,4,5,6,7,8,9,"*","0","#"," "," "]` to give the output. The spaces could really be any character; they are just buffer to create an implicit mod 14.

**Low level**

```
DḢ×3++6ị9R;“*0#  ”¤
D                     - digits (of <row>)
 Ḣ                    - head (get first element)
  ×3                  - multiply by 3
    +                 - add (<column>)
     +6               - add 6
       ị              - index into:
                  ¤   - the nilad:
        9R              - range(9)
          ;             - concatenate
           “*0#  ”      - the string "*0#  "

```

# Answer
> 8 votes
# JavaScript (ES6), ~~39~~ 35 bytes

```
a=>b=>"310*58# 47269"[a%b%83%16%13]

```

Maps the two inputs into numbers in the range `[0, 13)` by calculating:
`col % row % 83 % 16 % 13`.
Takes input in currying syntax (`f(col)(row)`) and returns a single-char string.

## Test Cases

```
let f=
a=>b=>"310*58# 47269"[a%b%83%16%13]

console.log( f(1336)(941) )
console.log( f(1209)(697) )
console.log( f(1336)(697) )
console.log( f(1477)(697) )
console.log( f(1209)(770) )
console.log( f(1336)(770) )
console.log( f(1477)(770) )
console.log( f(1209)(852) )
console.log( f(1336)(852) )
console.log( f(1477)(852) )
console.log( f(1209)(941) )
console.log( f(1477)(941) )
```

```
.as-console-wrapper{max-height:100%!important}
```

## History

Started with the range of `[0, 20)` with calculation `col % row % 29 % 20`, which required 8 wasted characters in the mapping.

# Answer
> 4 votes
## Haskell, ~~42~~ 37 bytes

```
x#y="_1425__#9__*70836"!!mod(2*x+y)18

```

Just some math to index a string. Input order is `<column> # <row>`, e.g. `1336 # 697`.

Try it online!

Edit: @flawr found a formula that works on shorter string. Overall -5 bytes. Thanks!

# Answer
> 3 votes
# MATL, 23 bytes

```
13*+79\'186#294*3750'w)

```

Inputs are: column frequency, then row frequency.

Try it online!

### Explanation

Multiplying the column frequency by 13, adding the row frequency, and computing modulo 79 gives a different value for each of the 12 pairs of inputs.

```
13*               % First input (implicit) times 13
+                 % Add to second input (implicit)
79\               % Modulo 79
'186#294*3750'    % Push this string
w                 % Swap
)                 % Index

```

# Answer
> 2 votes
# Jelly, 24 bytes

```
%157ị9R;“*0#”¤s3¤ị@⁹%11¤

```

Try it online!

# Answer
> 2 votes
# JavaScript (Node.js), 34 bytes

```
(a,b)=>'806739214*#5'[a*b%2785%12]

```

Try it online!

# Python 3, 39 bytes

```
lambda a,b: '806739214*#5'[a*b%2785%12]

```

Try it online!

row,column can be supplied in any order.

# Answer
> 1 votes
# Befunge, 34 bytes

```
&"~"/9-4*&"d"/6-+1g,@
147*2580369#

```

Try it online!

Explanation:

```
&"~"/9-4*                (Get number / 126 - 9) * 4 = 0, 4, or 8
         &"d"/6-         Get number / 100 - 6 = 0, 1, 2, or 3
                +        Add the above results - this becomes the x-coordinate in the "lookup table"
                 1g,@    Get a value on row 1 column x, output it, and end

```

---
Tags: code-golf, matrix, conversion
---


# Question
Title: Reduce string to a snippet of the alphabet
Given a non-empty string consisting of only lower and upper case alphabetical characters and spaces (`[a-zA-Z ]`), reduce it to a snippet of the alphabet, starting with the first character.

To reduce a string, begin with the first alphabetical character, then remove every character after it that is not the next letter of the alphabet. Continue doing this until you reach the end of the string.

For example `codegolf`:

Begin with `c`, remove `o` as it is not the next letter of the alphabet.
Keep `d` as it *is* the next letter of the alphabet, and keep `e` as it is the next letter too.
Remove `g`, `o`, and `l`, and keep `f`.

Your final snippet would then be `cdef`

## Rules

* Capitalisation should be maintained, so `CodEgolF` would result in `CdEF`
* Space is not a letter of the alphabet, and thus should always be removed, even if it is the start of the string
* Due to the nature of the reduction, the first alphabetical character of the input will *always* be the first character of the output.
* `zZ` is the last letter of the alphabet. There are no letters after it, the alphabet does not loop.

## Test Cases

```
codegolf -> cdef
CodEgolf -> CdEf
 codeolfg -> cdefg
ProgrammingPuzzles -> P
Stack Exchange -> St
The quick red fox jumped over the lazy brown dog -> Tuvw
Zebra -> Z
Abcdegfhijkl -> Abcdef

```

## Scoring

This is , so fewest bytes **in each language** wins!

# Answer
> 12 votes
# JavaScript (ES6), ~~66~~ ~~79~~ ~~68~~ 67 bytes

```
f=([c,...s],p)=>c?(p?~parseInt(c+p,36)%37:c<'!')?f(s,p):c+f(s,c):''

```

### How?

**Testing consecutive letters**

Because converting two characters to their ASCII codes would be a rather lengthy operation in JS, we use the following formula instead:

```
~parseInt(b + a, 36) % 37

```

Provided that both ***a*** and ***b*** are in `[a-zA-Z ]`, the above expression equals `0` if and only if ***a*** and ***b*** are consecutive letters (i.e. consecutive digits in base 36), no matter the case of the characters.

For instance:

```
~parseInt("Y" + "x", 36) = ~(36 * parseInt("Y", 36) + parseInt("x", 36))
                         = ~(36 * 34 + 33)
                         = -(36 * 34 + 33 + 1)
                         = -(37 * 34)

```

**Formatted and commented**

```
f = ([c,                              // c = current character
         ...s],                       // s = array of remaining characters
                p) =>                 // p = previous matching letter
  c ? (                               // if there's still at least 1 character to process:
      p ?                             //   if p was already defined:
        ~parseInt(c + p, 36) % 37     //     test if p and c are NON-consecutive letters
      :                               //   else:
        c < '!'                       //     test if c is a space character
    ) ?                               //   if the above test passes:
      f(s, p)                         //     ignore c and keep the current value of p
    :                                 //   else:
      c + f(s, c)                     //     append c to the final result and update p to c
  :                                   // else:
    ''                                //   stop recursion

```

### Test cases

```
f=([c,...s],p)=>c?(p?~parseInt(c+p,36)%37:c<'!')?f(s,p):c+f(s,c):''

console.log(f("codegolf")) // -> cdef
console.log(f("CodEgolf")) // -> CdEf
console.log(f(" codeolfg")) // -> cdefg
console.log(f("ProgrammingPuzzles")) // -> P
console.log(f("Stack Exchange")) // -> St
console.log(f("The quick red fox jumped over the lazy brown dog")) // -> Tuvw
console.log(f("Zebra")) // -> Z
console.log(f("Abcdegfhijkl")) // -> Abcdef
```

# Answer
> 7 votes
# Python 3, ~~75 85 84 91 81 77~~ 75 bytes

I think this is as short as it can get in Python 3. It can be shortened by a few bytes in Python 2, as shown in Sisyphus' submission.

* **EDIT:** +10 for fixing a bug
* **EDIT:** -1 by fixing another bug
* **EDIT:** +7 for fixing another bug
* **EDIT:** -10 bytes with help from @Ruud
* **EDIT:** -4 bytes since the OP allowed us to output the letters separated by a newline
* **EDIT:** -2 bytes thanks to @Ruud, back to the original byte count!

```
s=input().strip();k=0
for i in s:
 if(ord(i)-ord(s[0]))%32==k:k+=1;print(i)

```

Try it online!

# Answer
> 7 votes
# Python 2, 69 bytes

```
lambda s:reduce(lambda x,y:x+y*((ord(y)-ord(x[~0]))%32==1),s.strip())

```

Try it online!

A simple reduction of the string. We simply concatenate the next character if and only if `(ord(y)-ord(x[~0]))%32==1`. Very ugly check - I am sure it can be improved, but I'm not sure how!

# Answer
> 6 votes
# 05AB1E, 13 bytes

```
áćsv¤yìuÇÆiy«

```

Try it online!

-1 thanks to Adnan

# Answer
> 4 votes
# Brachylog, 15 bytes

```
;ṢxS⊇.ḷ~sẠ∧Sh~h

```

Try it online!

This would be 10 bytes: `⊇.ḷ~sẠ&h~h`, if it weren't for the fairly uninteresting "strings can start with spaces" constraint.

### Explanation

```
;ṢxS               S is the Input with all spaces removed
   S⊇.             The Output is an ordered subset of the Input
     .ḷ            The Output lowercased…
        ~sẠ          …is a substring of "abcdefghijklmnopqrstuvwxyz"
           ∧
            Sh     The first char of S…
              ~h   …is the first char of the Output

```

Since this is fairly declarative, this is also really slow.

# Answer
> 3 votes
# MATL, ~~18~~ ~~16~~ 15 bytes

*Thanks to Mr.Xcoder for pointing out a mistake, now corrected*

```
Xz1&)"t@hkd1=?@

```

Letters in the output are separated by newlines.

Try it online! Or verify all test cases (the footer code displays all output letters on the same line for clarity).

### Explanation

```
Xz       % Implicitly input a string. Remove spaces
1&)      % Push first character and then the remaining substring
"        % For each
  t      %   Duplicate previous character
  @      %   Push current character
  h      %   Concatenate both characters
  k      %   Convert to lowercase
  d      %   Consecutive difference. Gives a number
  1=     %   Is it 1?
  ?      %   If so
    @    %     Push current char
         %   End (implicit)
         % End (implicit)
         % Display stack (implicit)

```

# Answer
> 3 votes
# Java (OpenJDK 8), ~~102~~ ~~101~~ 74 bytes

```
s->{char c=0;for(char x:s)if(c<1&x>32|~-x%32==c%32)System.out.print(c=x);}

```

Try it online!

-27 bytes thanks to @Olivier Grégoire

# Answer
> 2 votes
# C# (Mono), ~~129~~ ~~107~~ ~~93~~ ~~91~~ ~~87~~ 86 bytes

```
s=>{var r=s.Trim()[0]+"";foreach(var c in s)if(r[r.Length-1]%32==~-c%32)r+=c;return r;}

```

*Saved 2 bytes thanks to @Mr. Xcoder.*
*Saved 4 bytes thanks to @jkelm.*
*Saved 1 byte thanks to @ceilingcat.*

Try it online!

# Answer
> 2 votes
# Pyth, ~~23 22 21~~ 20 bytes

-1 byte indirectly thanks to @Erik the Outgolfer's trick (`-Qd`).

-1 byte thanks to @Erik the Outgolfer.

```
VQIqZ%-CNCh-Qd32N=hZ

```

**Try it here.**

# Answer
> 2 votes
# Pyth, ~~21~~ ~~20~~ 18 bytes

```
ef&qhThQhxGrT0tyr6

```

Try it here.

Way more efficient 20-byte version:

```
.U+b?t-CrZ1Creb1kZr6

```

Try it here.

-1 thanks to Mr. Xcoder (indirectly).

# Answer
> 2 votes
# PHP, 64+1 bytes

```
while($c=$argn[$i++])$c<A||$n&&($c&_)!=$n||(print$c)&$n=++$c&__;

```

Run as pipe with `-nR` or try it online.

---

Apart from the usual tricks: When `$c` reaches `Z`, `++$c` results in `AA`,
and `&__` keeps that length untouched; so `$n` will match no further `$c`.

# Answer
> 2 votes
# Haskell, ~~106 105~~ 97 bytes

```
import Data.Char
import Data.List
z=ord.toUpper
a%b|z a+1==z b=b|0<3=a
nub.scanl1(%).filter(>' ')

```

*I tried to use `fromEnum` \+ char arithmetic instead of importing `Data.Char`, but that ended up being longer...*

Saved 8 bytes thanks to H.PWiz!

Try it online.

# Answer
> 1 votes
# Jelly, 17 bytes

```
ḟ⁶;ð,ŒuṪ€O_/⁼-ð¡/

```

Try it online!

# Answer
> 1 votes
# q/kdb+, ~~47~~ 45 bytes

**Solution:**

```
{10h$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x}

```

**Examples:**

```
q){"c"$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x}"CodEgolf"
"CdEf"
q){"c"$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x}" codeolfg"
"cdefg"
q){"c"$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x}"ProgrammingPuzzles"
"P"
q){"c"$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x}"The quick red fox jumped over the lazy brown dog"
"Tuvw"

```

**Explanation:**

Leveraging the `mod 32` trick from existing solutions along with converge function. Iterate over the string, if the difference between the last element of the result (e.g. starts with `T` for "The quick red fox...") and the current character is 1 (after being `mod`'d with 32), then we add this to the result (hence taking why we take `last x`), then cast everything back to a string.

```
{10h$({(x;x,y)1=mod[y-last x;32]}/)7h$trim x} / the solution
{                                           } / lambda function
                                      trim x  / trim whitespace (leading/trailing)
                                   7h$        / cast string to ASCII (a -> 97)
     ({                         }/)           / converge
                    y-last x                  / y is the next item in the list, x contains results so far
              1=mod[        ;32]              / is the result mod 32 equal to 1
       (x;x,y)                                / if false, return x, if true return x concatenated with y
 10h$                                         / cast back to characters

```

# Answer
> 1 votes
# Japt, ~~18~~ ~~17~~ 16 bytes

*Saved 1 byte thanks to @Shaggy*

```
x
c
Çc %H¥V%H©V°

```

Test it online!

Was thinking this would be a good bit shorter, but... Such is life...

### Explanation

```
x    First line: set U to the result.
x    Trim all spaces off of the input. Only necessary to remove leading spaces.

c    Second line: set V to the result.
c    Take the charcode of the first character in U.

 Ç   c %H¥ V%H© V°
UoZ{Zc %H==V%H&&V++}   Final line: output the result.
UoZ{               }   Filter to only the chars in Z where
    Zc                   the charcode of Z
       %H                mod 32
         ==V%H           equals V mod 32.
              &&V++      If true, increment V for the next letter.

```

# Answer
> 1 votes
# C# (.NET Core), ~~70~~ 60 + 18 bytes

*-10 bytes thanks to TheLethalCoder*

```
a=>{var c=a.Trim()[0];return a.Where(x=>x%32==c%32&&++c>0);}

```

Byte count also includes:

```
using System.Linq;

```

Try it online!

1 byte longer ~~(currently)~~ (not anymore) than TheLethalCoder's so posting for the sake of fun. Different approach, with LINQ.

This takes advantage of two C-like features in C# - a character `char` variable is implicitly behaving the same as an integer `int`, and boolean AND operator `&&` doesn't execute right operation if left returns a `false`. Code explanation:

```
a =>                                  // Take string as input
{
    var c = a.Trim()[0];              // Delete leading spaces and take first letter
    return a.Where(                   // Filter out characters from the string, leaving those that:
               x => x % 32 == c % 32  // it's the next character in alphabet case-insensitive (thanks to modulo 32 - credits to previous answers)
               && ++c > 0             // If it is, go to the subsequent character in alphabet (and this always has to return true)
           );
}

```

# Answer
> 1 votes
# Perl 5, 30 + 1 (-n) = 31 bytes

```
/$b/i&&(print,$b=++$_)for/\S/g

```

Try it online!

**How?**

```
/$b/i        # check if this letter equals the one in $b, ignore case
&&(print,    # output it if so
$b=++$_)     # store the next character to find
for/\S/g     # Looping over all non-whitespace characters

```

# Answer
> 1 votes
# Perl 6, 51 bytes

```
{S:i:g/\s|(\w){}<([<!before "{chr $0.ord+1}">.]+//}

```

Test it

## Expanded:

```
{  # bare block lambda with implicit parameter $_

  S                          # substitute implicitly on $_, not in-place
  :ignorecase
  :global
  /

    |  \s                    # match any space

    |  (\w)                  # match a word character
       {}                    # make sure $/ is updated (which $0 uses)

       <(                    # ignore everything before this

       [

           <!before "{       # make sure this won't match after this point
             chr $0.ord + 1  # the next ASCII character
           }">

           .                 # any character

       ]+                    # match it at least once

  //                         # remove what matched
}

```

Note that `<!before …>` is a zero width assertion

# Answer
> 0 votes
# Retina, 76 bytes

```

^.
$&$&$&¶
{T`@@L@l`@l@l@`..¶
T`l`L`.¶
(.)(.)((¶).*?(\1|\2)|¶.*)
$5$5$5$4

```

Try it online! Link includes test cases. Explanation:

```

```

Delete spaces.

```
^.
$&$&$&¶

```

Triplicate the first character and insert a separator.

```
{T`@@L@l`@l@l@`..¶
T`l`L`.¶

```

Convert the second and third characters to lower case and increment them. Convert the latter to upper case. These are now the search characters.

```
(.)(.)((¶).*?(\1|\2)|¶.*)
$5$5$5$4

```

Try to match either of the search characters. If found, then triplicate the match, which restarts the loop for the next search. Otherwise, just delete the search characters and the rest of the input.

# Answer
> 0 votes
# 8th, 114 bytes

**Code**

```
: z dup n:1+ 32 bor >r "" swap s:+ . ;
: f s:trim 0 s:@ z ( nip dup 32 bor r@ n:= if rdrop z then ) s:each rdrop ;

```

**Explanation**

```
: z             \ n -- (r: x)
                \ print letter and save on r-stack OR-bitwised ASCII code of following letter
  dup           \ duplicate item on TOS
  n:1+          \ get ASCII code of the following letter
  32 bor        \ bitwise OR of ASCII code and 32
  >r            \ save result on r-stack
  "" swap s:+ . \ print letter
;

: f        \ s --
  s:trim   \ remove trailing whitespace
  0 s:@    \ get 1st letter
  z        \ print 1st letter and save on r-stack OR-bitwised ASCII code of following letter
  ( nip    \ get rid of index
    dup    \ duplicate item on TOS
    32 bor \ bitwise OR of current ASCII code and 32
    r@     \ get value stored on r-stack
    n:=    \ compare values to see if letter is printable or not
    if
      rdrop \ clean r-stack
      z     \ print letter and save on r-stack OR-bitwised ASCII code of following letter
    then
  )
  s:each    \ handle each character in string
  rdrop     \ clean r-stack
;

```

**Example**

```
ok> " The quick red fox jumped over the lazy brown dog" f
Tuvw

```

# Answer
> 0 votes
# C (gcc), ~~79~~ ~~78~~ ~~75~~ 70 bytes

```
c;f(char*s){for(c=0;*s;s++)*s^32&&!c|c==(*s|32)-1?c=32|putchar(*s):0;}

```

Try it online!

# Answer
> 0 votes
# Proton, 59 bytes

Port of the Python 2 submission.

```
s=>reduce((x,y)=>x+y*((ord(y)-ord(x[-1]))%32==1),s.strip())

```

Try it online!

# Answer
> 0 votes
# Pyth, 15 bytes

```
eo,}r0NG_xQhNty

```

Test suite

Unlike all of the other answers, this doesn't stick together the output, it generates all subsequences of the input, and then orders them to put the desired string at the end, and outputs it.

# Answer
> 0 votes
# J, partial solution

I'm posting this for feedback and ideas for improvement more than anything else. It works, but doesn't handle the capitalization and space edge cases, and is already long for J.

First a dyadic helper verb that tells you if the left and right args are alphabetically adjacent:

```
g=.(= <:)&(a.&i.)  NB. could save one char with u:

```

Next a verb that removes the first element which is *not* part of an alphabetic streak starting from the first element:

```
f=.({~<^:3@>:@i.&0@(0,~2&(g/\))) ::]

```

Note we use Adverse `::` to return the entire argument unchanged if there is no non-streak element found (ie, if the entire argument is a valid alphabetic streak).

Finally, the solution is given by applying `f` until convergence:

```
f^:_ 'codegolf'  NB. => 'cdef'

```

Try it online!

---

And here is a parsed version of `f` for easier reading:

```
           ┌─ ~ ─── {
           │                              ┌─ <
           │                       ┌─ ^: ─┴─ 3
           │                 ┌─ @ ─┴─ >:
       ┌───┤           ┌─ @ ─┴─ i.
       │   │     ┌─ & ─┴─ 0
       │   │     │
       │   └─ @ ─┤     ┌─ 0
── :: ─┤         │     ├─ ~ ─── ,
       │         └─────┤
       │               │     ┌─ 2
       │               └─ & ─┴─ \ ─── / ──── g
       └─ ]

```

**Side Question**: why do the box characters not align perfectly when display on SO (they work in my console):

---
Tags: code-golf, string, alphabet
---


title: Minimal taxicab distance map

text:
# Question
Title: Minimal taxicab distance map
# Challenge description

In **taxicab metric**, a distance between two points  is defined as:

Consider a matrix with only zeros and ones:

```
0 0 0 1 0
1 0 0 0 0
0 0 0 0 0
1 0 0 1 0
1 0 0 0 0

```

Let's map each `1` to the distance to nearest *different* `1` in the matrix (of course, assuming that distance between two adjacent rows/columns is equal to 1):

```
0 0 0 3 0
2 0 0 0 0
0 0 0 0 0
1 0 0 3 0
1 0 0 0 0

```

For this challenge, given a matrix, find it's map of distances as shown above.

# Examples

```
0 0 1
0 0 0
0 0 0
0 1 0
0 0 0

0 0 4
0 0 0
0 0 0
0 4 0
0 0 0

-----

0 0 0 0 0 0 1
0 1 0 1 0 0 0
1 1 0 0 0 0 1
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 1 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 1 0 0 1

0 0 0 0 0 0 2
0 1 0 2 0 0 0
1 1 0 0 0 0 2
0 0 2 0 0 0 0
0 0 0 0 0 3 0
0 2 0 0 0 0 0
2 0 0 0 0 0 0
0 0 0 3 0 0 3

-----

1 1 1
1 1 1
1 1 1

1 1 1
1 1 1
1 1 1

-----

1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1

22 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 22

-----

0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

0 0 9 0 0 0 0 0 0 0 0 2 0 0 0 4
0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0

-----
1 1

1 1

```

# Notes

* You may take input in any reasonable format
* Apart from `0` and `1` you may choose any two distinct values
* You may assume that a matrix is non-empty (contains at least one row of length larger or equal to 1), as well that it is rectangular
* You may also assume there are at least two truthy values present in the matrix (otherwise, output is undefined)
* You may write a full program, or a function
* Instead of returning a new matrix, you may modify an existing one
* This is , so make your byte count as low as possible!

# Answer
# Taxi, ~~14652~~ 14572 bytes

Saved 80 bytes by using `plan ?` instead of `plan "?"` throughout. Now it's even more confusing in its golfed version *but* it's 0.55% shorter so that's worth it..

```
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Auctioneer School.Pickup a passenger going to Chop Suey.Go to Auctioneer School:n 1 r.1 is waiting at Starchild Numerology.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l.Pickup a passenger going to Rob's Rest.Pickup a passenger going to Bird's Bench.Go to Rob's Rest:w 1 r 2 l 1 r.Go to Bird's Bench:s.Go to Chop Suey:n 1 r 1 l 2 r 1 r 3 r.[a]Switch to plan d if no one is waiting.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan b if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Rob's Rest:s 1 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Firemouth Grill.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l 1 r 2 l.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to Rob's Rest.Go to Rob's Rest:n 1 r 2 r 1 r.Go to Bird's Bench:s.Pickup a passenger going to Addition Alley.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Cyclone:w 1 l 1 r 2 r.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Bird's Bench.Go to Bird's Bench:n 1 l 1 l 1 l 2 r 1 l.Switch to plan c.[b]Go to Rob's Rest:s 1 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Addition Alley.Pickup a passenger going to Sunny Skies Park.Go to Trunkers:s 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Rob's Rest.Go to Cyclone:n 1 l 1 l.Pickup a passenger going to Bird's Bench.Go to Sunny Skies Park:n 1 r.Go to Rob's Rest:s 2 r 1 r.Go to Bird's Bench:s.[c]Go to Chop Suey:n 1 r 1 l 2 r 1 r 3 r.Switch to plan a.[d]Go to Rob's Rest:n 1 l 3 l 1 l 2 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Auctioneer School:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 4 l.Pickup a passenger going to Auctioneer School.Pickup a passenger going to Chop Suey.Go to Auctioneer School:n 1 r.0 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l.Pickup a passenger going to Addition Alley.Go to Chop Suey:w 1 r 3 r 1 r 3 r.[e]Switch to plan g if no one is waiting.Pickup a passenger going to Cyclone.Go to Zoom Zoom:n 1 l 3 r.Go to Cyclone:w.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:s.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan f if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Cyclone:n 4 l 2 l.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 l 2 l 1 r.Go to Chop Suey:e 1 l 4 r 1 l.Switch to plan e.[f]Go to Cyclone:n 4 l 2 l.Pickup a passenger going to Joyless Park.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 2 l 2 r.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Addition Alley.Go to Joyless Park:n 1 r 1 r 2 l.Go to Chop Suey:w 1 r 1 r 1 l.Switch to plan e.[g]Go to Addition Alley:n 1 l 2 l.Pickup a passenger going to Rounders Pub.Go to Rounders Pub:n 1 r 1 r 2 r 1 l.Go to Joyless Park:n 1 r.Pickup a passenger going to KonKat's.[h]Switch to plan i if no one is waiting.Pickup a passenger going to KonKat's.Go to Zoom Zoom:w 1 r 2 l 2 r.Go to KonKat's:w 1 l 2 r.Pickup a passenger going to KonKat's.Go to Joyless Park:s 2 l.Switch to plan h.[i]Go to KonKat's:w 1 r.Pickup a passenger going to Tom's Trims.Go to Tom's Trims:s 3 r 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 1 l 1 l 2 l.Pickup a passenger going to Joyless Park.Go to Joyless Park:w 1 r 2 r 1 r 2 l 4 r.[j]Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Joyless Park.Pickup a passenger going to The Underground.Go to Joyless Park:n 2 r 2 r 2 l.Go to Tom's Trims:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Chop Suey.Go to Tom's Trims:s 1 l 2 r 1 l.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Go to The Underground:s 1 r 1 l.[k]Switch to plan l if no one is waiting.Pickup a passenger going to The Underground.Go to Chop Suey:n 2 r 1 l.Pickup a passenger going to Firemouth Grill.Go to Zoom Zoom:n 1 l 3 r.Go to Firemouth Grill:w 3 l 2 l 1 r.Go to The Underground:e 1 l.Switch to plan k.[l]Go to Chop Suey:n 2 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Go to Chop Suey:n 6 r 1 l.[m]Switch to plan o if no one is waiting.Pickup a passenger going to Firemouth Grill.Switch to plan n if no one is waiting.Pickup a passenger going to Firemouth Grill.Switch to plan n if no one is waiting.Pickup a passenger going to Firemouth Grill.[n]Go to Zoom Zoom:n 1 l 3 r.Go to Firemouth Grill:w 3 l 2 l 1 r.Go to Chop Suey:e 1 l 4 r 1 l.Switch to plan m.[o]Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to The Underground.Go to The Underground:n.Switch to plan p if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.Go to Joyless Park:e 1 l 3 r.Switch to plan q.[p]' 0' is waiting at Writer's Depot.Go to Writer's Depot:s 2 r 1 l 2 l.Pickup a passenger going to Heisenberg's.Go to Heisenberg's:n 3 r 3 r.Go to Joyless Park:s 1 r 1 l 1 l.Switch to plan 4.[q]Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Joyless Park.Pickup a passenger going to The Underground.Go to Joyless Park:n 2 r 2 r 2 l.Go to Trunkers:w 1 l 2 r 1 l.[r]Pickup a passenger going to Cyclone.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l.Pickup a passenger going to Trunkers.Pickup a passenger going to Rob's Rest.Go to Trunkers:s 1 l.Go to Rob's Rest:w 1 l 1 r 1 r.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to Bird's Bench.Go to Zoom Zoom:n.Go to Sunny Skies Park:w 2 l.Go to Bird's Bench:s 2 r 1 l.Go to The Underground:n 1 r 1 l 1 r 1 r 2 l.Switch to plan s if no one is waiting.Pickup a passenger going to The Underground.Go to Rob's Rest:s 2 r 1 l 1 l 1 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Trunkers:w 1 l 1 r.Switch to plan r.[s]Go to Rounders Pub:n 1 l 1 l.Pickup a passenger going to Cyclone.Go to Joyless Park:n 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to Rounders Pub.Pickup a passenger going to What's The Difference.Pickup a passenger going to Joyless Park.Go to Rounders Pub:n 2 r 2 r 2 r 1 l.Go to Joyless Park:n 1 r.Go to Cyclone:w 1 r 2 l 2 l.Pickup a passenger going to What's The Difference.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 2 l 2 r.Pickup a passenger going to Addition Alley.Go to What's The Difference:w 1 r 3 r 1 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:e 2 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.[t]Switch to plan u if no one is waiting.Pickup a passenger going to The Underground.Go to Trunkers:s 2 r 1 l.Pickup a passenger going to Trunkers.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Sunny Skies Park.Go to Zoom Zoom:n 1 r.Go to The Underground:w 1 l 2 r.Switch to plan t.[u]Go to Trunkers:s 2 r 1 l.Go to Sunny Skies Park:w 1 r.Go to Tom's Trims:s 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 r 1 l 2 r.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Chop Suey.Go to Tom's Trims:s 1 l 2 r 1 l.Go to Chop Suey:n 1 r 1 l 4 r 1 l.[v]Switch to plan 0 if no one is waiting.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to The Underground.Go to The Underground:n.Switch to plan z if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.Go to Trunkers:w 1 l 1 r.Pickup a passenger going to Cyclone.Go to Rob's Rest:w 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Trunkers.Pickup a passenger going to What's The Difference.Pickup a passenger going to Rob's Rest.Go to Trunkers:s 1 l.Go to Rob's Rest:w 1 l 1 r 1 r.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 l 1 r 2 l.Pickup a passenger going to Addition Alley.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to What's The Difference.Go to Zoom Zoom:n.Go to What's The Difference:w 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Multiplication Station.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan w if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 r 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to The Underground:w 1 r 2 r 1 r 2 l.[w]Go to Multiplication Station:s 1 l 1 r.Go to Sunny Skies Park:s 1 r 2 l 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s 2 r 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Pickup a passenger going to Bird's Bench.Go to Sunny Skies Park:n 1 r.Go to Bird's Bench:s 2 r 1 l.1 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 r 1 r 2 l.Pickup a passenger going to Addition Alley.Go to Cyclone:w 1 r 4 l.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 l.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Multiplication Station.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan x if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 r.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 r 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to The Underground:w 1 r 2 r 1 r 2 l.[x]Go to Multiplication Station:s 1 l 1 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Addition Alley.-1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 2 l 1 l 2 l.Pickup a passenger going to Multiplication Station.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 1 r 3 l 1 r.Pickup a passenger going to Addition Alley.1 is waiting at Starchild Numerology.Go to Starchild Numerology:s 1 r 2 l 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to The Underground.Go to The Underground:n 1 r 1 r.Switch to plan y if no one is waiting.Pickup a passenger going to Narrow Path Park.[y]Go to Narrow Path Park:n 4 l.Go to Chop Suey:e 1 r 1 l 1 r.Switch to plan v.[z]Go to Trunkers:s 2 r 1 l.Pickup a passenger going to Trunkers.Go to Sunny Skies Park:w 1 r.Pickup a passenger going to Sunny Skies Park.Go to Zoom Zoom:n 1 r.Go to Trunkers:w 3 l.Go to Sunny Skies Park:w 1 r.Go to Chop Suey:n 1 r 1 r 3 r.Switch to plan v.[0]Go to Rob's Rest:n 1 l 3 l 1 l 2 r 1 r.Pickup a passenger going to Firemouth Grill.Go to Bird's Bench:s.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 r 1 l 1 r 1 l 1 r.Go to Narrow Path Park:e 1 l 4 l.Pickup a passenger going to Writer's Depot.[1]Switch to plan 3 if no one is waiting.Pickup a passenger going to Cyclone.Go to Zoom Zoom:w 1 l 1 r 1 r.Go to Writer's Depot:w.Pickup a passenger going to Magic Eight.Go to Cyclone:n.Pickup a passenger going to Magic Eight.Go to Magic Eight:s 1 l 2 r.Switch to plan 2 if no one is waiting.Pickup a passenger going to Writer's Depot.Go to Cyclone:n 1 l 2 r.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:s 1 l 2 l 1 r.Go to Narrow Path Park:e 1 l 4 l.Switch to plan 1.[2]Go to Cyclone:n 1 l 2 r.Pickup a passenger going to Writer's Depot.Go to Narrow Path Park:n 2 r 1 l 1 r.Switch to plan 1.[3]Go to Writer's Depot:w 1 l 1 r 2 l.Pickup a passenger going to The Babelfishery.' ' is waiting at Writer's Depot.Pickup a passenger going to KonKat's.Go to The Babelfishery:n 1 r 2 r 1 r.Pickup a passenger going to KonKat's.Go to KonKat's:n.Pickup a passenger going to Heisenberg's.Go to Heisenberg's:n 1 r 1 r.Go to Joyless Park:s 1 r 1 l 1 l.[4]Pickup a passenger going to Magic Eight.Go to Rounders Pub:w 2 l.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 r 1 l 2 l 2 l.Pickup a passenger going to Rounders Pub.Pickup a passenger going to Magic Eight.Go to Rounders Pub:n 2 r 2 r 2 r 1 l.Go to Magic Eight:n 1 r 1 r 2 r.Switch to plan 5 if no one is waiting.Pickup a passenger going to Addition Alley.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 1 l 2 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 3 r 1 r 1 r.Pickup a passenger going to Joyless Park.Go to Joyless Park:n 1 r 1 r 2 l.Switch to plan j.[5]Go to Auctioneer School:w 1 l 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 3 r 1 r 3 r.[6]Switch to plan 9 if no one is waiting.Pickup a passenger going to Crime Lab.'|' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Zoom Zoom:n.Go to Crime Lab:w 1 l 2 r.Switch to plan 7 if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:n 1 l.'\n' is waiting at Writer's Depot.Go to Writer's Depot:w 1 l 1 r 2 l.Pickup a passenger going to KonKat's.Go to KonKat's:n 3 r 2 r.Switch to plan 8.[7]Go to Heisenberg's:n 5 r 1 l.Pickup a passenger going to KonKat's.Go to KonKat's:n 1 l 1 l.[8]Pickup a passenger going to KonKat's.Go to Chop Suey:n 1 r 1 r.Switch to plan 6.[9]Go to KonKat's:n 1 l 1 l.Pickup a passenger going to Post Office.Go to Post Office:s 3 r 1 l.

```

Try it online!

I'm presuming that no human wants to try to read through that code so try it online with comments, meaningful labels, and more readable output!

---

**Input:** String representing an array. The string must be made up of 3 characters: `0`, `|` representing row breaks, and any positive digit greater than one (PDGTO). I like to use `8`. OP allows input in any reasonable format and I believe this qualifies. The string does not *have* to format to a rectangular array but it will be assumed to be left-aligned whatever it is.

**Output:** Array of values showing the minimum taxi cab distance from each `PDGTO` to its nearest other `PDGTO`.

**Example:**

```
OP's Input:         OP's Output:
-------------       -------------
0 0 0 0 0 0 1       0 0 0 0 0 0 2
0 1 0 1 0 0 0       0 1 0 2 0 0 0
1 1 0 0 0 0 1       1 1 0 0 0 0 2
0 0 1 0 0 0 0       0 0 2 0 0 0 0
0 0 0 0 0 1 0       0 0 0 0 0 3 0
0 1 0 0 0 0 0       0 2 0 0 0 0 0
1 0 0 0 0 0 0       2 0 0 0 0 0 0
0 0 0 1 0 0 1       0 0 0 3 0 0 3

Taxi Input: 0000008|0808000|8800008|0080000|0000080|0800000|8000000|0008008

Taxi Output: (golfed)
------------------------------------
 0 0 0 0 0 0 2.000000
 0 1.000000 0 2.000000 0 0 0
 1.000000 1.000000 0 0 0 0 2.000000
 0 0 2.000000 0 0 0 0
 0 0 0 0 0 3.000000 0
 0 2.000000 0 0 0 0 0
 2.000000 0 0 0 0 0 0
 0 0 0 3.000000 0 0 3.000000

Taxi Output: (ungolfed version)
------------------------------------
 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 2.000000
 0.000000 1.000000 0.000000 2.000000 0.000000 0.000000 0.000000
 1.000000 1.000000 0.000000 0.000000 0.000000 0.000000 2.000000
 0.000000 0.000000 2.000000 0.000000 0.000000 0.000000 0.000000
 0.000000 0.000000 0.000000 0.000000 0.000000 3.000000 0.000000
 0.000000 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000
 2.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
 0.000000 0.000000 0.000000 3.000000 0.000000 0.000000 3.000000

```

The second output format is clearly much easier to read but it also adds 7 bytes to the program and this *is* , after all. It would have been possible to go back at the end and remove all the trailing zeroes instead of simply padding the `0` values but that would have added even more bytes and was deemed an unacceptable cost.

---

Here's the text of the un-golfed code with comments, better labels, and clearer output:
(It's the same as you get with the link above.)

```
[ Minimal Taxi Cab Distance ]
[ Inspired by: https://codegolf.stackexchange.com/q/138311 ]

[ INPUT:  String of 8 & 0 using a pipe | as a delineator for each row of the array                  ]
[ OUTPUT: String formatted as an array showing min taxi distance from each 8 to the nearest other 8 ]
[ Note:   Any single digit > 1 may be substituted for 8. I.E., it must be 2 <= i <= 9               ]

[ PROCESS:                                                                 ]
[ Store STDIN exactly as input and keep it around until the end            ]
[ Store 3 arrays: Input, X Positions, Y Positions                          ]
[ Store a count of how many elements are in Input                          ]
[ For each element in Input, create an array of minimal taxi cab distances ]
[ Find the minimum value greater than zero in that array and store it      ]
[ Pad with a space and, if it's the last element in the row, a line break  ]
[ Repeat for each element, storing the minimums each time                  ]
[ Concatenate all the results and output                                   ]

[ DATA                     LOCATION              REASON                    ]
[ Input                    Auctioneer School     Close to Cyclone          ]
[ Input w/o Delineator     Tom's Trims           Running out of options    ]
[ Elements in Input        Rounders Pub          Close to Joyless Park     ]
[ Iteration                Joyless Park          Close to The Underground  ]
[ X Positions              Trunkers              Close to Rob's Rest       ]
[ Y Positions              Sunny Skies Park      Close to Bird's Bench     ]
[ Element's X              Rob's Rest            Easy to remember          ]
[ Element's Y              Bird's Bench          Easy to remember          ]
[ Array of distances       Narrow Path Park      LIFO is OK for this       ]
[ Array of minimums        Heisenberg's          Close to KonKat's         ]

[ Note: We use the Input w/o Delineator to keep track of iteration             ]
[ By storing it at Chop Suey, we can keep going until we run out of passengers ]

[ Note: When using arrays other than the Input w/o Delineator, is it critical  ]
[ that we clone each element and enque it again. The iteration tracking will   ]
[ let us stop before we go through the array again.                            ]

[ Some things I learned about Taxi in TIO while writing this:                            ]
[  - You can store any passenger at Writer's Depot and Starchild Numerology              ]
[  - You can store any passenger at Heisenberg's and even pick them up again *in order*  ]
[    (You don't start picking up random integers until that queue is gone)               ]
[  - If you try to drop someone at Rob's Rest or Bird's Bench but it's already occupied, ]
[    it won't error out. The passenger just stays in the Taxi which will probably cause  ]
[    a "Too Many Passengers" error eventually. It cost me a week figuring that one out.  ]

[ Pickup stdin and split it into elements ]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Auctioneer School.
Pickup a passenger going to Chop Suey.
Go to Auctioneer School: north 1st right.
1 is waiting at Starchild Numerology.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left.
Pickup a passenger going to Rob's Rest.
Pickup a passenger going to Bird's Bench.
Go to Rob's Rest: west 1st right 2nd left 1st right.
Go to Bird's Bench: south.
Go to Chop Suey: north 1st right 1st left 2nd right 1st right 3rd right.

[ Create the arrays of X & Y positions ]
[NextInputElement]
Switch to plan "XYCreated" if no one is waiting.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "NextColumn" if no one is waiting.

[ Next Row ]
[ Set X = 1 and Y = Y + 1 ]
Pickup a passenger going to Firemouth Grill.
Go to Rob's Rest: south 1st right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Firemouth Grill.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left 1st right 2nd left.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to Rob's Rest.
Go to Rob's Rest: north 1st right 2nd right 1st right.
Go to Bird's Bench: south.
Pickup a passenger going to Addition Alley.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.
Go to Cyclone: west 1st left 1st right 2nd right.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to Bird's Bench.
Go to Bird's Bench: north 1st left 1st left 1st left 2nd right 1st left.
Switch to plan "EndOfElement".

[NextColumn]
[ Set X = X + 1, Store X & Y positions ]
Go to Rob's Rest: south 1st right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Sunny Skies Park.
Go to Trunkers: south 1st left.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Rob's Rest.
Go to Cyclone: north 1st left 1st left.
Pickup a passenger going to Bird's Bench.
Go to Sunny Skies Park: north 1st right.
Go to Rob's Rest: south 2nd right 1st right.
Go to Bird's Bench: south.

[EndOfElement]
Go to Chop Suey: north 1st right 1st left 2nd right 1st right 3rd right.
Switch to plan "NextInputElement".

[XYCreated]
[ Clean up the leftovers ]
Go to Rob's Rest: north 1st left 3rd left 1st left 2nd right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.

[ Create the Input w/o Delineator string ]
Go to Auctioneer School: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 4th left.
Pickup a passenger going to Auctioneer School.
Pickup a passenger going to Chop Suey.
Go to Auctioneer School: north 1st right.
0 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left.
Pickup a passenger going to Addition Alley.
Go to Chop Suey: west 1st right 3rd right 1st right 3rd right.

[NextInputWithoutDelineatorElement]
Switch to plan "DelineatorRemoved" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: north 1st left 3rd right.
Go to Cyclone: west.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: south.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "NotAPipe" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Cyclone: north 4th left 2nd left.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st left 2nd left 1st right.
Go to Chop Suey: east 1st left 4th right 1st left.
Switch to plan "NextInputWithoutDelineatorElement".

[NotAPipe]
Go to Cyclone: north 4th left 2nd left.
Pickup a passenger going to Joyless Park.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 2nd left 2nd right.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Addition Alley.
Go to Joyless Park: north 1st right 1st right 2nd left.
Go to Chop Suey: west 1st right 1st right 1st left.
Switch to plan "NextInputWithoutDelineatorElement".

[DelineatorRemoved]
Go to Addition Alley: north 1st left 2nd left.
Pickup a passenger going to Rounders Pub.
Go to Rounders Pub: north 1st right 1st right 2nd right 1st left.
Go to Joyless Park: north 1st right.
Pickup a passenger going to KonKat's.

[NextBit]
Switch to plan "EndOfBits" if no one is waiting.
Pickup a passenger going to KonKat's.
Go to Zoom Zoom: west 1st right 2nd left 2nd right.
Go to KonKat's: west 1st left 2nd right.
Pickup a passenger going to KonKat's.
Go to Joyless Park: south 2nd left.
Switch to plan "NextBit".

[EndOfBits]
Go to KonKat's: west 1st right.
Pickup a passenger going to Tom's Trims.
Go to Tom's Trims: south 3rd right 1st left.

[ Start with the first character ]
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 1st left 1st left 2nd left.
Pickup a passenger going to Joyless Park.
Go to Joyless Park: west 1st right 2nd right 1st right 2nd left 4th right.

[ At this point, we have the following data structure: ]
[ Input                           Auctioneer School    ]
[ Input w/o Delineator (IwoD)     Tom's Trims          ]
[ Elements in Input               Rounders Pub         ]
[ Iteration = 1                   Joyless Park         ]
[ X Positions                     Trunkers             ]
[ Y Positions                     Sunny Skies Park     ]
[                                                      ]
[ Now, we use Joyless Park to track our starting point ]
[ and IwoD to iterate through all the destinations. If ]
[ the starting point's value is <=1, then add '0 ' to  ]
[ Heisenberg's and go to the next element. If value    ]
[ is >1, then calculate the distance to every other    ]
[ element. IwoD will start full and be emptied as we   ]
[ iterate through all the possible destinations. Once  ]
[ that's done, cycle back through those results until  ]
[ we find the smallest value >0 and add that to        ]
[ Heisenberg's. Either way, in order to move to        ]
[ the next starting point, we add 1 to Joyless Park,   ]
[ check that it's less than Rounders Pub, and loop. If ]
[ it's not less, then we're done so we can go build    ]
[ the output with the passengers at Heisenberg's.      ]
[ We'll use the Auctioneer School to find pipes so we  ]
[ can add line breaks to the output as needed.         ]

[NextElementForStartingPoint]

[ Extract this element's value ]
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Joyless Park.
Pickup a passenger going to The Underground.
Go to Joyless Park: north 2nd right 2nd right 2nd left.
Go to Tom's Trims: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Chop Suey.
Go to Tom's Trims: south 1st left 2nd right 1st left.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.
Go to The Underground: south 1st right 1st left.

[GetTheNextElement]
Switch to plan "FoundThisElement" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Chop Suey: north 2nd right 1st left.
Pickup a passenger going to Firemouth Grill.
Go to Zoom Zoom: north 1st left 3rd right.
Go to Firemouth Grill: west 3rd left 2nd left 1st right.
Go to The Underground: east 1st left.
Switch to plan "GetTheNextElement".

[FoundThisElement]
Go to Chop Suey: north 2nd right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st right 1st left.
Go to Chop Suey: north 6th right 1st left.

[TrashNextElement]
Switch to plan "ElementsAllTrashed" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Switch to plan "AllRiders" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Switch to plan "AllRiders" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
[AllRiders]
Go to Zoom Zoom: north 1st left 3rd right.
Go to Firemouth Grill: west 3rd left 2nd left 1st right.
Go to Chop Suey: east 1st left 4th right 1st left.
Switch to plan "TrashNextElement".

[ElementsAllTrashed]
Go to The Babelfishery: south 1st right 1st left.
Pickup a passenger going to The Underground.
Go to The Underground: north.
Switch to plan "ItsAZero" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
Go to Joyless Park: east 1st left 3rd right.
Switch to plan "ExtractXY".

[ItsAZero]
' 0.000000' is waiting at Writer's Depot.
Go to Writer's Depot: south 2nd right 1st left 2nd left.
Pickup a passenger going to Heisenberg's.
Go to Heisenberg's: north 3rd right 3rd right.
Go to Joyless Park: south 1st right 1st left 1st left.
Switch to plan "GotoNextStartingPoint".

[ Extract the X & Y values for this element ]
[ExtractXY]
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Joyless Park.
Pickup a passenger going to The Underground.
Go to Joyless Park: north 2nd right 2nd right 2nd left.
Go to Trunkers: west 1st left 2nd right 1st left.

[GetNextXY]
Pickup a passenger going to Cyclone.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Rob's Rest.
Go to Trunkers: south 1st left.
Go to Rob's Rest: west 1st left 1st right 1st right.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to Bird's Bench.
Go to Zoom Zoom: north.
Go to Sunny Skies Park: west 2nd left.
Go to Bird's Bench: south 2nd right 1st left.
Go to The Underground: north 1st right 1st left 1st right 1st right 2nd left.
Switch to plan "FoundXY" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Rob's Rest: south 2nd right 1st left 1st left 1st right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.
Go to Trunkers: west 1st left 1st right.
Switch to plan "GetNextXY".

[FoundXY]
[ Keep rotating the arrays until they're back to normal ]
Go to Rounders Pub: north 1st left 1st left.
Pickup a passenger going to Cyclone.
Go to Joyless Park: north 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to Rounders Pub.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Joyless Park.
Go to Rounders Pub: north 2nd right 2nd right 2nd right 1st left.
Go to Joyless Park: north 1st right.
Go to Cyclone: west 1st right 2nd left 2nd left.
Pickup a passenger going to What's The Difference.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 2nd left 2nd right.
Pickup a passenger going to Addition Alley.
Go to What's The Difference: west 1st right 3rd right 1st left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: east 2nd right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.

[RotateXY]
Switch to plan "XYareRotated" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Trunkers: south 2nd right 1st left.
Pickup a passenger going to Trunkers.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Sunny Skies Park.
Go to Zoom Zoom: north 1st right.
Go to The Underground: west 1st left 2nd right.
Switch to plan "RotateXY".

[XYareRotated]
[ At this point, we have the following data structure: ]
[ Input                           Auctioneer School    ]
[ Input w/o Delineator (IwoD)     Tom's Trims          ]
[ Elements in Input               Rounders Pub         ]
[ Iteration                       Joyless Park         ]
[ X Positions                     Trunkers             ]
[ Y Positions                     Sunny Skies Park     ]
[ Element's X                     Rob's Rest           ]
[ Element's Y                     Bird's Bench         ]

[ Now we need to compute the distance to each point greater than one ]
[ IwoD will start full and be emptied as we iterate through all the  ]
[ possible destinations. Once that's done, cycle back through those  ]
[ results until we find the smallest value >0 and add that to the    ]
[ result at Heisenberg's.                                            ]

[ Finish the RotateXY process by dropping off the passengers ]
[ They have been carried around to raise money for gas       ]
Go to Trunkers: south 2nd right 1st left.
Go to Sunny Skies Park: west 1st right.

[ Split IwoD into pieces ]
Go to Tom's Trims: south 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st right 1st left 2nd right.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Chop Suey.
Go to Tom's Trims: south 1st left 2nd right 1st left.
Go to Chop Suey: north 1st right 1st left 4th right 1st left.

[NextDestination]
Switch to plan "EndOfDestinations" if no one is waiting.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st right 1st left.
Pickup a passenger going to The Underground.
Go to The Underground: north.
Switch to plan "DestinationIsZero" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.

[ Calculate the X distance ]
Go to Trunkers: west 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Rob's Rest: west 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Trunkers.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Rob's Rest.
Go to Trunkers: south 1st left.
Go to Rob's Rest: west 1st left 1st right 1st right.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st left 1st right 2nd left.
Pickup a passenger going to Addition Alley.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to What's The Difference.
Go to Zoom Zoom: north.
Go to What's The Difference: west 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "XDiffIsNegative" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st right 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to The Underground: west 1st right 2nd right 1st right 2nd left.
[XDiffIsNegative]
Go to Multiplication Station: south 1st left 1st right.

[ Calculate the Y distance ]
Go to Sunny Skies Park: south 1st right 2nd left 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south 2nd right 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Bird's Bench.
Go to Sunny Skies Park: north 1st right.
Go to Bird's Bench: south 2nd right 1st left.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: north 1st right 1st right 2nd left.
Pickup a passenger going to Addition Alley.
Go to Cyclone: west 1st right 4th left.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: north 1st left.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st right.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "YDiffIsNegative" if no one is waiting.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st right.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st right 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to The Underground: west 1st right 2nd right 1st right 2nd left.
[YDiffIsNegative]
Go to Multiplication Station: south 1st left 1st right.

[ Add the two negative distances and multiply by -1 ]
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Addition Alley.
-1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 2nd left 1st left 2nd left.
Pickup a passenger going to Multiplication Station.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: north 1st right 1st right 3rd left 1st right.

[ Do not record the distance if it's zero     ]
[ i.e., if start and destination are the same ]
Pickup a passenger going to Addition Alley.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: south 1st right 2nd left 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to The Underground.
Go to The Underground: north 1st right 1st right.
Switch to plan "IgnoreZeroDistance" if no one is waiting.
Pickup a passenger going to Narrow Path Park.
[IgnoreZeroDistance]
Go to Narrow Path Park: north 4th left.
Go to Chop Suey: east 1st right 1st left 1st right.
Switch to plan "NextDestination".

[DestinationIsZero]
[ Rotate X & Y and keep going ]
Go to Trunkers: south 2nd right 1st left.
Pickup a passenger going to Trunkers.
Go to Sunny Skies Park: west 1st right.
Pickup a passenger going to Sunny Skies Park.
Go to Zoom Zoom: north 1st right.
Go to Trunkers: west 3rd left.
Go to Sunny Skies Park: west 1st right.
Go to Chop Suey: north 1st right 1st right 3rd right.
Switch to plan "NextDestination".

[EndOfDestinations]
[ Clean up the leftovers ]
Go to Rob's Rest: north 1st left 3rd left 1st left 2nd right 1st right.
Pickup a passenger going to Firemouth Grill.
Go to Bird's Bench: south.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st right 1st left 1st right 1st left 1st right.

[ Run through all the passengers are Narrow Path Park  ]
[ None of them should be 0 because we dropped those    ]
[ Find the smallest value and store it at Heisenberg's ]
Go to Narrow Path Park: east 1st left 4th left.
Pickup a passenger going to Writer's Depot.

[CheckNextDistance]
Switch to plan "FoundMinDistance" if no one is waiting.
Pickup a passenger going to Cyclone.
Go to Zoom Zoom: west 1st left 1st right 1st right.
Go to Writer's Depot: west.
Pickup a passenger going to Magic Eight.
Go to Cyclone: north.
Pickup a passenger going to Magic Eight.
Go to Magic Eight: south 1st left 2nd right.
Switch to plan "SecondIsLess" if no one is waiting.

[ Keep the first passenger b/c it's less and dump the second ]
Pickup a passenger going to Writer's Depot.
Go to Cyclone: north 1st left 2nd right.
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: south 1st left 2nd left 1st right.
Go to Narrow Path Park: east 1st left 4th left.
Switch to plan "CheckNextDistance".

[SecondIsLess]
[ Go pickup the second passenger's clone b/c it's less or equal ]
Go to Cyclone: north 1st left 2nd right.
Pickup a passenger going to Writer's Depot.
Go to Narrow Path Park: north 2nd right 1st left 1st right.
Switch to plan "CheckNextDistance".

[FoundMinDistance]
[ Store the results ]
Go to Writer's Depot: west 1st left 1st right 2nd left.
Pickup a passenger going to The Babelfishery.
' ' is waiting at Writer's Depot.
Pickup a passenger going to KonKat's.
Go to The Babelfishery: north 1st right 2nd right 1st right.
Pickup a passenger going to KonKat's.
Go to KonKat's: north.
Pickup a passenger going to Heisenberg's.
Go to Heisenberg's: north 1st right 1st right.
Go to Joyless Park: south 1st right 1st left 1st left.

[GotoNextStartingPoint]
[ Start at Joyless Park ]
Pickup a passenger going to Magic Eight.
Go to Rounders Pub: west 2nd left.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st right 1st left 2nd left 2nd left.
Pickup a passenger going to Rounders Pub.
Pickup a passenger going to Magic Eight.
Go to Rounders Pub: north 2nd right 2nd right 2nd right 1st left.
Go to Magic Eight: north 1st right 1st right 2nd right.
Switch to plan "GoDoOutput" if no one is waiting.

[ We're not done yet so start over ]
Pickup a passenger going to Addition Alley.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: west 1st right 3rd right 1st right 1st right.
Pickup a passenger going to Joyless Park.
Go to Joyless Park: north 1st right 1st right 2nd left.
Switch to plan "NextElementForStartingPoint".

[ At this point, we have the following data structure: ]
[ (The queues not listed here aren't needed any more)  ]
[ Input                           Auctioneer School    ]
[ Array of minimums               Heisenberg's         ]

[GoDoOutput]
[ Split the original input at Chop Suey ]
Go to Auctioneer School: west 1st left 1st left.
Pickup a passenger going to Chop Suey.
Go to Chop Suey: north 3rd right 1st right 3rd right.

[OutputNextResult]
Switch to plan "PrintOutput" if no one is waiting.
Pickup a passenger going to Crime Lab.
'|' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 3rd left.
Pickup a passenger going to Crime Lab.
Go to Zoom Zoom: north.
Go to Crime Lab: west 1st left 2nd right.
Switch to plan "OutputThisResult" if no one is waiting.

[ Output line breaks where every pipe was in the input ]
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: north 1st left.
'\n' is waiting at Writer's Depot.
Go to Writer's Depot: west 1st left 1st right 2nd left.
Pickup a passenger going to KonKat's.
Go to KonKat's: north 3rd right 2nd right.
Switch to plan "GoToNextOutput".

[OutputThisResult]
[ Output the actual result, which will include space padding ]
Go to Heisenberg's: north 5th right 1st left.
Pickup a passenger going to KonKat's.
Go to KonKat's: north 1st left 1st left.

[GoToNextOutput]
Pickup a passenger going to KonKat's.
Go to Chop Suey: north 1st right 1st right.
Switch to plan "OutputNextResult".

[PrintOutput]
Go to KonKat's: north 1st left 1st left.
Pickup a passenger going to Post Office.
Go to Post Office: south 3rd right 1st left.

```
> 9 votes

# Answer
# MATL, ~~23~~ 22 bytes

```
tt0*&v&fht1&ZPtg/X<yf(

```

Try it online! Or verify all test cases.

### Explanation

To see intermediate results, it may be helpful to insert `X#` (display stack) between any two statments.

```
t      % Implicit input: matrix. Duplicate
t0*    % Duplicate and multiply by 0. Gives a matrix of zeros
&v     % Concatenate vertically to input matrix. This is needed
       % in case the input is a row vector, because in that case the
       % subsequent &f would give row vectors, not column vectors
&f     % Push two column vectors with the row and column indices of
       % nonzero elements
h      % Concatenate into a 2-column matrix. Each row gives the
       % coordinates of a nonzero entry in the input matrix
t      % Duplicate
1&ZP   % Taxicab distance between the two copies of the 2-column
       % matrix containing the coordinates. The diagonal contains
       % zeros (distance of each point to itself)
tg     % Duplicate, convert to logical (turn nonzeros into 1)
/      % Divide, element-wise. This leaves off-diagonal entries in
       % the distance matrix unchanged, and sets the diagonal to NaN
X<     % Minimum of each column. NaN values are ignored. The
       % resulting vector contains the new values to be written in
       % the input matrix
y      % Duplicate from below: push input matrix again
f      % Linear indices of nonzero entries
(      % Assignment indexing: write specified values at specified
       % positions. Implicitly display

```
> 5 votes

# Answer
# Python 3 \+ numpy, 102 bytes

```
def f(M):
 x,y=where(M)
 for i,j in zip(x,y):d=abs(x-i)+abs(y-j);M[i,j]=min(d[d>0])
from numpy import*

```

Try it online!

Takes input as a `numpy` array.

---

# Python 3, 127 bytes

```
lambda M,e=enumerate:[[v*min(abs(x-i)+abs(y-j)for x,r in e(M)for y,a in e(r)if(x!=i or y!=j)*a)for j,v in e(r)]for i,r in e(M)]

```

Try it online!
> 4 votes

# Answer
# Perl 5, 197 bytes

```
@a=map{[/\d/g]}<>;for$r(0..$#a){for$c(0..$#{$a[$r]}){$d=@a*@{$a[0]}*$a[$r][$c];for$y(0..$#a){$a[$r][$c]=$d=$a[$y][$_]&&($t=abs($y-$r)+abs($_-$c))&&$t<$d?$t:$d for 0..$#{$a[$y]}}}}$,=$";say@$_ for@a

```

Try it online!
> 2 votes

# Answer
## JavaScript (ES6), 97 bytes

Takes input in currying syntax `(w)(a)`, where ***w*** is the width of the matrix and ***a*** is a flat array of zeros and ones. Returns another flat array.

```
w=>a=>a.map((c,i)=>c*Math.min(...a.map((d,j)=>d&&A((j/w|0)-(i/w|0))+A(j%w-i%w)||1/0)),A=Math.abs)

```

### Test cases

This snippet includes a helper function to format the output for readability.

```
let f =

w=>a=>a.map((c,i)=>c*Math.min(...a.map((d,j)=>d&&A((j/w|0)-(i/w|0))+A(j%w-i%w)||1/0)),A=Math.abs)

format = (a, w) => `${a}`.match(RegExp(`(\\d+,?){${w}}`, 'g')).join('\n')

console.log(
  format(f(3)([
    0, 0, 1,
    0, 0, 0,
    0, 0, 0,
    0, 1, 0,
    0, 0, 0
  ]),
  3)
)
console.log(
  format(f(7)([
    0, 0, 0, 0, 0, 0, 1,
    0, 1, 0, 1, 0, 0, 0,
    1, 1, 0, 0, 0, 0, 1,
    0, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 0,
    0, 1, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 1
  ]),
  7)
)
console.log(
  format(f(3)([
    1, 1, 1,
    1, 1, 1,
    1, 1, 1
  ]),
  3)
)
console.log(
  format(f(12)([
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
  ]),
  12)
)
console.log(
  format(f(16)([
    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
  ]),
  16)
)
console.log(
  format(f(2)([
    1, 1
  ]),
  2)
)
```
> 2 votes

# Answer
# Python 3 with Numpy, ~~106~~ 101 bytes

*5 bytes off thanks to @notjagan*

```
from numpy import*
def f(x):i,j=where(x);d=abs(i-i[None].T)+abs(j-j[None].T);x[i,j]=nanmin(d/(d>0),0)

```

Function that inputs a Numpy array and outputs by modifying that array (which is allowed by default).

Try it online!
> 1 votes

# Answer
# Octave, 59 bytes

```
for f=find(a=input(''))'a(f)=0;a(f)=bwdist(a,'ci')(f);end;a

```

Takes the input as a 2D array.

Try it online!

Explanation:

For each location of 1s found in the array set it to zero and calculate city block distance transform of the array and finally set the location to the calculated distance.
> 1 votes

# Answer
# Jelly, 27 bytes

```
T€,€"JẎ
ZJpJạþÇḅ1Zḟ€0Ṃ€ṁZZ×

```

Try it online!
> 1 votes

---
Tags: code-golf, matrix, binary-matrix
---