Quantcast
Channel: User Yay295 - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 12

Answer by Yay295 for Integers, Assemble!

$
0
0

JavaScript, 284259245241240223209 205 bytes

// Golfedlet f = (N,W,H)=>eval('a=Array(H).fill("");while(N)g:{s=""+N--;d=s[L="length"];for(i in a)if(a[i][L]+d<=W){a[i]+=s;break g}for(p=0;d;++p){l=a[p][L];for(k=p+d;k>p;)l=a[--k][L]-l?W:l;while(l<W&&d)a[p+--d]+=s[d]}}a');// Ungolfed(N,W,H) => {    a = Array(H).fill(""); // Create `H` empty rows.    while (N) g : {        s = ""+ N--; // Convert the number to a string.        d = s[L="length"]; // Count the digits in the number.        // Loop through the rows trying to fit the number in horizontally.        for (i in a) {            if (a[i][L] + d <= W) { // If it fits.                a[i] += s; // Append the number to the row.                break g; // This is what a goto statement looks like in JavaScript.            }        }        // Loop through the rows trying to fit the number in vertically.        for (p = 0; d; ++p) {            l = a[p][L]; // Get the length of the row.            // Find `d` adjacent rows of the same length.            for (k = p + d; k > p; ) {                // If `a[--k][L] == l`, continue.                // Else set `l` to `W` so the next loop doesn't run.                l = a[--k][L] - l ? W : l;            }            // Put the characters in position.            while (l < W && d)                a[p+--d] += s[d];        }    }    return a;}let test_data = [[1,1,1],                 [6,6,1],                 [6,2,3],                 [10,1,11],                 [10,11,1],                 [11,13,1],                 [27,9,5],                 [183,21,21],                 [184,2,222],                 [200,41,12],                 [1003,83,35]];for (let test of test_data)    console.log(f(test[0],test[1],test[2]));

Viewing all articles
Browse latest Browse all 12

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>