Try it online!
This counts the opening brackets via a bit of bitwise magic. If we consider the results of the character codes of the bitwise AND of
2, we get:
[ , ]
2 0 0
So if we just sum up the result of this operation for each character, we get twice the value we want.
As for the code itself, the 2x2 block at the beginning is a small loop. On the first iteration
&- don't really do anything except that they put an explicit zero on top of the implicit ones at the bottom of the stack. This will be the running total (and it will actually be negative to save a byte later). Then the loop goes as follows:
, Read character. At EOF this gives -1 which causes the instruction pointer to
leave the loop. Otherwise, the loop continues.
# Push the stack depth, 2.
& Bitwise AND.
- Subtract from running total.
Once we leave the loop, the following linear bit is executed:
( Decrement to turn the -1 into a -2.
/ Divide negative running total by -2 to get desired result.
The IP then hits a dead and turns around. When it tries to executed
/ again, the program terminates due to the attempted division by zero.