Edit: Thanks @Martin Ender♦ for saving me 1 byte from the marvelous -1 trick!
Try it online to verify test cases!
The images below are not modified but the flow is basically the same. Also note that this will return
-1 if the input is not an array (i.e. without
I have lots of no-ops inside the Hexagon... I guess it can definitely be golfed more.
In brief, it adds
-1 when encounters a
[ and adds
1 when encounters a
]. Finally it prints the max it has got.
Let's run along Test Case 5 to see its behaviour when it runs along the String
[1, [], [5, 6], [[[]]], 1]:
It starts at the beginning and takes its input at the W corner:
Since there is still input (not the null character
\0 or EOL), it wraps to the top and starts the crimson path.
Here is what happens when from there till cute
[ into Buffer, and
Z sets the constant Z to be 90.
' moves to Diff and
- calculates the difference. For
] the difference will be
3 respectively. For numbers and spaces and commas it'll be negative.
Then we run
( twice (once at the end of crimson path, one at the start after wrapping at the green path) to get
1 resp for
]. Here we change the naming of
Value. Add this Value to Depth. (I used
Z& to ensure that it copies the right neighbor). Then we calculate
lastMin - Depth and got a number on the Memory edge
Then we apply
& (at the end of green path) to
minLR: If the number is <=0, it copies the left value (i.e.
lastMin - Depth <= 0 => lastMin <= Depth), otherwise it takes the right value.
We wraps to the horizontal blue path and we see
Z& again which copies the
minLR. Then we
"& and made a copy of the calculated min. The brackets are assumed to be balanced, so the min must be <=0. After wrapping, the blue path go left and hit
(, making the copy
1 less than the real min. Reusing the
-, we created one more 1-off copy as a neighbor of Buffer:
copy is renamed as
When blue path hits
\ and got a nice
< catches it back to the main loop.
When the loop hits
or other numbers as input:
The Diff will become negative and it got reflected back to the main loop for next input.
When everything has gone through the main loop, we reach EOL which makes Buffer
-1 and it finally goes to the bottom edge:
' moves the MP to the
1-off copy and
) increments it, and with
~ negation it got the correct Max Depth value which is printed with
And the story ends with a
I guess I must have over complicating things a little bit. If I have had to only "move back" and "print" without incrementing and negation, I would have well saved 2 bytes without using the full Hexagon.
Great thanks to Timwi for Esoteric IDE and Hexagony Colorer!