CJam, 94 92 82 bytes
This is the 92 bytes version. 82 byte version follows.
l~1$,:L,:)m*{1bL=},\e!\m*{~W<{/(\e_}%}%{::+)-!},{{_,,\f<1fb}%2ew{:&,(},!}={{(2*'_*'[\']}/N}/
This partitions the bricks into every possible way and take only the one which is valid. Pretty brute force for now but still runs the last test case in about 10 seconds on the Java Interpreter on my machine.
Explanation:
The code is split into 5 parts:
1) Given an array of length L
, how all can we partition it into H
parts.
l~1$,:L,:)m*{1bL=},
l~ e# Read the input as string and evaluate it.
`$,:L e# Copy the array and take its length. Store that in L
,:) e# Get an array of 1 to L
m* e# Cartesian power of array 1 to L of size H (height of wall)
{1bL=}, e# Take only those parts whose sum is L
After this, we have all possible ways of splitting up our input array into H brick layers.
2) Get all permutations of the input array and then further get all partitions for all permutations
\e!\m*{~W<{/(\e_}%}%
\e! e# Put the input array on top of stack and get all its permutations
\m* e# Put the all possible partition array on top and to cartesian
e# product of the two permutations. At this point, every
e# permutation of the input array is linked up with every
e# permutation of splitting L sized array into H parts
{ }% e# Run each permutation pair through this
~W< e# Unwrap and remove the last part from the partition permutation
{ }% e# For each part of parts permutation array
/ e# Split the input array permutation into size of that part
(\ e# Take out the first part and put the rest of the parts on top
e_ e# Flatten the rest of the parts so that in next loop, they can be
e# split into next part length
After this, we have all possible layouts of the input bricks into an H
layers brick wall.
3) Filter out only those layouts whose brick lengths are same
{::+)-!},
{ }, e# Filter all brick layouts on this condition
::+ e# Add up brick sizes in each layer
)-! e# This checks if the array contains all same lengths.
After the end of this filter, all remaining layouts would be perfect rectangles.
4) Take out the first brick layout which matches the stability criteria
{{_,,\f<1fb}%2ew{:&,(},!}=
{ }= e# Choose the first array element that leaves truthy on stack
{ }% e# For each brick layer
_,, e# Create an array of 0 to layer length - 1
\f< e# Get all sublists starting at 0 and ending at 0
e# through length - 1
1fb e# Get sum of each sub list. This gives us the cumulative
e# length of each brick crack except for the last one
2ew e# Pair up crack lengths for every adjacent layer
{ }, e# Filter layer pairs
:& e# See if any cumulative crack length is same in any two
e# adjacent layers. This means that the layout is unstable
,( e# make sure that length of union'd crack lengths is greater
e# than 1. 1 because 0 will always be there.
! e# If any layer is filtered through this filter,
e# it means that the layer is unstable. Thus negation
After this step, we simply have to print the layout
5) Print the layout
{{(2*'_*'[\']}/N}/
{ }/ e# For each brick layer
{ }/ e# For each brick
(2*'_* e# Get the (brick size - 1) * 2 underscores
'[\'] e# Surround with []
N e# Newline after each layer
Try it online here
82 bytes
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g{{(2*'_*'[\']}/N}/
This is almost similar to the 92 byte version, except that it has a touch of randomness. If you have read the explanation for the 92 byte version, then in the 82 byte version, parts 3, 4 and 5 are exactly same, while instead of iterating over all permutations from part 1 and 2, this version simply randomly generates one of the permutation at a time, tests it using part 3 and 4, and then restarts the process if the tests of part 3 and 4 fail.
This prints the results very quickly for the first 3 test cases. The height = 5 test case is yet to give an output on my computer.
Explanation of the difference
l~:H;{e_mrH({H-X$,+(mr)/(\e_}%_::+)-X${_,,\f<1fb}%2ew{:&,(},+,}g
l~:H; e# Eval the input and store the height in H
{ ... }g e# A do-while loop to iterate until a solution is found
e_mr e# Flatten the array and shuffle it.
H({ }% e# This is the random partition generation loop
e# Run the loop height - 1 times to get height parts
H-X$,+( e# While generating a random size of this partition, we
e# have to make sure that the remaining parts get at least
e# 1 brick. Thus, this calculation
mr) e# Get a random size. Make sure its at least 1
/(\e_ e# Similar to 92's part 2. Split, pop, swap and flatten
_::+)- e# 92's part 3. Copy and see if all elements are same
X${_,,\f<1fb}%2ew{:&,(}, e# 92's part 4. Copy and see if layers are stable
+, e# Both part 3 and 4 return empty array if
e# the layout is desirable. join the two arrays and
e# take length. If length is 0, stop the do-while
The idea for this version was given by randomra (Get it ?)
Try this one online
n>1
and didn't like how it restricted test cases. Also, apparently there's precedent. \$\endgroup\$