Brackets (original) (raw)

The formal definition of this geometric representation is as follows: For every "(" bracket there must be a ")" bracket, making the simplest sequence "()", which represents a 1 * 1 square. If A is a correct bracket sequence and g(A) is its geometric representation, then the representation of (A) is g(A) surrounded by a rectangle that is two units wider than g(A) and one unit taller than the highest point of g(A). Furthermore, if A and B are two correct bracket sequences and g(A) and g(B) are their representations, then we get g(AB) by placing g(B) one unit to the right of g(A). The following example illustrates all of these points:

After finishing your lecture, you decide to use the geometric representations you prepared to entertain yourself. You paint the bounded areas of the images alternatively black and white, such that the outer-most areas are painted all black. After coloring the previous example, you have this as a result:

Since you love parsing expressions so much, you thoroughly enjoyed counting the numbers of black squares in these images. However, you'd like to be able to quickly check that you counted them correctly.

Bracket Sequence

LP Input

Output

Geometric Representation

( )

bracket(0,1).
bracket(1,-1).

black_squares(1).

( ( ( ) ) )

bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,-1).
bracket(5,-1).

black_squares(10).

( ( ) ) ( ( ) ( ( ) ) )

bracket(0,1).
bracket(1,1).
bracket(2,-1).
bracket(3,-1).
bracket(4,1).
bracket(5,1).
bracket(6,-1).
bracket(7,1).
bracket(8,1).
bracket(9,-1).
bracket(10,-1).
bracket(11,-1).

black_squares(20).

( ( ( ) ( ( ) ) ) )

bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,1).
bracket(5,1).
bracket(6,-1).
bracket(7,-1).
bracket(8,-1).
bracket(9,-1).

black_squares(21).

( ( ( ) ) ( ) ) ( )

bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,-1).
bracket(5,1).
bracket(6,-1).
bracket(7,-1).
bracket(8,1).
bracket(9,-1).

black_squares(16).

( ( ( ( ) ( ) ) ) ( ) )

bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,1).
bracket(4,-1).
bracket(5,1).
bracket(6,-1).
bracket(7,-1).
bracket(8,-1).
bracket(9,1).
bracket(10,-1).
bracket(11,-1).

black_squares(30).

( ) ( ( ) ) ( ( ( ) ) )

bracket(0,1).
bracket(1,-1).
bracket(2,1).
bracket(3,1).
bracket(4,-1).
bracket(5,-1).
bracket(6,1).
bracket(7,1).
bracket(8,1).
bracket(9,-1).
bracket(10,-1).
bracket(11,-1).

black_squares(16).

( ( ( ) ) ( ( ( ) ( ) ( ) ) ) )

bracket(0,1).
bracket(1,1).
bracket(2,1).
bracket(3,-1).
bracket(4,-1).
bracket(5,1).
bracket(6,1).
bracket(7,1).
bracket(8,-1).
bracket(9,1).
bracket(10,-1).
bracket(11,1).
bracket(12,-1).
bracket(13,-1).
bracket(14,-1).
bracket(15,-1).

black_squares(39).