Determine if a grid contains another grid

Brachylog (v2), 4 bytes


Try it online!

Most easily run as a full program, as usual for a decision-problem, with a specified as a command-line argument, b on standard input. The question asks for a function, and the program also works as a function, with b on the left, a on the right, and output via producing an exception if and only if the decision is false.


s     a substring of rows of {the left input}
 \…\  assert rectangular; swap row and column operations
  s   a substring of <s>rows</s> columns of {the above matrix}
      {implicit} assert that the result can be {the right input}

The "assert rectangular" is, obviously, pointless, as the question guarantees that already. The rest of the program does the grid-finding for us by identifying a substring of the rows and of the columns, i.e. a submatrix.


We've had a very similar question before; I'd expect most answers to one question to be modifiable into answers to the other. I think this is the neater version of it, though.

Python 2, 67 bytes

f=lambda a,b,r=4:b*r and f(a,b[1:],r)|f(a,zip(*b)[::-1],r-1)or a==b

Try it online!

Takes input as lists of tuples of characters.

Tries all sub-grids of b and checks if a is among them. The sub-grids are generated by recursively branching on either removing the first row of b or rotating it 90 degrees. After exactly four rotations, checks if the trimmed down b is equal to a.

J, 21 15 8 7 bytes


Try it online!

-7 bytes thanks to Bolce Bussiere

original answer

J, 21 15 bytes


Try it online!

-6 bytes thanks to FrownyFrog


  • <@[ boxed left arg
  • $@[<;.3] all rectangles in the right arg with the same shape as the left arg
  • now pass those as the left and right arg to...
  • is the left arg an elm of the right arg, after flattening both e.&,