14
\$\begingroup\$

You should write a program or function which given a list of tetris blocks as input outputs or returns the biggest gap between two points in the same height level which the pieces can connect.

The 7 types of tetris pieces are the following:

Tetris bricks

We will refer to these pieces by the letters I, J, L, O, S, T and Z respectively, referring to their shapes. You can rotate the pieces but cannot mirror them just like in a Tetris game.

Our task is to create an orthogonally connected area (sides connected to sides) from the given pieces. This area should connect (also orthogonally) two unit squares which are at the same height. We should find the biggest possible gap between the two squares which we can bridge.

Detailed examples

With the piece L we can connect a gap of 3

   L
XLLLX

With the piece S we can connect a gap of 2

  SS
XSSX

With the pieces S,S,O we can connect a gap of 7 (Note that we can't connect a gap of 8)

 S
XSSOO SSX
  SOOSS

Input

  • A string representing the available pieces containing only the uppercase letters I, J, L, O, S, T and Z. Every letter represents a complete tetris piece.
  • The letters will be in alphabetical order in the string.
  • The string will be at least one character long.

Output

  • A single positive integer, the biggest gap connectable with the given pieces.

Examples

Input => Output

OSS  =>  7

LS  =>  5

LZ  =>  6

ZZZZ  =>  10

LLSSS  =>  14

IIJSSSTTZ  =>  28

IISSSSSS  =>  24

OOOSSSSSSSSSSSSTT  =>  45

IJLOSTZ  =>  21

IJLOSTZZZZZZZ  =>  37

IIJLLLOSTT  =>  31

IJJJOOSSSTTZ  =>  35

This is code-golf so the shortest entry wins.

\$\endgroup\$
  • \$\begingroup\$ Ahh, I see. I was looking at them as are. \$\endgroup\$ – Tim May 14 '15 at 16:12
4
\$\begingroup\$

CJam, 53

'[,73>qf{1$e=s':+\+~}:+3*I+O-SZ-JO+m0e>ZS-LO+-e>2+3/-

Try it online

The idea is: assign each of the I, J, ..., Z variables to be the number of occurences of that letter, and calculate string length * 3 + I - O. Then count the number of non-compensated S's or Z's: an S can be compensated by Z, J or O, and a Z can be compensated by S, L or O, and subtract ceil(that number/3), as we lose 1 unit for every 3 S's or Z's.

\$\endgroup\$

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.