Skip to main content
added 688 characters in body
Source Link
marinus
  • 31.2k
  • 7
  • 71
  • 112

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 be 0 so this will return 0. If the braces do not match, it will return a nonzero value.
  • exit: set the exit value to that and exit.

J (38)

exit(0:~:{:+<./)+/\+/1 _1*'[]'=/1!:1[3

J (38 35)

exit({:+.<./)+/\+/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 be 0 so this will return 0. If the braces do not match, it will return a nonzero value.
  • exit: set the exit value to that and exit.
Source Link
marinus
  • 31.2k
  • 7
  • 71
  • 112

J (38)

exit(0:~:{:+<./)+/\+/1 _1*'[]'=/1!:1[3