30
9
The Hilbert curve is a space filling fractal that can be represented as a Lindenmayer system with successive generations that look like this:
Thanks to http://www.texample.net/tikz/examples/hilbert-curve/ for the image.
Goal
Write the shortest program possible (in bytes) that takes a positive integer n from stdin and draws the nth order Hilbert curve to stdout using only forward slash, backward slash, space, and newline.
For example, if the input is 1
the output must be
\
\/
If the input is 2
the output must be
/
\/\
/\ \
/ /\/
\ \
\/
If the input is 3
the output must be
\
/\/
/ /\
\/\ \ \
/\ / / /
/ / \/ \/\
\ \/\ /\ \
\/ / / / /\/
/\/ / \ \
\ \/\ \/
\/\ \
/ /\/
\ \
\/
And so on. (They look nicer if you paste them into something with less line spacing.)
The output should not contain newlines above or below the extremities of the curve, nor any trailing spaces on any lines.
Golfed this a bit more, hope you don't mind: http://ideone.com/kvcPWT - the
– Ventero – 2014-07-22T07:12:28.863.map(&:rstrip)
had to be added to fulfill the "no trailing spaces" requirement.@Ventero Thank you. Hope you don't mind that I took your solution - you may even discard the parantheses around the map argument. – Howard – 2014-07-22T07:25:32.860
Ah, of course! I also just realized that it's possible to inline the definition of
x
and shorten the assignment toy
andd
, for a total of 205 characters (see the same link as before). – Ventero – 2014-07-22T07:27:36.923