Maze generating one liner

15

1

The famous C64 basic one liner

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

prints a maze of slashes and backslashes.

\\/\\\//\/\////\\/\/
\/\///\\///////\//\/
/\\\//\//\////\\//\\
\////\//\//\/\\\\\\/
/\/\\///\\\\/\\\\/\\
\/\//\\\\\\//\/\////
/\//\\///\/\///\////
\/\\\//\\/\\\//\\/\/
//////\\/\\/\/\/\///
\\/\/\\////\/\/\\/\/

Read in such maze made of diagonal walls from stdin and print out the same maze with horizontal and vertical walls consisting of the wall character "#"

For example the small maze

/\\
\\/
///

translates to

     #####
     #   #
     # # # #
     # # # #
 ##### # # #
       #   #
   #########

     #####    

To be precise, each isolated wall segment has the length of five characters, adjacent wall segments share a corner. Moving a character to the right/left/top/down in the matrix of slashes and backslashes corresponds to a diagonal translation by 2 characters in vertical and 2 characters in horizontal direction in the #-matrix.

mschauer

Posted 2015-09-25T00:02:09.370

Reputation: 1 348

Another output example would probably be useful. And I expect the title should be "one liner". – Calvin's Hobbies – 2015-09-25T00:06:23.453

Will the input maze always be a rectangle? Could you should a larger example so we can see the spacing? – xnor – 2015-09-25T00:06:31.393

2Welcome to Programming Puzzles & Code Golf Stack Exchange! Great first challenge, but a few things: can input/output be something other than STDIN/STDOUT (ex. as a function argument and return value)? Can lines be separated via a character other than newlines? – Doorknob – 2015-09-25T00:08:07.240

The input will be an n times m rectangle. – mschauer – 2015-09-25T00:08:31.627

2Using stdin and stdout is obligatory if possible, otherwise "the closest equivalent." Are there reasons to weaken the newline-assumption? – mschauer – 2015-09-25T00:13:32.143

@PeterTaylor: Bonus, but not obligatory: handle ' ' (space) to procure a general slash image inverter. – mschauer – 2015-09-25T09:49:50.220

Is trailing whitespace (newlines and/or spaces) allowed in the output? – Zgarb – 2015-09-25T18:09:04.023

@zgarb: yes xxxxxxxxx – mschauer – 2015-09-25T18:23:12.937

@codegolf.stackexchange: Why am I not allowed to be concise? Why is there a minimum length of a comment - in this very place? – mschauer – 2015-09-25T19:30:07.837

Ok, how about preceding whitespace? – Zgarb – 2015-09-25T19:53:41.437

At most 5 preceding whitespace in front of the lower left corner of the original picture, no preceding newline. – mschauer – 2015-09-25T20:43:17.487

reasons to weaken the newline-assumption: in JavaScript inside a browser console, I can get input (stdin like) using prompt. But prompt does not manage multiline input well – edc65 – 2015-09-26T23:25:54.667

You can save some memory by re-writing the program as follows 0 PRINTCHR$(205.5+RND(1));:GOTO – Shaun Bebbers – 2017-01-26T12:00:46.167

Answers

5

Python 3, 226 224 bytes

My first Python golf, so probably very sub-optimal. It produces a whole lot of trailing whitespace, but there are no preceding newlines, and at most two preceding spaces. The input needs to be given by hand from command line (maybe someone knows a shorter way to get multiline input in Python...).

e,z,s=enumerate,'0',list(iter(input,""))
p=''.join(s)*5
r=[len(p)*[' ']for _ in p]
for y,l in e(s):
 for x,c in e(l):
  for i in range(-2,3):r[2*(x+y+(s>[z]))+i*(c>z)][2*(x+len(s)-y)+i*(c<z)]='#'
for l in r:print(''.join(l))

The idea is to initialize a huge array of spaces r, then iterate through the input and replace the spaces with # as needed, and finally print the whole array. A trick I used is to compare characters to z = '0' instead of testing equality to '/' or '\', which saves a bunch of bytes.

Zgarb

Posted 2015-09-25T00:02:09.370

Reputation: 39 083

1

Julia, 258 bytes

A functional solution...

A=split(readall(STDIN))
q(i,j)=fld(i-1,j)
n,^ =A[].(3),q
f(i,j)=try A[1+i^5][1+j^5]<'0'?(i+j)%5==1:(i-j)%5==0catch 0end
h(i,j)=f(i+i^4,j)|f(i+(i-1)^4,j)
g(i,j)=h(i,j+j^4)|h(i,j+(j-1)^4)
for i=1:6length(A),j=-n-5:2n;print(" #"[1+g(i-j,i+j)],j==2n?"\n":"")end

In order of appearance: f covers '/' and '\' by their 5*5 bit patters, h folds every fifth and following line into a single line (recall "adjacent wall segments share a corner") and g does the same for the columns. Finally, i-j,i+j rotates the picture.

mschauer

Posted 2015-09-25T00:02:09.370

Reputation: 1 348

1

JavaScript (ES6), 258

A function with the maze as a parameter, returning the output.

Unsure if it's valid, due to the input/output rules (it was fun anyway)

f=m=>([...m].map(c=>{if(c<' ')x=sx-=2,y=sy+=2;else for(x+=2,y+=2,d=c>'0',i=y-3*d,j=x-3*!d,c=5;c--;)o[i+=d][j+=!d]='#';},w=m.search`
`,h=m.match(/\n/g).length,sy=y=0,sx=x=h*2,o=Array(z=(w+h+1)*2).fill(' ').map(x=>Array(z).fill(x))),o.map(r=>r.join``).join`
`)

// LESS GOLFED

U=m=>(
  w=m.search`\n`,
  h=m.match(/\n/g).length,
  o=Array(z=(w+h+1)*2).fill(' ').map(x=>Array(z).fill(x)),
  sy=y=0,
  sx=x=h*2,
  [...m].forEach(c=>{
    if(c<' ')x=sx-=2,y=sy+=2
    else for(x+=2,y+=2,d=c>'0',i=y-3*d,j=x-3*!d,c=5;c--;)o[i+=d][j+=!d]='#';
  }),
  o.map(r=>r.join``).join`\n`  
)

// TEST
out=x=>O.innerHTML+=x+'\n'

test=`\\\\/\\\\\\//\\/\\////\\\\/\\/
\\/\\///\\\\///////\\//\\/
/\\\\\\//\\//\\////\\\\//\\\\
\\////\\//\\//\\/\\\\\\\\\\\\/
/\\/\\\\///\\\\\\\\/\\\\\\\\/\\\\
\\/\\//\\\\\\\\\\\\//\\/\\////
/\\//\\\\///\\/\\///\\////
\\/\\\\\\//\\\\/\\\\\\//\\\\/\\/
//////\\\\/\\\\/\\/\\/\\///
\\\\/\\/\\\\////\\/\\/\\\\/\\/`
out(test),out(f(test))
<pre id=O></pre>

edc65

Posted 2015-09-25T00:02:09.370

Reputation: 31 086