34
3
Overview
Write a program that prints out simple fractal patterns given a bit pattern encoding the fractal, plus the per-generation scale factor of the fractal and number of generations.
Explanation
Here is an ASCII representation of the Sierpinski Carpet:
Generation 0:
#
Generation 1:
# # #
# #
# # #
Generation 2:
# # # # # # # # #
# # # # # #
# # # # # # # # #
# # # # # #
# # # #
# # # # # #
# # # # # # # # #
# # # # # #
# # # # # # # # #
Generation n+1 of the ASCII Sierpinski Carpet is made up of a 3x3 grid containing 8 copies of generation n, with the central element of the grid missing.
So, because it is defined using a 3x3 grid and gets 3 times bigger in width and height each generation, we can say it has a scale factor of 3.
We could define a bit pattern for the Sierpinski carpet by numbering the elements in the 3x3 grid from 0 to 8, top-to-bottom, left-to-right, and setting the corresponding bit of an integer if generation n+1 contains a copy of generation n at that grid position:
bit: place value: bit pattern: bit value:
0 1 2 1 2 4 1 1 1 1 2 4
3 4 5 8 16 32 1 0 1 8 0 32
6 7 8 64 128 256 1 1 1 64 128 256
integer value = 1 + 2 + 4 + 8 + 32 + 64 + 128 + 256 = 495
For a scale factor of 2, the bit pattern would be arranged like this:
0 1
2 3
and so on.
Your task is to write a program that accepts a bit pattern in this form, a scale factor (e.g. 3 for the Sierpinski Carpet) and a generation number and outputs an ASCII fractal.
Input
Your program should accept 3 integers in the following order: a bit pattern, a scale factor (ranging from 2 to 5, inclusive) and a generation count (ranging from 0 to 5, inclusive).
You do not need to perform any input validation on these values and it's perfectly fine if the program works for values larger than the ranges specified.
The inputs can be passed in any form (tuples, comma/space-separated list, etc)
Output
The program should output a fractal made up of the #
character followed by a space in positions where the fractal is defined, double-spaces where it is not, and a newline character at the end of each line, either printing them out or returning a string from a function.
Examples
Input:
495,3,3
Output (Sierpinski Carpet generation 3):
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
Input:
7,2,5
Output (Sierpinski Triangle):
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # #
# # # #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # #
# # # #
# # # # # # # #
# # # #
# # # #
# #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # #
# # # #
# # # # # # # #
# # # #
# # # #
# #
# # # # # # # #
# # # #
# # # #
# #
# # # #
# #
# #
#
Input:
325,3,3
Output (Cantor Dust):
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
Input
186,3,3
Output (Vicsek fractal):
#
# # #
#
# # #
# # # # # # # # #
# # #
#
# # #
#
# # #
# # # # # # # # #
# # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # #
# # # # # # # # #
# # #
#
# # #
#
# # #
# # # # # # # # #
# # #
#
# # #
#
Input:
279,3,3
Output (example of an asymmetrical fractal):
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # #
# # #
# # # # # # # # #
# # #
# # #
# # # # # # # # #
# # #
# # #
# # #
#
#
# # #
#
#
# # # # # # # # #
# # #
# # #
# # #
#
#
# # #
#
#
etc.
Notes:
- This is code-golf so the shortest answer in bytes wins
- Your program can be either a stand-alone or a function that is called with the 3 input parameters and returns (or prints) a string
- Generation 0 is defined as
#
(a#
followed by a space) even for a bit pattern of 0. - A trailing newline on the last line is optional but permitted, as is any amount of trailing white-space on each line.
3+1, I liked this in sandbox and I like it more here, with the symbol changed from
"##"
to"# "
. I see the one trailing space at end of line is included in your examples, is it required? . Per the last rule I would assume it's optional, but the fact that you require a trailing space for generation 0 makes me wonder. Also I think you should indicate the max whitespace and newlines (you have it plural) allowed. As an extreme example I could always start with an array of 5^6=15625 lines of 2*5^6 spaces then substitute the#
s. In most input cases that's an enormous amount of unused whitespace – Level River St – 2015-08-11T06:36:32.440@steveverrill I don't require the trailing space when outputting generation 0, however the trailing space is part of its definition, which subsequent generations are defined in terms of. The plural of newlines was a typo, fixed. – samgak – 2015-08-11T07:17:23.677
Could you post the expected output for something less symmetric, such as
279,3,3
? – aditsu quit because SE is EVIL – 2015-10-10T17:28:17.037@aditsu sure, see edited question – samgak – 2015-10-11T06:50:18.340