30
4
I was messing around with infinite resistor networks (long story) when I came across the following interesting recursive pattern:
|-||
|---
Each instance of this pattern is twice as wide as it is tall. To go from one level of the pattern to the next, you break up this rectangle into two sub-blocks (each of which is a NxN square):
AB =
|-||
|---
so A =
|-
|-
and B =
||
--
These halves are then duplicated and rearranged according to the following pattern:
ABAA
ABBB
giving
|-|||-|-
|---|-|-
|-||||||
|-------
Challenge
Write a program/function which, given a number N
, outputs the N
th iteration of this recursive design. This is golf.
I/O format is relatively lenient: you may return a single string, a list of strings, a 2D array of characters, etc. Arbitrary trailing whitespace is allowed. You may also use either 0 or 1 indexing.
Examples
The first several iterations of the pattern are as follows:
N = 0
|-
N = 1
|-||
|---
N = 2
|-|||-|-
|---|-|-
|-||||||
|-------
N = 3
|-|||-|-|-|||-||
|---|-|-|---|---
|-|||||||-|||-||
|-------|---|---
|-|||-|-|-|-|-|-
|---|-|-|-|-|-|-
|-||||||||||||||
|---------------
N = 4
|-|||-|-|-|||-|||-|||-|-|-|||-|-
|---|-|-|---|---|---|-|-|---|-|-
|-|||||||-|||-|||-|||||||-||||||
|-------|---|---|-------|-------
|-|||-|-|-|-|-|-|-|||-|-|-|||-|-
|---|-|-|-|-|-|-|---|-|-|---|-|-
|-|||||||||||||||-|||||||-||||||
|---------------|-------|-------
|-|||-|-|-|||-|||-|||-|||-|||-||
|---|-|-|---|---|---|---|---|---
|-|||||||-|||-|||-|||-|||-|||-||
|-------|---|---|---|---|---|---
|-|||-|-|-|-|-|-|-|-|-|-|-|-|-|-
|---|-|-|-|-|-|-|-|-|-|-|-|-|-|-
|-||||||||||||||||||||||||||||||
|-------------------------------
I wonder if there is some short algebraic way to compute this structure.
What do you mean by "algebraic"? – user202729 – 2018-02-09T04:27:36.860
4@user202729 Like maybe there's some "simple" math formula
f(n,x,y)
which can directly calculate whether a given coordinate should contain-
or|
. It might involve modulo operations or bitwise operations. The techniques I've seen so far all involve cutting/joining arrays as shown in the spec. – PhiNotPi – 2018-02-09T04:35:56.1603
f(x,y)
also works, since ifx,y
is valid then the result doesn't depend onn
– amara – 2018-02-09T04:47:59.340Found a recursive method to algebraically calculate the required symbol, but the implementation is far longer than my JS answer (I mean, 50% longer). – Shieru Asakoto – 2018-02-09T07:44:55.383
2Can the output be 1-indexed, i.e. input 1 giving
|-
? – Zgarb – 2018-02-09T11:19:35.473@Zgarb I'll allow it. – PhiNotPi – 2018-02-09T13:32:29.057
2Is this loss? – qwr – 2018-04-27T15:11:47.270