You are on a plane filled with boxes, and you get a sequence of movements, say "up right right down left":

You move up, pushing all boxes in front of you away:

You move right, right and down, getting

and finally left. This time you only push one box rather than infinite boxes:

At the end, there are 4 spaces.

Given a sequence of moves, decide how many spaces are there after these moves. You can take 4 distinct inputs to mean 4 directions. You can also count yourself as space(result +1). Shortest code in each language wins.

Test data:

R => 1
RD => 2
RL => 1
RDRU => 4
URRDL => 4
  • \$\begingroup\$ Can you take input as coordinates, like (1,0) for right? \$\endgroup\$
    – mousetail
    Commented Jan 25, 2023 at 6:37
  • 1
    \$\begingroup\$ @mousetail You take 4 distant inputs to mean LRUD \$\endgroup\$
    – l4m2
    Commented Jan 25, 2023 at 9:02
  • 1
    \$\begingroup\$ Not relevant to the question at all, but I like the illustrations :) \$\endgroup\$ Commented Jan 25, 2023 at 16:38
  • 6
    \$\begingroup\$ @ They are from the game "Baba is you" \$\endgroup\$
    – mousetail
    Commented Jan 25, 2023 at 16:43

5 Answers 5


JavaScript (ES6), 94 bytes

-3 bytes thanks to @l4m2

Expects an array of [dx,dy] pairs, e.g. [[-1,0],[1,0]] for LR.


Try it online!


The algorithm used here is pretty straightforward.

Whenever we move in a direction \$(dx,dy)\$, we look for the first empty cell along this ray.

Figure A

If we find one, we mark it as non-empty anymore.

Figure B

Otherwise, we increment the total number of empty cells. In both cases, we mark the cell at our new position as empty.

Note that if we move to an empty cell, it will briefly be marked as non-empty and then marked as empty again, which is conceptually silly but functionally correct.

In order to make sure that we look far enough, the length of the test ray is simply the number of moves performed so far (including the current one).


a =>            // a[] = input array, re-used to store visited cells
a.map(          // for each ...
([h, v], i) =>  // ... direction [h, v] at index i in a[]:
  a[[           //
    X = x,      //   initialize (X, Y) to (x, y)
    Y = y       //
  ]] =          //   mark this cell as visited (i.e. empty)
  !(            //   by setting it to false
    g = _ =>    //   g is a recursive function ignoring its argument
    ~--a[       //
      [         //     lookup key of the new position
        X += h, //     defined as (X + h, Y + v)
        Y += v  //
      ]         //     we force the state of this cell to non-empty
    ] ?         //     if it already was non-empty:
      i-- ?     //       if i is not equal to 0 (decrement i afterwards):
        g()     //         keep moving along this ray
      :         //       else:
        ++n     //         increment the number n of empty cells and stop
    :           //     else:
      1         //       stop
  )(            //   initial call to g ...
    x += h,     //     ... with (x, y) updated to (x + h, y + v)
    y += v      //
  ),            //
  n = x = y = 0 //   start with n = 0 and (x, y) = (0, 0) 
) | n           // end of map(); return n
  • \$\begingroup\$ Alternative 97 \$\endgroup\$
    – l4m2
    Commented Jan 25, 2023 at 15:48
  • \$\begingroup\$ 96 \$\endgroup\$
    – l4m2
    Commented Jan 25, 2023 at 17:18

Python 3.8 (pre-release), 130 123 bytes

-7 bytes thanks to @l4m2

def f(l):
 for m in l:E^={p,p+min([q for y in E if(x:=(y-p)/m)==int(q:=x.real)>0]or[0])*m};p+=m
 return len(E)

Try it online!

Takes input as a list of complex numbers.

  • \$\begingroup\$ 123 \$\endgroup\$
    – l4m2
    Commented Jan 25, 2023 at 10:38

Vyxal, 20 bytes


Try it Online! or verify all test cases

A full program accepting a list of strings "1°0", "0°1N", "1°0N", "0°1" for right, down, up, left respectively. Works by keeping a list of empty positions and removing the first one in the direction of travel each time.

Ė              # evaluate (convert each to a complex number)
 ¾             # push empty list
  $            # swap
   (           # for n in directions:
    n*         #   multiply by n
      :        #   duplicate
       ~¨=ȧ    #   keep only real non-negative numbers by checking if they are invariant under abs(x)
           g   #   minimum
            F  #   remove from the original list

0J             #   append 0
  ‹            #   decrement
   n/          #   divide by n
     )         # end for
      L        # length
  • \$\begingroup\$ Please can you explain a little more what Ė exactly does? Thanks! \$\endgroup\$
    – lesobrod
    Commented Feb 5, 2023 at 14:19
  • \$\begingroup\$ @lesobrod It just executes a string as vyxal code. Afaik Vyxal can't parse complex number input so I used this to substitute for it. \$\endgroup\$
    – AndrovT
    Commented Feb 5, 2023 at 14:43
  • \$\begingroup\$ Aha, now i see! \$\endgroup\$
    – lesobrod
    Commented Feb 5, 2023 at 15:30

Pyth, 34 bytes


Try it online!

Same basic method as @Arnauld. Takes input as four distinct direction vectors (1,0), (-1,0), (0,1), (0,-1).


K is the current position and Y is a list of empty positions.

                                      # implicitly assiqn Q = eval(input())
K,ZZ                                  # K = (0,0)
    VQ                                # for N in Q:
        aYK                           #   append K to Y
              m        UY             #   map range(len(Y)) over lambda d
               +VK*LhdN               #     K + ((d + 1) * N)  (vectorized)
            @Y                        #   intersection of that with Y
           <             1            #   take the first element (or empty list if none)
      =-                              #   remove that element from Y
                          =+VKN       #   K = K + N  (vectorized)
                               ;lY    # after all iterations, output length(Y)

Charcoal, 31 bytes


Try it online! Link is to verbose version of code. Explanation:


Loop over the input string.


Mark the current square as "empty" and move in the given direction.


Peek ahead the number of characters in the input string in the direction given by the current character.


Try to erase the first # found (this corresponds to pushing a box there).


Erase the finishing square. (This could be »# to count yourself as an empty square.)


Get the number of empty squares, clear the canvas, and output the count.


Your Answer

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

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