Bathroom Etiquette, when pertaining to the available urinals, states that the next urinal to be filled in should be the one that minimizes the total discomfort. The total discomfort equation is given by the following set of equations:

dist(x,y) = linear distance between person x and person y in Urinal Units
discomfort(x) = sum(1/(dist(x,y)*dist(x,y))) for all persons y excluding person x
total_Discomfort = sum(discomfort(x)) for all x

A more in depth paper dealing with a similar (not the exact same) problem can be found here: (Thanks to @Lembik for alerting me to this amazing whitepaper!)


Given an input of a empty and full urinals, output the resulting set of urinals with the addition of one person. If there is a tie for a position the urinals should fill in left to right. The output should be in the same format as the input.

  • If given a case with full urinals, return the input.
  • The input will always have at least one urinal defined.

Test Cases

1000001   -> 1001001
101010101 -> 111010101
100       -> 101
00000     -> 10000
1111111   -> 1111111
0100      -> 0101
101000    -> 101001


This is , so shortest code in bytes wins. Standard loop-holes are forbidden.

  • 2
    \$\begingroup\$ Related problem: codegolf.stackexchange.com/questions/47952/the-urinal-protocol \$\endgroup\$ – hobbs Nov 1 '16 at 3:42
  • 1
    \$\begingroup\$ It is recommended to wait about a week before accepting an answer. Accepting in less than a day might decrease the amount of answers your challenge receives. \$\endgroup\$ – Emigna Nov 1 '16 at 15:05
  • 1
    \$\begingroup\$ I'd suggest adding 0100 and 101000 in the test cases (some regex-based approach work on the actual test cases but won't work on those ones which should still be handled) \$\endgroup\$ – Dada Nov 1 '16 at 15:07
  • \$\begingroup\$ also related: codegolf.stackexchange.com/questions/94074/… \$\endgroup\$ – Titus Nov 2 '16 at 14:02
  • \$\begingroup\$ @TheBitByte How is it offensive? It's a pretty accurate description of how men choose urinals in a bathroom. \$\endgroup\$ – mbomb007 Nov 11 '16 at 22:50

Jelly, 13 12 bytes


Try it online! or Verify all test cases.


J_þTݲSiṂ$Ṭo  Input: boolean array A
J             Indices, returns [1, 2, ..., len(A)]
   T          Truthy indices, returns the indices which have a truthy value
 _þ           Form the subtraction (_) table (þ) between them
    İ         Inverse, find the reciprocal of each
     ²        Square each
      S       Sum the sublists column-wise
         $    Monadic chain
        Ṃ       Minimum
       i        Find the first index of that
          Ṭ   Untruth indices, returns a boolean array with 1's at those indices
           o  Logical OR between that and A, and return
| improve this answer | |

MATL, 19 18 17 bytes


Try it online! Or verify all test cases (slightly modified code).


It suffices to compute the distance from each potential new position to the already occupied ones. The remaining distances are do not depend on the potential new position, and so constitute a constant term, which can be ignored.

Let's take input [1 0 0 0 0 0 1] as an example.

l      % Push 1
       % STACK: 1
y      % Take input implicitly. Duplicate from below
       % STACK: [1 0 0 0 0 0 1], 1, [1 0 0 0 0 0 1]
f!     % Indices of nonzero elements, as a column array
       % STACK: [1 0 0 0 0 0 1], 1, [1 7]
Gn:    % Push [1 2 ... n], where n is input size (array of possible positions)
       % STACK: [1 0 0 0 0 0 1], 1, [1; 7], [1 2 3 4 5 6 7]
-      % Matrix with all pairs of differences 
       % STACK: [1 0 0 0 0 0 1], 1, [1; 7], [0 -1 -2 -3 -4 -5 -6;
                                             6  5  4  3  2  1  0]
H_^    % Raise each entry to -2
       % STACK: [1 0 0 0 0 0 1], 1, [   Inf 1.0000 0.2500 0.1111 0.0625 0.0400 0.0278;
                                     0.0278 0.0400 0.0625 0.1111 0.2500 1.0000    Inf]
Xs     % Sum of each column
       % STACK: [1 0 0 0 0 0 1], 1, [Inf 1.04 0.3125 0.2222 0.3125 1.04 Inf]
&X<    % Index of minimum. Takes the first if there is a tie
       % STACK: [1 0 0 0 0 0 1], 1, 4
