J (3838 35)
exit(0:~:{:+<+.<./)+/\+/1 _1*'[]'=/1!:1[3
Explanation:
1!:1[3
: read stdin'[]'=/
: create a matrix where the first row is a bitmask of the[
s in the input, and the second row is the]
s.1 _1*
: multiply the first row by 1 and the second row by -1.+/
: sum the columns of the matrix together, giving delta-indentation per character+/\
: create a running total of these, giving indentation level at each character({:+.<./)
: return the GCD of the final element ({:
) and the smallest element (<./
). If all the braces match, both of these should be0
so this will return0
. If the braces do not match, it will return a nonzero value.exit
: set the exit value to that and exit.