Make An ASCII Poker Chip Stack Arrangement

Poker has etiquette in how you arrange your chips, often enforced in tournaments - your chips may not be "hidden" from your opponents by being behind others, mostly to not hide some large denomination chip(s).

The Challenge

We are going to be playing poker in ASCII, so we need to write a function or program that will draw our ASCII chip stack arrangement given its total value, n.

Input
- A positive integer, n (up to 2**32-1 should be handled)

Output
- An ASCII representation of the stack arrangement as defined below.
This may contain white-space to the right of each line such that no line is longer than one more character than the length used by printable characters in the longest (bottom) line;
This may contain a single trailing new line; and
The characters representing chips may be in lowercase if you prefer.

The stack arrangement will:

• Contain the fewest chips possible, given the denominations (see below);
• Will have equal valued chips in "stacks" (columns);
• Be ordered such that the shorter stacks are to the right of taller stacks; and
• Be ordered such that stacks with greater denomination chips will be to the right of equal sized stacks of lower denominations (representing that they are visible to our opponent(s) on the right)

The chips themselves are to be represented as individual characters identifying their colour:

White : 1    = W
Red : 5    = R
Green : 25   = G
Black : 100  = B
Yellow : 500  = Y
Pink : 1K   = P
Orange : 5K   = O
Cyan : 25K  = C
Magenta : 100K = M
Aqua-blue : 500K = A
Lavender : 1M   = L
Indigo : 5M   = I
Turquoise : 25M  = T
Violet : 100M = V
Silver : 500M = S

Example

For n = 276,352 the smallest number of chips would be:

2 * 100K + 3 * 25K + 1 * 1K + 3 * 100 + 2 * 25 + 2 * 1
MM         CCC       P        BBB       GG       WW

The single P must go on the far right,
then the three stacks of size 2 must go next,
- but the MM must go furthest to the right followed by the GG and then the WW since 100K > 25 > 1 then the two stacks of size 3 go on the left,
- but the CCC must go to the right of the BBB since 25K > 100

Now we must place these chips into actual stacks, to make our output:

BC
BCWGM
BCWGMP

Test Cases

Input:
1

Output:
W

Input:
9378278

Output:
L
LWGPCM
LWGPCMB
LWGPCMBI

Input:
22222222

Output:
ROI
ROI
ROIWBPML
ROIWBPML

Input:
1342185143

Output:
WRCIV
WRCIVOLS
WRCIVOLSGBMT

Input:
2147483647

Output:
RMIS
RMISPC
RMISPCWL
RMISPCWLGBYOTV

Input:
4294967295

Output:
S
S
S
S
SRML
SRMLGOIT
SRMLGOITBPCV
SRMLGOITBPCVA

This is , so shortest code in bytes wins. No loopholes, yada yada, you know the drill.

• Wow, this looks fun, I may try a Julia answer when i get home. Sep 21, 2016 at 20:39
• Did you mean 2**31-1, or is your intent to be larger than most signed int types? Sep 21, 2016 at 21:54
• @Linus hmm, I made it unsigned; I did actually specifically ask if the input range was OK in the sandbox and no one responded. As you can see 2**32-1 is a test case, but I'm willing to lower it. (Interesting fact: PokerStars has a 25B chip in their image folder.) Sep 21, 2016 at 21:58

JavaScript (ES6), 185177 ... 171 bytes

f=(a,s='')=>(a=(a[0]?a:[...(m='25455')+m+m].map((m,i)=>(a-=(k=a/(v/=m)|0)*v,k*16+i),v=1E9).sort((a,b)=>b-a)).map(n=>n>15&&(s+='SVTILAMCOPYBGRW'[n&15],n-16)),s&&f(a)+
+s)

Formatted and commented

(a, s = '') => (                           // a = bankroll OR array of chip stacks
a = (                                    // s = string of chip initials for this iteration
a[0] ?                                 // if 'a' already is an array:
a                                    // use it as-is
:                                      // else, 'a' is the bankroll:
[...(m = '25455') + m + m]           // for each 'm' in [ 2, 5, 4, 5, 5, ... ] (x3)
.map((m, i) =>                       // v = current chip value = previous value / m
(                                  // k = floor(a / v) = number of these chips
a -= (k = a / (v /= m) | 0) * v, // update remaining bankroll: a = a - k * v
k * 16 + i                       // saved in array: chip index in bits 0-3
),                                 // and number of chips in bits 4+
v = 1E9                            // initial chip value = 1 billion
)                                    // (1B / 2 -> 500M, 500M / 5 -> 100M, etc.)
.sort((a, b) => b - a)               // sort the chip stacks
)                                        //
.map(n =>                                // for each chip stack:
n > 15 &&                              // if there's at least one remaining chip of
(                                      // this kind:
s += 'SVTILAMCOPYBGRW'[n & 15],      // append its initial to the string
n - 16                               // decrement the number of chips in this stack
)                                      //
),                                       // process recursive call if there was at least
s && f(a) + '\n' + s                     // one non-empty chip stack (the next lines are
)                                          // appended at the beginning of the final string)

Demo

let f=(a,s='')=>(a=(a[0]?a:[...(m='25455')+m+m].map((m,i)=>(a-=(k=a/(v/=m)|0)*v,k*16+i),v=1E9).sort((a,b)=>b-a)).map(n=>n>15&&(s+='SVTILAMCOPYBGRW'[n&15],n-16)),s&&f(a)+
+s)

function update() {
document.getElementById('o').innerHTML = f(+document.getElementById('i').value);
}
update();
<input id="i" value="1342185143" oninput="update()"><pre id="o"></pre>

Pyth, 5655 52 bytes

The code contains some unprintables, so here's a reversible xxd hexdump.

00000000: 3d48 516a 5f2e 745f 2023 6c44 2a56 2e22  =HQj_.t_ #lD*V."