# Integers, Assemble!

Your task is to assemble the integers from 1 to N (given as input) into a rectangle of width W and height H (also given as input). Individual numbers may be rotated by any multiple of 90 degrees, but they must appear as contiguous blocks in the rectangle. That is, you cannot break one of the numbers into multiple digits and place the digits in the rectangle individually, neither can you bend three digits of a number around a corner. You could consider each number a brick from which you're building a wall.

Here is an example. Say your input is (N, W, H) = (12, 5, 3). One possible solution is:

18627
21901
53114


For clarity, here are two copies of this grid, one with the one-digit numbers hidden and one with the two-digit numbers hidden:

1####    #8627
2##01    #19##
##11#    53##4


It's fine if the rectangle cannot be disassembled again in a unique way. For instance, in the above example, the 12 could also have been placed like this:

#####    18627
21#01    ##9##
##11#    53##4


## Rules

You may assume that N is positive and that W*H matches the number of digits in the integers from 1 to N inclusive, and that a tiling of the rectangle into the given numbers exists. I don't currently have a proof whether this is always possible, but I'd be interested in one if you do.

Output may either be a single linefeed-separated string or a list of strings (one for each line) or a list of lists of single-digit integers (one for each cell).

The results of your submission must be determinstic and you should be able to handle all test cases in less than a minute on reasonable desktop machine.

You may write a program or a function and use any of the our standard methods of receiving input and providing output.

You may use any programming language, but note that these loopholes are forbidden by default.

This is , so the shortest valid answer – measured in bytes – wins.

## Test Cases

Except for the first one, none of these are unique. Each test case is N W H followed by a possible output. Make sure that your answer works when the rectangle is too narrow to write the larger numbers horizontally.

1 1 1
1

6 6 1
536142

6 2 3
16
25
34

10 1 11
1
0
8
9
2
6
7
3
1
5
4

11 13 1
1234567891011

27 9 5
213112117
192422581
144136119
082512671
205263272

183 21 21
183116214112099785736
182516114011998775635
181116013911897765534
180415913811796755433
179115813711695745332
178315713611594735231
177115613511493725130
176215513411392715029
175115413311291704928
174115313211190694827
173115213111089684726
172015113010988674625
171915012910887664524
170814912810786654423
169714812710685644322
168614712610584634221
167514612510483624120
166414512410382614019
165314412310281603918
164214312210180593817
163114212110079583716

200 41 12
81711132917193661114105533118936111184136
50592924448815915414562967609909953662491
89529721161671582389717813151113658811817
41418184511110119010183423720433017331118
35171183614003547461181197275184300111711
41874381132041861871718311415915921116264
11914245014112711011594492626831219331845
17125112629222085166344707736090956375181
94507611291431121128817413566319161275711
11011540021119913511011169939551729880780
92725141607727665632702567369893534277304
78118311405621148296417218591118562161856

• Is there a proof that this is always possible? Aug 2, 2016 at 12:04
• @Fatalize Good question actually. You can assume it's possible for all given inputs, but a proof either way would be interesting. Aug 2, 2016 at 12:06
• @Fatalize: At least in the trivial case of input (10, 1, 1), it's not possible (assuming that all numbers from 1 to N MUST be used in the construction). If that constraint is held, the area of the rectangle in units must be at least the number of digits in 1..N in order to make it possible. If that constraint is relaxed, it's possible in all cases (but then the challenge isn't much fun :P) Aug 2, 2016 at 17:35
• @SebastianLenartowicz: I think you missed the part where it says the area of the rectangle matches the sum of the digits of the numbers in [1,N]. If N == 10, then width and height have to be 1 and 11. If the width or height is 1, this problem is always solvable. Aug 2, 2016 at 17:45
• @MartinEnder The opposite challenge could be interesting too : a rectange of digits as input (and eventually N, but the program could calculate it from the width and height), and the program needs to check if the rectangle is a valide answer to this challenge....
Aug 2, 2016 at 20:46

# Python 2, 210 200 bytes

Edit: Works now!

Fills from top to bottom, left to right, starting with the largest numbers. Then, transpose and do it again. Then transpose and print. I had to pad with spaces for the transposition to work, since the lines aren't to their full length yet.

I had trouble getting a nested exec working (to do exec'exec"..."*w\n;...'*2. If someone can figure it out, let me know.

n,w,h=input()
s=[""]*h
for x in 1,2:
exec"for i in range(h):l=len(s[i]+n)<=w;s[i]+=n*l;n-=l\n"*w;s=[r.replace(" ","")for r in map(lambda x:x[2::5],zip(*[r.ljust(w)for r in s]))];w,h=h,w
print s


Try it online - Uses a modified function so it can run multiple test cases more easily (and it couldn't use exec). Uncomment the other version and modify stdin to see it run.

Less golfed:

def f(n,w,h):
s=[""]*h
for x in 1,2:
for j in[0]*w:
for i in range(h):
l=len(s[i]+n)<=w
s[i]+=n*l
n-=l
s=[r.ljust(w)for r in s]
s=map(lambda x:x[2::5],zip(*s))
s=[r.replace(' ','')for r in s]
w,h=h,w
print"\n".join(s)

• It's very likely that this should work for all cases now, but an (informal) proof would still be appreciated. ;) Aug 2, 2016 at 21:34
• @MartinEnder A proof is probably beyond me. For the numbers to vary more in length, the test cases get very large. It's probably related to or the same as the proof of whether there is always a solution. Aug 2, 2016 at 21:47

## JavaScript, 284259245241240223209 205 bytes

// Golfed
let 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]));

• Save 1 byte by using - instead of != to test whether two numbers are different.
– Neil
Aug 7, 2016 at 17:10

# Pyth, 35 bytes

