Background
In the online graphing calculator Desmos, there is a certain shape that appears in the lower left portion of the graph in many high detail graphs, which the Desmos community has dubbed "Bernard". You can see an example of it in this graph and an isolated version of it in this graph. This shape is a consequence of the quadtree algorithm which Desmos utilizes to actually graph equations. I barely comprehend how it all works, but by descending into "deeper", or more detailed, quadrants, a more detailed Bernard can be created.
(Don't take my word for how Bernard is formed, I barely understand how it works myself.)
A screenshot of what Bernard looks like for posterity:
In this challenge, you will write code that will print out a 2d array version of a depth-n
Bernard.
How to create Bernard?
Given a positive integer n
, create a \$2^n\times2^n\$ grid of 1
's and 0
's.
If \$n=1\$, then fill in the \$2\times2\$ grid like so:
0 1
1 1
That is your output.
If \$n>1\$, then recursively fill in the grid in the following way:
- Split the grid into 16 blocks of size \$2^{n-2}\times2^{n-2}\$.
- Fill in the square regions according to the below diagram, and recurse on the region labelled
R
:
0 0 1 1
0 0 0 R
1 1 1 1
1 1 1 1
- If the region
R
is a \$1\times1\$ or a \$2\times2\$ region, then finish this step and output the resulting array. If the regionR
is a \$1\times1\$ region, then fill its only entry with1
. IfR
is a \$2\times2\$ region, then fill it in the following way:
0 1
1 1
- If step 3 does not apply, then repeat steps 1-4 but on region
R
.
Worked example: n=4
n
is 4, so we will have a \$16\times16\$ grid. Following step 1, we fill the grid in the following way (with R
denoting the region we will recurse on):
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 R R R R
0 0 0 0 0 0 0 0 0 0 0 0 R R R R
0 0 0 0 0 0 0 0 0 0 0 0 R R R R
0 0 0 0 0 0 0 0 0 0 0 0 R R R R
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The region R
is \$4\times4\$, so we recurse. The region R
should now look like this, with the new R
inside the grid denoting where to recurse:
0 0 1 1
0 0 0 R
1 1 1 1
1 1 1 1
Because R
is now \$1\times1\$, we fill it in, so it becomes:
0 0 1 1
0 0 0 1
1 1 1 1
1 1 1 1
Placing this back into the original \$16\times16\$ grid, we obtain the desired output:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Task
Given a positive integer n
as input, return a \$2^n\times2^n\$ 2d array which represents the depth-n
Bernard.
Test Cases
n=1
0 1
1 1
n=2
0 0 1 1
0 0 0 1
1 1 1 1
1 1 1 1
n=3
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1
1 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