Make An ASCII Poker Chip Stack Arrangement
JavaScript (ES6), 185 177 ... 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>
Ruby, 181 177 bytes
->n{*a=5*10**8
14.times{|i|a<<a[-1]/[5,4,5,5,2][i%5]}
a=a.zip("SVTILAMCOPYBGRW".chars).map{|v,c|[-(n/v),v,c,n%=v]}.sort
l=-a[0][0]
(1..l).map{|i|a.map{|d|l-i<-d[0]?d[2]:""}*""}}
Tests on Ideone.
Pyth, 56 55 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."
00000010: 4159 261c 0c24 2087 0c86 1e22 6d68 412e AY&..$ ...."mhA.
00000020: 4448 645f 2e75 2a4e 5950 2a33 6a37 3733 DHd_.u*NYP*3j773
00000030: 3620 362f 6 6/
Try it online. Test suite.
Pro tip: to golf 1
in the end of a program accepting a nonzero number in Q
, just add /
. /QQ
, which in Python would be Q // Q
, is 1 for nonzero Q
.
Without compression and unprintables (55 bytes):
=HQj_.t_ #lD*V"SVTILAMCOPYBGRW"mhA.DHd_.u*NYP*3j7736 6/