juum+WghHl+dQd~tQN<+.TGmkeHeH)_BvzY


Credits to mbomb007. I used his algorithm. Originally I only wanted to help Steven H., but then I really wanted to see a short version.

Takes N on the first line, and W,H on the second line: Try it online: Demonstration

Found a nasty bug in the Pyth implementation of .[ (my own fault, since I implemented it). Gotta fix it tomorrow. This resulted in +3 bytes.

### Explanation:

juum+WghHl+dQd~tQN<+.TGmkeHeH)_BvzY
I'll perform all operations on this list.
Sometimes it is called G, sometimes N.
vz    read the second line and evaluate it: [W, H]
_B      bifurcate it with reverse: [[W, H], [H, W]]
u                                    for each pair H in ^:
.TG                  transpose G
+   mkeH              append H[1] empty strings
<        eH            use only the first H[1] strings
lets call this result N
u                          )           modify N, until it doesn't change anymore:
m                        N               map each d in N to:
WghHl+dQ                                  if H[0] >= len(d+Q):
+        d  Q                                 d + Q
~t                                  and decrement Q by 1
d                                 else:
d
j                                     at the end print every row on a separate line


# Pyth, 79 50 48 Bytes

Noncompeting until I work out bugs (for example, [6,6,1] returns the same as [6,1,6]). It's my first time trying to use Pyth, so I'm probably missing a lot of features.

Thanks to Jakube, saved 29 bytes and made my code actually work!

Saved another two byte by realizing that the repr() calls were unnecessary.

It's basically just a translation of mbomb007's Python 2 answer.

AEJmkHFb2VHVGIgGl+@JNQ XNJ~tQ)))=.[.TJkGA,HG)jJ


Takes input in the form of
n
w,h.

• Answers should be deleted until they are a valid solution. Aug 3, 2016 at 1:18
• I think most of the code is correct. The only error I see happens during the transposition. mbomb007 transposes by carefully filling up the remaining columns with spaces, then zips and removes the spaces. This guarentees. that the after transposing the matrix has w length. =.TZ can't guarantee that, since it doesn't know the length w. Aug 3, 2016 at 14:10
• Actually the main error is, that !>+@ZNzK should be !>+@ZNzJ. Then all the small test cases work. But you can create test-cases, where the transposing fails (like described above). For this to work you need something like =.[.TZkK (fill the missing columns with empty strings) instead of =.TZ. Aug 3, 2016 at 21:34
• And try to not confuse yourself with Pyth. In your code you have two multiple variable that point to the same values (like K and @Q1). It was quite hard to track which variable is which value, ... And don't just copy the code. Keep it simple. The boolean trick =Y... may be a good idea for Python, but a simple I (if) would be much more readable (and also shorter). Aug 3, 2016 at 21:41
• Here's a really simple solution using mbomb007's code: Link It takes n on the first line (this way we don't have to assign the value to an extra variable, we can simply use Q). And w and h on the second line, which get immediately assigned to G and H with AE. Aug 3, 2016 at 21:41

# Stax, 27 bytes

é!L↑?∞S░♠╔)¥¼/ÿµ◄÷│♦╫Δò6√√╣


Run and debug it

It takes input on one line in the for {N} {H} {W}.

This program starts with a grid of spaces of the specified size. For each number from N .. 1, it attempts to do a single string replacement from a string of spaces of the appropriate size to the formatted number. If no replacement can be performed, then it tries again with a transposed grid.

z)A+*   create "grid" of spaces and newlines of specified size
,Rr     create range [n ... 1]
F       for each number execute the indented section; it will fit the value into the grid
$%z( make a string out of spaces the same length as the number; e.g. 245 => " " Y store the space string in register Y; this will be used as a search substring [# count the number of occurrences of y in the grid; the grid is still on the stack X store the count in register X; this will be used as a condition G jump to routine at closing curly brace y_$|e replace the first instance of y (the spaces) with the current number
end program
}       called routine jump target
C       pop top of stack; if it's truthy terminate routine
|jM|J   split on newlines; transpose; join with newlines


Run this one

# Pip, 59 bytes

Oy, Pip can be verbose for a golfing language sometimes...

YsXbRLcJnWa{T X#a~yx:Y(t:{J*Z:a^n}Jny)Y$.a.$'--a}x?(ty)y


Try it online!

### Ungolfed

Approach: store the grid as a newline-separated string, using regex to replace a run of spaces with the current number, and transposing if there isn't a run of spaces long enough.

; Command-line arguments are: a (the number), b (the width), c (the height)
; x is "", a falsey value

; Set up the initial string:
;  sXb  String of b spaces
;  RLc  Make a list of c of those
;  Jn   Join on newline
Y sXb RL c J n

; Define a function t that transposes a newline-separated string:
;  a^n  Split argument on newlines
;  Z    Zip (transposes, but results in a list of lists of characters)
;  J*   Join each of the sublists into a string
;  Jn   Join those strings on newline
t: {(J* Z (a^n)) J n}

; Loop while a is nonzero:
W a {

; Loop till a regex made of len(a) spaces matches in y:
T y ~  X#a

; Transpose y and set x to a truthy value to indicate that we've transposed it:
;  (t y)  Call the t function on y
;  Y      Assign the result back to y
;  x:     Assign it to x also
x: Y (ty)

; When we break out of the loop, we have matched the first run of len(a) spaces
; in y; now we want to replace that with the number a:
;  $ The part of the string before the match ; .a Concatenated to a ; .$'  Concatenated to the part of the string after the match
;  Y    Assign that to y
Y $.a.$'

; Decrement a
--a
}

; After the main loop, we need to transpose y back if we transposed it:
;  x?     Is x truthy?
;  (t y)  If so, transpose y
;  y      If not, return y as-is
x ? (ty) y

; The result is autoprinted.
`