Is this tower of blocks going to fall?
Description
Given a stacking configuration for a series of square blocks, your function should return true if they are at equilibrium and false otherwise.
The block configuration for N blocks is provided as a input vector x with N elements listing the x-coordinates of the left-side of each block. The blocks are square with side equal to 1 (so the i-th block left side is at x(i) and its right side is at x(i)+1). The y-coordinates of each block are determined implicitly by the order of the blocks, which are dropped "tetris-style" until they hit the floor or another block.
All blocks are identical (same dimensions and mass) and perfectly smooth (friction is to be disregarded).
Intermediate positions may be unstable. You are only required to determine whether the final configuration is stable.
Examples:
Example (1)
x = [0 0.4];
The first block bottom-left corner is at (0,0) and the second block falls on top of it, with its bottom-left corner at (0.4 1). This configuration is stable so your function should return true.
Example (2)
x = [0 0.6];
The first block bottom-left corner is at (0,0) and the second block falls on top of it, with its bottom-left corner at (0.6 1). This configuration is unstable (the second block will fall) so your function should return false.
Example (3)
x = [0 1.5 0.6];
The three block bottom-left corner coordinates are (0,0) (1.5,0) and (0.6,1). This configuration is stable so your function should return true.
Example (4)
x = [0 .9 -.9 zeros(1,5)];
This configuration is unstable, but note that if instead of five we add a few more blocks on top of this at the 0 position that will keep the tower from falling!
Example (5)
x = cumsum(fliplr(1./(1:8))/2);
This configuration is stable (see the classic optimal stacking solution) so your function should return true.
Display
If you wish, you may display any given block configuration x using the code below:
clf; y=[]; for n=1:numel(x), y(n)=max([0 y(abs(x(1:n-1)-x(n))<1)+1]); end h=arrayfun(@(x,y)patch(x+[0,1,1,0],y+[0,0,1,1],rand(1,3)),x,y); text(x+.5,y+.5,arrayfun(@num2str,1:numel(x),'uni',0),... 'horizontalalignment','center');
Visit Block canvas for a related Cody problem.
473 Solvers
241 Solvers
454 Solvers
Return the first and last character of a string
1840 Solvers
Mandelbrot Number Test [Real+Imaginary]
13 Solvers