54
11
Given an unsigned 16-bit integer N, your task is to determine whether its binary representation mapped inside a 4x4 matrix is matching a tetromino shape, and if so, which shape it is.
Matrix
Each bit of N is mapped inside a 4x4 matrix, from left to right and from top to bottom, starting with the most significant one.
Example:
N = 17600
binary representation: 0100010011000000
matrix: [ [ 0, 1, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 1, 1, 0, 0 ],
[ 0, 0, 0, 0 ] ]
Tetromino shapes
Base shapes
There are 7 tetromino shapes, identified by the letters O, I, S, Z, L, J and T:
Rotations and translations
If a shape is translated and/or rotated within the 4x4 matrix, it is still considered a valid variation of the same tetromino. For instance, 17600, 1136, 2272 and 1604 should all be identified as J tetrominoes:
Don't wrap!
However, the shapes can't wrap around or be shifted beyond any boundary of the matrix. For instance, neither 568 nor 688 should be identified as J tetrominoes (let alone any other shape):
Clarifications and rules
- You may take input as an integer, or directly as 16 binary digits in any reasonable format, such as a 2D array, a flat array or a delimited string.
- The input is guaranteed to be an unsigned 16-bit integer (or its equivalent representation as an array or a string).
- When a valid shape is identified, you must print or return the letter identifying the shape, in either lower or upper case.
- If no shape is identified, you must print or return a value that doesn't match any tetromino letter. You may also choose to return nothing at all.
- To be considered valid, the matrix must contain the exact tetromino shape without any additional cells (see 1911 and 34953 in the test cases).
- This is code-golf, so the shortest answer in bytes wins!
Test cases
You can follow this link to get the test cases as 2D arrays.
0 -> false
50 -> false
51 -> 'O'
1911 -> false
15 -> 'I'
34952 -> 'I'
34953 -> false
1122 -> 'S'
3168 -> 'Z'
785 -> 'L'
1136 -> 'J'
568 -> false
688 -> false
35968 -> 'T'
19520 -> 'T'
Interestingly, I was working on an extremely similar problem the other day before I got distracted creating a technique to use function chains
func1 . func2 . func3
in JS :P – ETHproductions – 2017-08-09T12:09:43.540Can I take input as the four rows joined with
0
, i.e.1111011110111101111
for65535
? – ETHproductions – 2017-08-09T16:14:56.330@ETHproductions That seems fine. I've edited the challenge with a slightly relaxed input format. – Arnauld – 2017-08-09T16:24:24.670
3I:
15,240,3840,4369,8738,17476,34952,61440
J:71,113,142,226,275,550,802,1100,1136,1604,1808,2272,3208,3616,4400,8800,12832,17600,18176,25664,28928,36352,51328,57856
L:23,46,116,232,368,547,736,785,1094,1570,1856,2188,3140,3712,5888,8752,11776,12560,17504,25120,29696,35008,50240,59392
O:51,102,204,816,1632,3264,13056,26112,52224
S:54,108,561,864,1122,1728,2244,8976,13824,17952,27648,35904
T:39,78,114,228,305,562,610,624,1124,1220,1248,1824,2248,3648,4880,8992,9760,9984,17984,19520,19968,29184,35968,58368
Z:99,198,306,612,1224,1584,3168,4896,9792,19584,25344,50688
– Engineer Toast – 2017-08-09T18:27:58.210^ Generated using Lynn's Python 3 answer because it had convenient input / output formats.
– Engineer Toast – 2017-08-09T18:28:46.623688 mino is rather interesting for gamers. – Takahiro Waki – 2017-08-09T23:11:21.730
tetrAmino, sorry. domino, trimino, tetramino, pentamino, hexamino and so on. – Gangnus – 2017-08-10T10:05:46.413
@Gangnus Tetrominoes is apparently the standard math term. I think the Tetris Company has used the alternate spelling Tetraminoes at some point and is now using Tetriminos, which is trademarked. But I tend to say 'Tetramino' and accidentally used both spellings in my original edit. :-) – Arnauld – 2017-08-10T10:18:07.250
@Arnauld Yes, I see. I have got that from a Russian translation of a Martin Gardner book, but it really seems that in English it has O. That translation is the reason why Pazhitnov(author of Tetris) used A, obviously. My excuses. – Gangnus – 2017-08-10T10:22:29.457
Am I right in assuming any connected block of exactly four ones is legal? I suspect there's some fairly simple bit trickery solution to this. Identifying the shape might complicate matters a little. – JollyJoker – 2017-08-10T11:27:13.473
@Arnauld Thanks. Maybe I should make a "does this matrix contain exactly one polyomino" challenge instead. The answers would be completely different.
– JollyJoker – 2017-08-10T12:09:38.180