r/dailyprogrammer • u/Coder_d00d 1 3 • Sep 05 '14
[9/05/2014] Challenge #178 [Hard] Regular Expression Fractals
Description:
For today's challenge you will be generating fractal images from regular expressions. This album describes visually how it works:
For the challenge you don't need to worry about color, just inclusion in the set selected by the regular expression. Also, don't implicitly wrap the regexp in ^...$. This removes the need to use .* all the time.
Input:
On standard input you will receive two lines. The first line is an integer n that defines the size of the output image (nxn). This number will be a power of 2 (8, 16, 32, 64, 128, etc.). The second line will be a regular expression with literals limited to the digits 1-4. That means you don't need to worry about whitespace.
Output:
Output a binary image of the regexp fractal according to the specification. You could print this out in the terminal with characters or you could produce an image file. Be creative! Feel free to share your outputs along with your submission.
Example Input & Output:
Input Example 1:
256
[13][24][^1][^2][^3][^4]Output Example 1:
Input Example 2 (Bracktracing) :
256
(.)\1..\1Output Example 2:
Extra Challenge:
Add color based on the length of each capture group.
Challenge Credit:
Huge thanks to /u/skeeto for [his idea posted on our idea subreddit] (http://www.reddit.com/r/dailyprogrammer_ideas/comments/24ykjs/intermediatehard_regexp_fractals/)
9
u/KillerCodeMonky Sep 06 '14 edited Sep 06 '14
Coming to you live, in JavaScript:
http://codechaos.me/regex-fractals/
I swapped the colors from the example outputs, but I plan on making them configurable as the next step anyway...
UPDATE: Result is now displayed in an image, so it can be saved.
UPDATE 2: Added colors based on the size of the first matched group, if it exists.
UPDATE 3: Removed implicit line anchoring (
^and$) to match updated description. Some examples still need the.*wrappers to match previous output, such as the initial example.8
u/wadehn Sep 05 '14 edited Sep 06 '14
Python: Very interesting problem! This can be solved quite nicely with recursion.
Edit: Added colors, e.g.
.*(?:13|31)(.*)gives [http://imgur.com/fkqt8WT].Edit: Used integer division for Python 3 support.
My output images http://imgur.com/wvvRfol, http://imgur.com/s7uxpOf are pretty similar to the given images, but rotated and with inverted colors. Also, the given images are actually 512x512.
I don't know why the images are rotated, the order of the quadrants should be correct. Did I make a mistake?
4
u/MaximaxII Sep 05 '14 edited Sep 05 '14
I really liked this one. I'm actually amazed by the results, but also by how short my code is. I was expecting something much longer. The attribution of the quadrant numbers is based on the fact that they depend on whether the pixel's coordinates are odd or even (and to a certain extent, the quadrant-code for each pixel is based on whether it's coordinates are divisible by 2, 4, 6, 8, 16... or not).
Results (all images are in 2048x2048):
[13][24][^1][^2][^3][^4]
(.)\1..\1
.*1.*
.*(12|23|34).*
Challenge #178 Hard - Python 3.4
3
u/OffPiste18 Sep 05 '14
Here's a Scala solution. Rather than go the recursive route, I did some bit manipulation to calculate the quadrants string.
3
u/G33kDude 1 1 Sep 06 '14 edited Sep 06 '14
Golfed AutoHotkey solution. Uses the scala example's bit wizardy.
I could trim it down a little more, but I like it better this way
http://i.imgur.com/4rr6Hgq.png
Edit: Trimmed more
Edit: Done proper
3
u/marchelzo Sep 06 '14
Haskell: This one was really fun. It took me a while to get right but the end result is so fun to play with that it was worth it.
Sample output: http://i.imgur.com/rPCImyC.png
2
u/Metroids_ Sep 05 '14 edited Sep 05 '14
Here's my PHP. Recursive solution.
/fractal.php?size=1024®ex=[13][24][1][2][3][4] http://i.imgur.com/bEkZsun.png
2
u/Splanky222 0 0 Sep 06 '14 edited Sep 07 '14
Short and sweet in MATLAB using recursion, although it's pretty slow due to (1) the recursive algorithm being N2 logN and (2) MATLAB's high function-call overhead.
Also nice and simple in standard C++. This one outputs to a text file with a "0" for a black pixel and whitespace for a blank pixel.
2
u/[deleted] Sep 07 '14
My first attempt at a difficult daily Programmer challenge. I'm pretty sure it's pretty weak code at some places. But I'm still learning :)
2
u/skeeto -9 8 Sep 08 '14
POSIX C99. I'm a little late for my own challenge since I was away for the weekend. My proposal on the ideas subreddit has my original JavaScript solution. Here's one in C using POSIX regular expressions. It outputs a Netpbm PGM (P5) image. This means on a POSIX system you don't need any third-party libraries to compile and run.
It uses very little memory, practically constant-space. As-is it can output up to a 4 billion by 4 billion image (232 x 232) using only a few bytes of memory. It's also very fast.
No reason to show output images since they look like everyone else's.
1
u/basic_bgnr Sep 06 '14 edited Sep 07 '14
Python 2.7 using PIL
edit: output corrected
1
u/zeringus Sep 06 '14
An iterative solution in Ruby using ChunkyPNG
1
u/yoho139 Sep 07 '14
Java
Fun, if a bit easy for a hard challenge... Thanks to /u/G33kDude for explaining the basics of regex to me in IRC!
1
u/le_donger Sep 07 '14 edited Sep 07 '14
Java
My first submission to dailyprogrammer. Feels like I somewhat overcomplicated this by first building a quadtree with the values and then drawing it, both done recursively, but whatever. No color for now. For some reason I don't get output for the two input examples given, however for others I do. Anyone an idea what I messed up?
.*224.* 1024x1024
1
u/0x746d616e Sep 08 '14
Recursive solution in Go (with challenge). Backtracing regex doesn't work as it's not supported by Go's regexp package :(
Input example:
Output example: http://i.imgur.com/WTVVWhY.png
1
u/Litra Sep 16 '14
Can somebody ELIF5 to mentally challenged business developer who doesn't have a clue how fractals work and how regex will match with that concept
1
u/TieSoul 0 1 Sep 27 '14 edited Sep 27 '14
Ruby
First, some outputs
All are in 1024x1024
[13][24][^1][^2][^3][^4]
(.)\1..\1
((?:13|31|24|42)+)
([^3]+)(\1+)
.*4[^4][^4]2(.*)
Code
1
u/apgwoz Sep 30 '14 edited Oct 01 '14
Python version. Doesn't support coloring via capture size. No recursion, but excessive use of generators. Overall, pretty happy with it.
EDIT: Golfed a few lines in generate. Would love to simplify str2pt a bit...