32
6
A Flow Snake, also known as a Gosper curve, is a fractal curve, growing exponentially in size with each order/iteration of a simple process. Below are the details about the construction and a few examples for various orders:
Order 1 Flow Snake:
____
\__ \
__/
Order 2 Flow Snake:
____
____ \__ \
\__ \__/ / __
__/ ____ \ \ \
/ __ \__ \ \/
\ \ \__/ / __
\/ ____ \/ /
\__ \__/
__/
Order 3 Flow Snake:
____
____ \__ \
\__ \__/ / __
__/ ____ \ \ \ ____
/ __ \__ \ \/ / __ \__ \
____ \ \ \__/ / __ \/ / __/ / __
____ \__ \ \/ ____ \/ / __/ / __ \ \ \
\__ \__/ / __ \__ \__/ / __ \ \ \ \/
__/ ____ \ \ \__/ ____ \ \ \ \/ / __
/ __ \__ \ \/ ____ \__ \ \/ / __ \/ /
\ \ \__/ / __ \__ \__/ / __ \ \ \__/
\/ ____ \/ / __/ ____ \ \ \ \/ ____
\__ \__/ / __ \__ \ \/ / __ \__ \
__/ ____ \ \ \__/ / __ \/ / __/ / __
/ __ \__ \ \/ ____ \/ / __/ / __ \/ /
\/ / __/ / __ \__ \__/ / __ \/ / __/
__/ / __ \ \ \__/ ____ \ \ \__/ / __
/ __ \ \ \ \/ ____ \__ \ \/ ____ \/ /
\ \ \ \/ / __ \__ \__/ / __ \__ \__/
\/ / __ \/ / __/ ____ \ \ \__/
\ \ \__/ / __ \__ \ \/
\/ \ \ \__/ / __
\/ ____ \/ /
\__ \__/
__/
Construction
Consider the order 1 Flow Snake to be built of a path containing 7 edges and 8 vertices (labelled below. Enlarged for feasibility):
4____5____6
\ \
3\____2 7\
/
0____1/
Now for each next order, you simply replace the edges with a rotated version of this original order 1 pattern. Use the following 3 rules for replacing the edges:
1 For a horizontal edge, replace it with the original shape as is:
________
\ \
\____ \
/
____/
2 For a /
edge (12
in the above construction), replace it with the following rotated version:
/
/ ____
\ / /
\/ /
/
____/
3 For a \
edge (34
and 67
above), replace it with the following rotated version:
/
/ ____
\ \ \
\ \ \
\ /
\/
So for example, order 2 with vertices from order 1 labelled will look like
________
\ \
________ \____ \6
\ \ / /
\____ \5___/ / ____
/ \ \ \
4___/ ________ \ \ \7
/ \ \ \ /
/ ____ \____ \2 \/
\ \ \ / /
\ \ \3___/ / ____
\ / \ / /
\/ ________ \/ /
\ \ /
\____ \1___/
/
0___/
Now for any higher order, you simply break up the current level into edges of lengths 1 /
, 1 \
or 2 _
and repeat the process. Do note that even after replacing, the common vertices between any two consecutive edges are still coinciding.
Challenge
- You have to write a function of a full program that receives a single integer
N
via STDIN/ARGV/function argument or the closest equivalent and prints the orderN
Flow Snake on STDOUT. - The input integer is always greater than
0
. - There should not be any leading spaces which are not part of the pattern.
- There should be either no trailing spaces or enough trailing spaces to pad the pattern to fill the minimum bounding rectangle completely.
- Trailing newline is optional.
Fun Facts
- Flow Snakes is a word play of Snow Flakes, which this pattern resembles for order 2 and above
- The Flow and Snakes actually play a part in the pattern as the pattern is made up of a single path flowing throughout.
- If you notice carefully, the order 2 (and higher as well) pattern comprises of rotations of order 1 pattern pivoted on the common vertex of the current and the previous edge.
- There is a Non ASCII variant of Flow Snakes which can be found here and at several other locations.
This is code-golf so shortest code in bytes win!
Leaderboard
The first post of the series generates a leaderboard.
To make sure that your answers show up, please start every answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
If I understand correctly, the shapes 1,2,3 are enlarged 2x, so the bottom row in 2 should be made of 4 undescores, not 3. – edc65 – 2015-05-20T19:06:43.040
@edc65 The shapes in the examples are perfectly sized. If you are talking about the Construction part, yes, that is enlarged and there are 3 underscores so that the edge number takes up the 4th's place – Optimizer – 2015-05-20T19:09:07.483
But there is no edge numbers in shape 2 (in construction part, yes). The bottom of shape 2 should be equal to the bottom of shape 1. – edc65 – 2015-05-20T19:14:58.250
@edc65 Oh, there!. Fixed! – Optimizer – 2015-05-20T19:21:35.680
3I read the title as "Snow Flakes" and didn't even notice the real title until you called attention to the difference. – mbomb007 – 2015-05-20T19:25:42.453
Concerning trailing spaces, and specifically "enough trailing spaces to pad the pattern in a perfect square," was this intended to be "a bounding rectangle"? (the pattern is not square). If square was intended does it matter on which side (top or bottom) the padding is? – KSab – 2015-05-20T20:43:17.757
@KSab Fixed. How does it sound now ? – Optimizer – 2015-05-20T20:48:53.410
@Optimizer The pedant in me wants 'minimum bounding rectangle', though I think the meaning is pretty clear either way ;) – KSab – 2015-05-20T20:52:58.283
@AndersKaseorg Input is always greater than 0. Mentioned in the Challenge section too. – Optimizer – 2015-05-27T05:37:33.847