r/dailyprogrammer • u/fvandepitte 0 0 • Oct 04 '17
[2017-10-04] Challenge #334 [Intermediate] Carpet Fractals
Description
A Sierpinski carpet is a fractal generated by subdividing a shape into smaller copies of itself.
For this challenge we will generalize the process to generate carpet fractals based on a set of rules. Each pixel expands to 9 other pixels depending on its current color. There's a set of rules that defines those 9 new pixels for each color. For example, the ruleset for the Sierpinski carpet looks like this:
https://i.imgur.com/5Rf14GH.png
The process starts with a single white pixel. After one iteration it's 3x3 with one black pixel in the middle. After four iterations it looks like this:
https://i.imgur.com/7mX9xbR.png
Input:
To define a ruleset for your program, each of the possible colors will have one line defining its 9 next colors. Before listing these rules, there will be one line defining the number of colors and the number of iterations to produce:
<ncolors> <niterations>
<ncolors lines of rules>For example, the input to produce a Sierpinski carpet at 4 iterations (as in the image above):
2 4
0 0 0 0 1 0 0 0 0
1 1 1 1 1 1 1 1 1The number of colors may be greater than two.
Output:
Your program should output the given fractal using whatever means is convenient. You may want to consider using a Netpbm PGM (P2/P5), with maxval set to the number of colors in the fractal.
Challenge Input:
3 4
2 0 2 0 1 0 2 0 2
1 1 1 1 2 1 1 1 1
2 1 2 0 0 0 2 1 2Challenge Output:
https://i.imgur.com/1piawqY.png
Bonus Input:
The bonus output will contain a secret message.
32 4
30 31 5 4 13 11 22 26 21
0 0 0 0 0 0 21 24 19
31 28 26 30 31 31 31 30 30
18 14 2 1 2 3 1 3 3
28 16 10 3 23 31 9 6 2
30 15 17 7 13 13 30 20 30
17 30 30 2 30 30 2 14 25
8 23 3 12 20 18 30 17 9
1 20 29 2 2 17 4 3 3
31 1 8 29 9 6 30 9 8
17 28 24 18 18 20 20 30 30
26 28 16 27 25 28 12 30 4
16 13 2 31 30 30 30 30 30
20 20 20 15 30 14 23 30 25
30 30 30 29 31 28 14 24 18
2 2 30 25 17 17 1 16 4
2 2 2 3 4 14 12 16 8
31 30 30 30 31 30 27 30 30
0 0 0 5 0 0 0 13 31
2 20 1 17 30 17 23 23 23
1 1 1 17 30 30 31 31 29
30 14 23 28 23 30 30 30 30
25 27 30 30 25 16 30 30 30
3 26 30 1 2 17 2 2 2
18 18 1 15 17 2 6 2 2
31 26 23 30 31 24 30 29 2
15 6 14 19 20 8 2 20 12
30 30 17 22 30 30 15 6 17
30 17 15 27 28 3 24 18 6
30 30 31 30 30 30 30 27 27
30 30 30 30 30 30 30 30 30
30 30 27 30 31 24 29 28 27Credits:
This idea originated from /u/Swadqq; more at The Pi Fractal.
7
u/lukz 2 0 Oct 04 '17
Z80 assembly
Sierpinski carpet rules are encoded into the program, and the number of iterations as well. You can change the initial contents of the
numitervariable to obtain a different number of iterations.It prints on the standard output using letters 0 and 1. Assembled program size is 126 bytes.
Sample output from a CP/M system in an emulator - image.
Source:
10
u/gandalfx Oct 04 '17 edited Oct 04 '17
Python 3
I read input from stdin and dump output to stdout (P2 formatted) so you can pipe it into a file. Replacing
sys.stdoutwith an actual file is trivial (if anyone wants to see it let me know).Note that I do not magnify the result, so for the challenge input the resulting image is only 81x81 pixels in size.
Bonus result (SPOILER!): https://i.imgur.com/lDr6R8v.png
3
u/mn-haskell-guy 1 0 Oct 04 '17 edited Oct 04 '17
Something you can run in your browser:
https://codepen.io/anon/pen/JrOqBX
2
u/skeeto -9 8 Oct 04 '17
C. I wrote up this challenge, and the bonus input was creating using the same generic algorithm described in Swadqq's article. That code is here.
Here's another interesting input: lenna.txt
My actual solution for the challenge:
2
u/TheBlackCat13 Oct 04 '17 edited Oct 04 '17
Python3, numpy, and matplotlib solution. The data is given as a single string
lines.Results: example, challenge, bonus
Edit: fixed colormap
2
u/speedster217 Oct 05 '17 edited Oct 05 '17
Clojure
Reads from stdin and prints result to stdout. Pretty disappointed with how unreadable this ended up being. Too many maps within maps
2
u/isowosi Oct 05 '17 edited Oct 05 '17
Dart
Web Version - canvas resizes depending on max number of iterations and every iteration is displayed
2
u/sirnamlik Oct 05 '17 edited Oct 05 '17
JAVA
example output
challenge output
bonus output
lenna output Lenna input thanks to u/skeeto.
The imgur images have been enlarged by a factor of 10. Due to the example input only giving a 81 on 81 pixels size image.
2
u/gabyjunior 1 2 Oct 05 '17
C wrapper that generates html page running the simulation (showing each iteration).
Bonus
Lenna
1
u/goodygood23 Oct 04 '17
R
Challenge result
Bonus result
1
u/curtmack Oct 04 '17
Common Lisp
Super sloppy, but it does the job. Takes the problem via stdin and writes the PGM to stdout. Unlike most of my other answers, this one doesn't loop indefinitely; it handles one problem and then exits. (Making it loop and handle EOF nicely is kind of a pain because of the large number of interdependent inputs.)
1
u/mcbears Oct 04 '17
J
Bonus output: https://i.imgur.com/48Dg65Q.png
1
u/[deleted] Oct 04 '17
Python 3
There is probably a lot that could have been done better since this is my first time working with the numpy and PIL modules but the code seems to work. Any feedback would be highly appreciated.
1
u/ethorad Oct 05 '17
I think the picture you have for the fourth iteration in the example has the colours inverted. Going by the rules, a black square will always stay solid black, so the central but should remain black. The picture has this white though
1
u/Daanvdk 1 0 Oct 05 '17
C
1
u/Daanvdk 1 0 Oct 05 '17 edited Oct 05 '17
Haskell
1
u/FusionX Oct 05 '17 edited Oct 05 '17
I'm somewhat a beginner to programming and wanted to do this without relying on any external packages. The code is poorly written, so I apologize for that in advance.
If you have any suggestions on how I can improve the code, please do reply.
Python 3
1
u/octolanceae Oct 06 '17
Python3
Tested for both challenge and bonus input. The output is correct. I don't have a place to host images so not linking the plot outputs.
I think there are couple of things I can do to tighten the code up. Will ponder it over the weekend when I have some more time.
Feedback is welcome.
1
u/jacobmcnamee Oct 06 '17
C
In-place rendering of each iteration. Prefers multiply over divide and modulo, using only a single divide per iteration.
https://gist.github.com/jacobmcnamee/d0cfc9a2492b48bffaf293e62e0ba886
1
u/zookeeper_zeke Oct 06 '17 edited Oct 06 '17
I coded my solution up in C. I used "flat" 2-D arrays. Tweaking the following line gave me more trouble than it should have:
As for the secret message, can somebody explain to me what it is?
Here's the solution:
1
u/[deleted] Oct 13 '17
C
Very late to the party but this was a very fun challenge :)
1
u/hobotbobot Oct 14 '17 edited Oct 14 '17
Scala. I just have started learning it, so I feel like there is a lot to be improved. It takes output from the stdin and prints the output to stdout using console escape sequences for coloring.
1
u/swagglikerichie Oct 17 '17
I don't understand how you get colors after the first line