14
3
Write a program or function that takes in a positive integer N (via stdin/command line/function arg) and prints or returns a string representation of a two dimensional random walk that is N steps long, drawn out of slashes: /
\
(plus spaces and newlines for spacing).
A 2D random walk starts at the origin of an infinite integer lattice. Then N times repeatedly, a cardinal direction (up, down, left, right) is chosen uniformly at random and the walker moves one unit in that direction. The resulting path taken is the random walk.
Here is a random walk for N = 6. Notice that it traverses back on itself when it reaches (-1, 3).
To draw this with slashes we essentially need to rotate the entire thing 45° clockwise. The axes and start and end points are not drawn in the slash version.
/
\
\
/\
A more complex walk like this (N = 20, though there's no way to tell):
Would become this:
/
/\/ /\
\/\/
/\/
\/
Your program needs to generate these type of slash versions of random walks. You must randomly choose each new direction the walk takes, so every running of the program for a certain N will almost definitely produce a different walk. (Pseudorandomness is fine.)
There should never be any empty lines above or below the lowest and highest slashes (except for one optional trailing newline), and there should never be empty columns of spaces before or after the leftmost and rightmost slashes.
So for N = 1, the output is always /
or \
, but never something like:
/
Trailing spaces are allowed as long as they don't go past the column of the rightmost slash.
The submission with the fewest bytes wins. Here's a handy byte counter.
so it's possible (although rare) that the output could be a single slash even when N = 20? – DaveAlger – 2015-04-01T01:39:17.643
2@DaveAlger Sure. Though if your program does that a lot I'd expect something is very wrong. – Calvin's Hobbies – 2015-04-01T01:40:26.210