14
1
Introduction
In this challenge, a 2×2 matrix is indexed like this:
0 1
2 3
We define a family of fractal-like patterns F(L)
, where L
is a length-n
list of these indices and F(L)
has size 2n-1 × 2n-1
.
- If
L == []
, thenF(L)
is the 1×1 pattern#
. If
L != []
, thenF(L)
is constructed as follows. LetP
be the pattern obtained fromL
with first element removed. Take four grids of size2n-1-1 × 2n-1-1
filled with periods.
, and replace the grid indexed byL[0]
with the patternP
. Then, glue the grids together using one layer of hashes#
between them. Here are diagrams for the four cases:L[0]==0 L[0]==1 L[0]==2 L[0]==3 #... ...# ...#... ...#... [P]#... ...#[P] ...#... ...#... #... ...# ...#... ...#... ####### ####### ####### ####### ...#... ...#... #... ...# ...#... ...#... [P]#... ...#[P] ...#... ...#... #... ...#
Example
Consider the input L = [2,0]
.
We begin with the 1×1 grid #
, and traverse L
from the right.
The rightmost element is 0
, so we take four copies of the 1×1 grid .
, replace the first one by #
, and glue them together with hashes.
This results in the 3×3 grid
##.
###
.#.
The next element is 2
, so we take four copies of the 3×3 grid of .
s, and replace the third one with the above grid.
The four grids are
... ... ##. ...
... ... ### ...
... ... .#. ...
and gluing them together with #
s results in the 7×7 grid
...#...
...#...
...#...
#######
##.#...
####...
.#.#...
This is our final output.
Input
Your input is a list L
of the indices 0, 1, 2, 3
.
You can take it as a list of integers, or a string of digits.
Note that it may be empty, and it may contain duplicates.
The length of L
is at most 5.
Output
Your output is the pattern F(L)
as a newline-delimited string.
Rules and scoring
You can write a full program or a function. the lowest byte count wins, and standard loopholes are disallowed.
Test cases
[]
#
[0]
##.
###
.#.
[3]
.#.
###
.##
[2,0]
...#...
...#...
...#...
#######
##.#...
####...
.#.#...
[1,1]
...#.##
...####
...#.#.
#######
...#...
...#...
...#...
[1,2,0]
.......#...#...
.......#...#...
.......#...#...
.......########
.......###.#...
.......#####...
.......#.#.#...
###############
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
[3,3,1]
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
###############
.......#...#...
.......#...#...
.......#...#...
.......########
.......#...#.##
.......#...####
.......#...#.#.
[0,1,2,3]
.......#...#...#...............
.......#...#...#...............
.......#...#...#...............
.......#########...............
.......#.#.#...#...............
.......#####...#...............
.......#.###...#...............
################...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
###############################
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
[0,0,1,2,3]
.......#...#...#...............#...............................
.......#...#...#...............#...............................
.......#...#...#...............#...............................
.......#########...............#...............................
.......#.#.#...#...............#...............................
.......#####...#...............#...............................
.......#.###...#...............#...............................
################...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
################################...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
###############################################################
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
In your example, why do you begin with the 1x1 grid
#
?L !=[]
in that example, as it has 1 or more elements. Does this mean that F(L) is always a#
at first? – R. Kap – 2016-04-10T20:10:43.8932@R.Kap Okay, the example is not very clear. The definition is recursive, so for
L = [2,0]
, you chop off the head and look at the patternF([0])
, then chop off the head of[0]
and look at the patternF([])
, which is the 1x1 grid#
. Then you use the chopped-off index0
on it to build the 3x3 pattern, and use the chopped-off index2
on that one to build the 7x7 pattern. To answer your question: yes, you always begin with the 1x1 grid since that's the base case of the recursion. – Zgarb – 2016-04-10T20:15:54.020Related reading – Sp3000 – 2016-04-11T11:51:00.973