(      % Assign: write 1 at the position of the minimizer
       % STACK: [1 0 0 1 0 0 1]
       % Implicitly display
| improve this answer | |

JavaScript (ES6), 89 bytes


Outputs by modifying the input array.

| improve this answer | |

R, 83 76 67 bytes

Just realized that I can save several bytes by not bothering to check if the candidate urinals are empty. Non-empty urinals will always return an Inf discomfort value, so they're excluded in the course of the calculation. Also, just using direct indexing rather than replace, so it's shorter but less elegant.




We read the current state from stdin and call it x. We assume that the input is a sequence of 1s and 0s separated by spaces or newlines. For the purposes of the explanation, let's say we input 1 0 0 0 0 0 1.


We replace a value of x at a particular index with 1. Everything between the [ ] is figuring out what the best index is.

Since the existing urinals are immutable, we don't need to consider the distances between them. We only need to consider the distances between the occupied urinals and the possible new one. So we determine the indices of the occupied urinals. We use which, a function to return the indices of a logical vector which are TRUE. All numbers in R, when coerced to type logical, are TRUE if nonzero and FALSE if zero. Simply doing which(x) will result in a type error, argument to 'which' is not logical, as x is a numeric vector. We therefore have to coerce it to logical. ! is R's logical negation function, which automatically coerces to logical. Applying it twice, !!x, yields a vector of TRUE and FALSE indicating which urinals are occupied. (Alternative byte-equivalent coercions to logical involve the logical operators & and | and the builtins T and F, e.g. F|x or T&x and so on. !!x looks more exclamatory so we'll use that.)


This is paired with seq(x), which returns the integer sequence from 1 to the length of x, i.e. all urinal locations (and thus all possible locations to consider).


Now we have the indices of our occupied urinals: 1 7 and our empty urinals 1 2 3 4 5 6 7. We pass `-`, the subtraction function, to the outer function to get the "outer subtraction", which is the following matrix of distances between all urinals and the occupied urinals:

[,1] [,2]

[1,] 0 -6

[2,] 1 -5

[3,] 2 -4

[4,] 3 -3

[5,] 4 -2

[6,] 5 -1

[7,] 6 0


We raise this to the -2th power. (For those who are a little lost, in the OP, "discomfort" is defined as 1 / (distance(x, y) * distance(x, y)), which simplifies to 1/d(x,y)^2, i.e. d(x,y)^-2.)


Take the sum of each row in the matrix.


Get the index of the smallest value, i.e. the optimal urinal. In the case of multiple smallest values, the first (i.e. leftmost) one is returned.


And voilà, we have the index of the optimal urinal. We replace the value at this index in x with 1. In the case of 1111 as input, it doesn't matter which one we replace, we'll still have a valid output.


Return the modified input.

| improve this answer | |

PHP, 135 bytes

$a=explode(1,$argv[1]);$b=0;foreach($a as$c=>$d){$l=strlen($d);if($l>$b){$b=$l;$e=$c;}}if($b)$a[$e][intval($b/2)]=1;echo implode(1,$a);

I'm sure there's a considerably quicker way of doing it, but I've got a fuzzy head and can't think of one!

Old code

The code without minification:

foreach($a as $c=>$d){
echo implode(1,$a);
| improve this answer | |

Python 3 223 222 165 Bytes

Okay, I know this isn't the prettiest answer out there, and I'm sure it can be golfed down quite a bit, but I was just messing around and seeing what I could do

Shout out to mbomb007 for the tips on whitespace and comparators Also, I saw my online character counter was taking all the tabs and turning them into spaces, so the count is a lot less than I originally had

def u(a):
 for i in range(x): 
    if a[i]<'1':
     for j in range(x):
        if a[j]>'0':d+=float((j-i)**-2)
     if d<m:r=i;m=d
 return a[:r]+'1'+a[r+1:]

Showing revised whitespace:

def u(a):
<sp> m,r,x=9,0,len(a)
<sp> for i in range(x): 
<tab> d=0
<tab> if a[i]<'1':
<tab><sp> for j in range(x):
<tab><tab> if a[j]>'0':d+=float((j-i)**-2)
<tab><sp> if d<m:r=i;m=d
<sp> return a[:r]+'1'+a[r+1:]


def u(a):
    for i in range(x): 
        if a[i]!='1':
            for j in range(x):
                if a[j]=='1':d+=float(1/(j-i)**2)
            if d<m:r=i;m=d
    return a[:r]+'1'+a[r+1:]

This expects a string passed to it of 1's and 0's like "10001" and returns a string "10101"

Edit: Changed 1/float((j-i)**2) to float((j-i)**-2)

| improve this answer | |
  • \$\begingroup\$ !='1' can be <'1' and =='1' can be >'0'. Also, consider this tip \$\endgroup\$ – mbomb007 Nov 1 '16 at 16:20
  • \$\begingroup\$ Thanks for that whitespace tip. I definitely did not know that. That's awesome! \$\endgroup\$ – bioweasel Nov 1 '16 at 16:24
  • \$\begingroup\$ That whitespace tip only works in Python 2, I think. Maybe early version of Python 3, but idk. You'll have to restrict your answer to Python 2 or some specific version of 3 with it working. \$\endgroup\$ – mbomb007 Nov 1 '16 at 16:37
  • \$\begingroup\$ I've got it running in a 3.5.2 shell in IDLE and it's running without an issue, so I think it's alright still \$\endgroup\$ – bioweasel Nov 1 '16 at 18:05

Python 3, 574 471 347 bytes

I'll probably work on this some more, considering the other Python solution is like a fifth of this one :[.

def a(I):
 for i in r(l):
  if I[i]<1:
   for j in r(l):
    if n[j]>0:
     for k in r(l):
      if k!=j and n[k]>0:q.append((k-j,j-k)[k<j])
     for i in q:Q+=1/(i**2)
   if T not in D.keys():D[T]=i
 if len(D)>0:I[D[min(D.keys())]]=1

Well that's much better now that I've learned you can use single spaces.

| improve this answer | |

Python, 165 163 158 147 141 140 139 bytes

def u(p):e=enumerate;a=[(sum((i-j)**-2for j,y in e(p)if"0"<y),i)for i,x in e(p)if"1">x];return a and p[:min(a)[1]]+"1"+p[min(a)[1]+1:] or p
| improve this answer | |
  • \$\begingroup\$ rewrite second line as if"1"*len(p)==p:return p to save a byte \$\endgroup\$ – FlipTack Nov 1 '16 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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