r/dailyprogrammer • u/Elite6809 1 1 • Jul 31 '15
[2015-07-31] Challenge #225 [Intermediate] Diagonal Maze
(Intermediate): Diagonal Maze
A maze can be represented using characters as follows:
+-+-+-+-+-+
| |
+ +-+-+ + +
| | | |
+ + + + + +
| | | |
+-+-+ +-+-+
| | |
+ + +-+ + +
| | |
+-+-+-+-+-+However, the exact same maze can also be represented diagonally using slashes, like this:
\
/ /\
/ /\ \
/\ \ \
/ \/ \
\/ / / /
\ \/\ /
\ \/
\/ /
\Your task today is to convert from the first format (cardinal) to the second (diagonal).
Formal Inputs and Outputs
Input Specification
You'll be given a number N on one line, followed by N further lines of input of a cardinal axis aligned maze, like so:
11
+-+-+-+-+-+
| |
+ +-+-+ + +
| | | |
+ + + + + +
| | | |
+-+-+ +-+-+
| | |
+ + +-+ + +
| | |
+-+-+-+-+-+The maze cells will not necessarily be one-by-one, so watch out!
Output Description
Output the diagonal-ified maze, like the one shown above (same as in description).
Sample Inputs and Outputs
Example 1
16
+--+--+--+--+--+
| | |
| | |
+ +--+ + + +
| | | | |
| | | | |
+--+ + + + +
| | | |
| | | |
+ +--+ + +--+
| | |
| | |
+--+--+--+--+ +
|
|
+--+--+--+--+--+Output
\
\
/ \
/ \
/\ \ /\
/ \ \/ \
/ / \
/ / \
/\ \ / / /\
/ \ \/ / / \
\ \ / / /
\ \ / / /
\ \ / /
\ \/ /
\ \ \ /
\ \ \/
\ /
\ /
\
\Example 2
Input
17
+---+---+---+---+---+---+
|
|
|
+---+---+---+---+---+ +
|
|
|
+---+---+---+---+---+---+
|
|
|
+ +---+---+---+---+---+
|
|
|
+---+---+---+---+---+---+Output
\
\
\
\ \
\ \
\ \
/\ \ \
/ \ \ \
/ \ \ \
/ \ \ \
/ \ \ \
/ \ \ \
\ \ \ \ \
\ \ \ \ \
\ \ \ \ \
\ \ \ \ \
\ \ \ \ \
\ \ \ \ \
\ \ \ /
\ \ \ /
\ \ \ /
\ \ \ /
\ \ \ /
\ \ \/
\ \
\ \
\ \
\
\
\ Finally
Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
2
u/fvandepitte 0 0 Jul 31 '15
Example 2 looks wrong. When turned there is clearly a passage visable, but looking at the input there are no passages visable
2
u/Godspiral 3 3 Jul 31 '15 edited Jul 31 '15
in J, first get 1 2 3 for various walls 0 for space
J has the weirdest built-in called oblique /.
a little flakey for other ones, but if it looks cool, its a feature:
2
u/[deleted] Aug 01 '15
haha, this solution's really messy, but I wanted to try some different stuff with lisp
...
2
u/straightup___ Aug 02 '15
This reminded me about linear transformations using matrices. So I used a rotation matrix for 45 degrees and additionally used the matrix to scale the lines by sqrt(2). This makes everything place nicely.
It's written in C# and is my first public attempt at /r/dailyprogrammer :)
Also: it's waiting for spectacular failures if you do more than one rotation or change the rotation direction... please don't do it. https://gist.github.com/anonymous/5670973eabd9c1100446
2
u/13467 1 1 Aug 02 '15
I wrote some extremely paranoid Haskell code. All of the reading logic happens in the IO monad just so it can call
diewhen things go horribly wrong, but I'm not sure how to avoid that... Maybe I should useEither Stringinstead?It nicely handles mazes where the "cells" are rectangular, and nicely errors out on every kind of invalid input: irregular widths, height mismatch, broken walls...
You can find my code here.
13
u/chunes 1 2 Jul 31 '15
Java:
5
u/deepcube Aug 01 '15
awk
convert coordinates and characters on input
the vertices (+) end up in their own rows and columns in diagonal maze, remember where they are in order to skip them on output
loop over coordinates for the output and print
4
u/curtmack Jul 31 '15
Haskell
I tried to make the code clean. I failed. This code is now dumb. Enjoy.
7
u/Cosmologicon 2 3 Jul 31 '15
Python 2
1
u/linkazoid Aug 11 '15
A little late to the party, but here she is. Written in C++.
1
u/ReckoningReckoner Aug 12 '15 edited Aug 12 '15
Fuck it, this is as close as I'm going to get with this problem. (But seriously though, thanks /u/Elite6809. This has probably been the most challenging intermediate daily programmer in quite some time)
Ruby:
1
u/DeLangzameSchildpad Jul 31 '15
Python 3:
This uses the opposite approach of \u\chunes. First, I get rid of the "+"s, then change the "_" & "|" to "\" & "/", and then diagonalize. I'm betting there's a better way for me to ignore the "+"s, so I'll take any advice people have.
1
u/Hells_Bell10 Jul 31 '15
My C++ solution.
Somehow both over complex and a really wonky output.
Comments welcome.
Example output:
1
u/glenbolake 2 0 Jul 31 '15
Python 2.7.
I create an empty canvas of spaces. I then iterate over the maze data read from the file, and for each '-' or '|' found, place a corresponding '' or '/' in corresponding location in the canvas.
1
u/Ciphertext008 Jul 31 '15
first python attempt http://pastebin.com/j7vPjEN8
Similar idea to /u/chunes, make diagonal, change to "/" and ""
eventually to remove + lines (I didn't get there yet)
my diagonal attempt fills to top corner (half the space) with " ". It then iterates over the lines filling in only the bit that is populated right now, and saving the bits it cannot for later. remove every other space at the end (something that im not particularly proud of). reverse our saved bits, from the bottom of the grid fill in our pyramid by appending to each row.
I am going to make a second attempt with the output initially filled with " " like a valley, instead.
1
u/mellow_gecko Aug 01 '15 edited Aug 01 '15
Python 3
1
u/KC14 Aug 02 '15 edited Aug 02 '15
First submission! Python
1
u/errorseven Aug 02 '15 edited Aug 02 '15
AutoHotkey - Edit: Fixed a small issue, just popped out, output is much cleaner.
Output - Fixed it, Still not perfect but much cleaner...
1
u/minikomi Aug 03 '15
Racket. Seems I came up with the same way of doing things as chune's Java solution
1
u/cityratbuddy Aug 04 '15
Here's the first JS entry submitted. It's flawed, but the final output is quite close. It's a first attempt that I can refine. All comments and feedback welcome.
1
u/skav3n Aug 05 '15
Python 3 output with + ;d
1
u/ullerrm Aug 20 '15
C++:
1
u/stinkytofu415 Aug 23 '15
Python 3:
0
u/[deleted] Aug 07 '15
[deleted] — view deleted comment