Stack Overflowing

Pyth, 43 41 40 bytes

<" \|/"g#0hK-QJEVJs[\(?<N_Kd\-\)*<N-K3\-

Try it online. Test suite.

First pass, quick and dirty. Input to STDIN as N\nH.


  1. Save the second input (height) to J (JE), and subtract it from the first input (the number of items). (-QJE)
  2. Save the difference (number of overflowing items) to K. (K-QJE)
  3. Add 1 to the number. (hK-QJE)
  4. Take max(0, previous). This is required as negative numbers would break the next step. (g#0hK-QJE)
  5. Take at most that many letters from the string " \|/" to get the first line and print. (<" \|/"g#0hK-QJE)
  6. Loop N over range(0, J). (VJ) For each N print the concatenation of the following: (s[)
    • "(" (\()
    • " " if there are at least N+1 free spaces in the stack (<N_K), "-" otherwise. (?<N_Kd\-)
    • ")" (\))
    • "-" if there are at least N+4 overflowing pieces in the stack (<N-K3), "" otherwise. (*<N-K3\-)

JavaScript (ES6), 105 102 bytes

@Edit: Saved 3 bytes thanks to @PatrickRoberts.

(n,h)=>` \\|/`.substr(0,n+1-h)+[...Array(h)].map((_,i)=>`
(${i+n<h?` `:`-`})${i+h+3<n?`-`:``}`).join``
<div oninput=o.textContent=f(+n.value,+h.value)>n<input id=n type=number min=0 value=0>h<input id=h type=number min=0 value=0><pre id=o>

JavaScript (ES6), 126 122 112 bytes

h=>n=>' \\|/'.substr(0,(o=n-h)+1)+`
( )`[r='repeat'](0>-o?0:-o)+`


f=h=>n=>' \\|/'.substr(0,(o=n-h)+1)+`
( )`[r='repeat'](0>-o?0:-o)+`


Alternate Test (if your browser does not support ES6)

See the test on and check "evaluate".

Interesting alternate approach at 136 bytes

h=>n=>' \\|/'.substr(0,(o=n-h)+1)+`
( )${0>-o?0:-o}

This moves the repeat amounts into the template string and uses a regexp and replace to inject the repeating groups. Unfortunately the signature for .replace() is just too long.