Let's represent a standard masonry brick as [__]
(and ignore the fact that the top is open). When these bricks are stacked every other layer is offset by half a brick, as is usual in brick construction:
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
Thus each brick has at most six neighbors and it's impossible for two bricks to directly line up vertically.
The key point is that arrangements of these bricks are not mortared, but merely held together by gravity. So it is important that each brick in the structure is stable, otherwise the entire structure is unstable.
There are three ways an individual brick may be stable:
- Any brick on the ground (the lowest line of bricks) is stable.
Any brick that has two bricks directly below it is stable:
[__] <- this brick is stable [__][__] <- because these bricks hold it up
Any brick that has a brick both above and below it on the same side is stable:
[__] [__] [__] [__] <- these middle bricks are stable [__] [__] because the upper and lower bricks clamp them in [__] [__] [__] [__] <- these middle bricks are NOT stable [__] [__]
From these rules we can see, for example, the arrangement
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
[__][__][__][__]
is unstable because the top right brick is unstable, which is all it takes.
A brick structure is only stable if all of its bricks are stable.
Challenge
Your task is to write a function that takes in a brick structure string and returns a truthy value if the structure is stable, and a falsy value if unstable. (truthy/falsy definition)
The input string may be arbitrarily large but it will always be a rectangular grid of characters, with spaces filling areas void of bricks. The character grid width will be divisible by 4 but the height may be odd or even.
The brick grid always extends above and to the right of the lower left brick position:
.
.
.
BRK?BRK?BRK?BRK?
BRK?BRK?BRK?BRK?BRK?
BRK?BRK?BRK?BRK?
BRK?BRK?BRK?BRK?BRK? . . .
BRK?BRK?BRK?BRK?
BRK?BRK?BRK?BRK?BRK?
Depending on the structure, each BRK?
either represents a brick ([__]
) or empty space (4 spaces).
Notice that the half-brick cavities are filled with spaces to ensure that the character grid is rectangular.
Scoring
The shortest code in bytes wins.
Notes
- If desired you may use
.
instead of space as the empty space character. - The empty string is considered stable.
- If your language doesn't have functions you may use a named string variable as input and assign the result to another variable.
- If your language doesn't have strings you may do whatever seems appropriate for input.
Test Cases
Various test cases, separated by empty lines. For clarity .
is used instead of space for empty spaces.
Stable:
[__]
..[__]..
[__][__]
........[__]........
......[__][__]......
........[__]........
..[__][__]..
[__][__][__]
..[__][__]..
[__]....[__]
............[__]..
..[__][__][__][__]
[__][__][__][__]..
..[__][__][__][__]
[__][__][__][__]..
..[__]........[__]..
[__][__][__][__][__]
..[__][__][__][__]..
....[__][__][__]....
......[__][__]......
........[__]........
Unstable:
..[__]..
........
..[__]..
[__]....
..[__]..
....[__]
..[__][__]..
[__]....[__]
..[__][__]..
[__]....[__]
..[__][__][__][__]
[__][__][__][__]..
..[__][__][__][__]
[__][__][__][__]..
[__][__][__][__][__]
..[__][__][__][__]..
....[__][__][__]....
......[__][__]......
........[__]........
........[__]....
......[__][__]..
....[__][__]....
..[__][__]......
[__][__]........
..[__]..........
(you'll have to mentally stack those lines on top of each other. The point being that your rules allow structures whose centre of gravity is far offset from their point of contact with the ground. It should be possible to tighten them to avoid this, without needing a physics engine, if you felt like it.) \$\endgroup\$