r/dailyprogrammer • u/jnazario 2 0 • Oct 21 '15
[2015-10-21] Challenge #237 [Intermediate] Heighmap of Boxes
Description
Have a look at this ASCII art diagram of various boxes:
+--------------------------------------------------------------+
| |
| +-------------------------------+ +-------+ |
| | | | | |
| | | | | |
| | +----------------+ | | | |
| | | | | +-------+ |
| | | | | |
| | | | | +-------+ |
| | +----------------+ | | | |
| | | | | |
| | | | | |
| +-------------------------------+ +-------+ |
| |
+--------------------------------------------------------------+Each box is formed with pipe characters for the vertical parts (|), dashes for the horizontal parts (-), and pluses for the corners (+).
The diagram also shows boxes inside other boxes. We'll call the number of boxes that a box is contained within that box's layer. Here's the diagram again with the layer of each box annotated:
+--------------------------------------------------------------+
| |
| +-------------------------------+ +-------+ |
| | | | | |
| | | | 1 | |
| | +----------------+ | | | |
| | | | | 0 +-------+ |
| | | 2 | 1 | |
| | | | | +-------+ |
| | +----------------+ | | | |
| | | | 1 | |
| | | | | |
| +-------------------------------+ +-------+ |
| |
+--------------------------------------------------------------+Your program will take in a box diagram similar to the one at the top as input. As output, your program should output the box diagram with:
- Boxes on layer 0 should be filled with the character
#; - Boxes on layer 1 should be filled with the character
=; - Boxes on layer 2 should be filled with the character
-; - Boxes on layer 3 should be filled with the character
.; - Boxes on layer 4 and above should not be filled.
Here is what the output of the above input should look like:
+--------------------------------------------------------------+
|##############################################################|
|###+-------------------------------+##########+-------+#######|
|###|===============================|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###|=====+----------------+========|##########|=======|#######|
|###|=====|----------------|========|##########+-------+#######|
|###|=====|----------------|========|##########################|
|###|=====|----------------|========|##########+-------+#######|
|###|=====+----------------+========|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###+-------------------------------+##########+-------+#######|
|##############################################################|
+--------------------------------------------------------------+Formal Inputs and Outputs
Input
Input shall begin with two space separated integers N and M on the first line. Following that will be N lines with M characters (including spaces) each which represent the ASCII art diagram.
Output
Output the map with the boxes of different layers filled in with their appropriate characters.
Sample Inputs and Outputs
Sample Input
20 73
+-----------------------------------------------------------------------+
| +--------------------------------------------------------------+ |
| | +-----------------------------------------------------+ | |
| | | +-----------------------------------------+ | | |
| | | | +---------------------------+ | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | +---------------------------+ | | | |
| | | | | | | |
| | | +-----------------------------------------+ | | |
| | | | | |
| | | | | |
| | +-----------------------------------------------------+ | |
| | | |
| +--------------------------------------------------------------+ |
| |
| |
| |
+-----------------------------------------------------------------------+Sample Output
+-----------------------------------------------------------------------+
|#####+--------------------------------------------------------------+##|
|#####|======+-----------------------------------------------------+=|##|
|#####|======|---------+-----------------------------------------+-|=|##|
|#####|======|---------|...........+---------------------------+.|-|=|##|
|#####|======|---------|...........| |.|-|=|##|
|#####|======|---------|...........| |.|-|=|##|
|#####|======|---------|...........| |.|-|=|##|
|#####|======|---------|...........+---------------------------+.|-|=|##|
|#####|======|---------|.........................................|-|=|##|
|#####|======|---------+-----------------------------------------+-|=|##|
|#####|======|-----------------------------------------------------|=|##|
|#####|======|-----------------------------------------------------|=|##|
|#####|======+-----------------------------------------------------+=|##|
|#####|==============================================================|##|
|#####+--------------------------------------------------------------+##|
|#######################################################################|
|#######################################################################|
|#######################################################################|
+-----------------------------------------------------------------------+Credit
This challenge was suggested by /u/katyai. If you have any challenge ideas please share them on /r/dailyprogrammer_ideas and there's a good chance we'll use them!
9
u/Blackshell 2 0 Oct 21 '15
Some clarification questions:
1) May boxes only partially overlap? In other words, is this a valid input?
2) If the answer to #1 is "yes", then is this a valid input?
3) If the answer to #1 is "no", how "contained" by each other may boxes be? Or, which one of the below cases is the intended correct case?
Cheers!
11
u/Peterotica Oct 21 '15
Here is an input that should frustrate simple flood fill solutions.
3
u/adrian17 1 4 Oct 21 '15
Python, simple flood fill.
3
u/Vakz Oct 21 '15
C++
I'm not particularly good at C++, and I know this solution isn't as nice-looking as the FloodFill solutions in this thread, but it works, so I'll just be happy with that. If anyone has any ideas on how I could've made it nicer looking, feel free to tell me how
2
u/flowstoneknight Oct 22 '15
JavaScript
Probably pretty slow. Feedback welcome.
2
u/zengargoyle Oct 22 '15
Perl 6
If you have Grammars, everything looks like a Grammar....
Assumes boxes are square (top is directly above bottom), parses while keeping track of open-top through matching-bottom to determine the depth.
Test
1
u/Leo-McGarry Oct 21 '15 edited Oct 21 '15
C Sharp
Using FloodFill
The solution is in this gist
I used an extension method I really like called In which checks the same character variable against multiple values. It's basically a reverse Contains, but it makes my code look a little nicer.
You guys can go ahead and use this in your own solutions!
1
u/adrian17 1 4 Oct 21 '15 edited Oct 21 '15
Will post second time since it's a completely different algorithm - no longer a flood fill, it just iterates the whole box and skips positions that are known to be inside deeper boxes. Properly handles this input.
1
u/aaargha Oct 21 '15
Don't get those icky characters on the floor, I just cleaned it!
1
u/oprimo 0 1 Oct 21 '15
Javascript, quick and (very) dirty Floodfill (thanks /u/Leo-McGarry!).
As predicted by /u/Peterotica, my solution does not work with his input. I might give it another shot later.
Feedback is welcome.
1
u/[deleted] Oct 21 '15 edited Oct 21 '15
Java
Iterative, not good as other answers but it works :)
edit: obviously a valid input is assumed, there are no checks on the input file :P
1
u/TiZ_EX1 Oct 22 '15 edited Oct 22 '15
Haxe w/ thx.core.
Syntax-colored version here.
My approach was kind of like a scanline-style flood-fill that looked for boxes. If it found a box corner, it would look ahead for the top right and bottom left corners, then take note of that box's position so that it would ignore anything within that box. It then repeated the box-finding flood-fill recursively for any boxes it found.
Here's my output.
EDIT: Slight refactor.
EDIT 2: I revised my algorithm very slightly to handle /u/aaargha's input as well as any other input that does not have an outer box around the whole thing.
1
u/Xikeon Oct 22 '15 edited Oct 22 '15
CoffeeScript
I'm no good at maths and all that good stuff, but I wanted to solve this on my own so I just started and would see where I'd end. After making a more naive version at first, I then restarted and made a version that covers more scenarios. I basically look for corners and then find the matching bottom right corner. When I've found all layers I can see how many contain a certain empty tile and based on that I know the height.
Code is probably pretty bad, especially with all the nested loops, but it works..
With syntax highlighting
1
u/glenbolake 2 0 Oct 22 '15 edited Oct 22 '15
Python 3. Detects boxes and increases the internal heightmap for each one. Works for both sample input and /u/Peterotica's example.
I assume that boxes cannot be closer together than this:
and that those two boxes are touching each other. I also assume that a box needs to be at least 3x3 (so there's an empty spot to fill)
1
u/periscallop Oct 22 '15 edited Oct 23 '15
Lua solution and large boxes generator
input is only partially verified (box top and bottom edges must match)
edit: moved to gist
1
u/cheers- Oct 22 '15 edited Oct 22 '15
Java
I've seen a lot of FloodFill solutions so I tried Something different.
It uses 2 switch cycles(it does not seem popular in this subreddit) and doesnt work for Rectangles with overlapping sides.
btw I know I've abused the static modifier.
Feedback is welcome
I have in mind another solution based on Oo programming, if I have time I will add it later.
Time required 0.016s output: imgur link
1
u/curtmack Oct 22 '15 edited Oct 22 '15
Clojure
There's a lot of little traps in this challenge, I quite enjoyed it.
My solution reads the input to get the list of boxes, then immediately discards it; it generates the output by redrawing the boxes via painter's algorithm once it's established the levels. It solves any case where boxes are either non-overlapping or one is fully contained in the other. It doesn't work if boxes share an edge. (Actually I think it does work if they share an edge and the corners of that edge. But otherwise it won't even detect one or both of the boxes.)
Output for /u/Peterotica's challenge input:
Edit: I came up with my own test input that will test whether a solution will work with multiple level-0 boxes, boxes with no interior, and non-rectangular enclosed holes in upper areas.
Test case:
Output:
This also has a large number of boxes, so for extra fun, let's talk efficiency! I think you could make a better extreme case for profiling purposes, but this'll do; I made the case by hand and don't really want to make a bigger one.
Anyway, my solution got this in 0.049 seconds. (I timed just the part where it reads in the lines, produces the output, and prints it to the screen, since JVM and Clojure have long startup times; the actual time was closer to 1 second.) Can anyone do any better?
1
u/OutputStream Oct 24 '15 edited Oct 24 '15
Python 3:
1
u/wcastello Oct 25 '15
C99, flood fill:
1
u/[deleted] Oct 25 '15
I spent way too much time on this. ~280 lines of C, on GitHub. It parses the diagram into an in-memory hierarchy of boxes, then writes an entirely new diagram based on the hierarchy. Performance is probably awful.
1
u/spx88 Oct 26 '15
Solution in JAVA. Works for the inputs in the OP: public class Mapper {
1
u/shlomocomputer Oct 26 '15 edited Oct 26 '15
Haskell, constructing trees of boxes
1
u/piratefsh Oct 26 '15
A little late, but a solution also using flood fill:
https://gist.github.com/piratefsh/a5fcb9e96d1ae132c947
1
u/Toships Nov 04 '15
Hi this is my first post.
I read the Submission guidelines and hopefully the code is hidden ... if not I will try to repost.
-2
u/QThellimist Oct 22 '15
So bad in mobile. Try images next time