r/dailyprogrammer • u/Elite6809 1 1 • Feb 03 '15
[2015-02-04] Challenge #200 [Intermediate] Metro Tile Meltdown
(Intermediate): Metro Tile Meltdown
In the continued name of backward-compatibility, Microsoft has released a version of their flagship operating system for VGA text-mode terminals. In this version of their operating system, rectangular tiles consisting of a single character are displayed on the screen, like so:
..........................................................................
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
................................bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
...................cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.............................jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee...............................................................
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
..........................................................................
Screen space with no tile is denoted by a period (.). Tiles can be made of any character other than periods (due to the reason given) and spaces.
However, the dev team forgot to add support for screen-readers! Now visually impaired users cannot determine the location of the tiles on their display. Your task is, given a tile display such as the one above, write a program to find the location and size of each rectangular tile on the screen, along with the character in it, and output it in a way that can be read by a screen reader. For example, one such tile in the above example is located at position (1, 1) on the screen (from the top-left), consists of the character a and is 30 characters wide and 8 characters tall.
Tiles
A tile will always be perfectly rectangular:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
There will never be a non-rectangular tile on the screen, or one that is not completely filled in. These are not single tiles:
..................................
.bbbbbbbbbb.........ccccccccccccc.
.bbbbbbbbb..........c...........c.
.bbbbbbbb...........c...........c.
.bbbbbbb............ccccccccccccc.
..................................
A tile is something completely bordered by empty space (.), so this is two separate tiles:
.....................................
.aaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa.
.aaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa.
.aaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa.
.aaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa.
.....................................
Lastly, if a tile is made of two regions of separate colours, then the input as invalid:
.....................................
.aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb.
.aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb.
.aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb.
.aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb.
.....................................
The above 'tile' is two separate tiles: one made of a, one made of b.
Handling of invalid input is undefined and thus mostly up to you; your program can try and make sense of the input if you want, but for the purpose of the challenge, assume all tiles will be rectangular, separated by empty space (.) and consisting of a single character.
Input and Output Description
Sample Input
You will first be given two numbers, like so:
74 30
These denote the width and height of the tile display in characters. You will then be given the tile display of that size via standard input, for example:
..........................................................................
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbb.ddddddddddddddddddddd.
...........................................bbbbbbbb.ddddddddddddddddddddd.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ddddddddddddddddddddd.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ddddddddddddddddddddd.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ddddddddddddddddddddd.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ddddddddddddddddddddd.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.......................
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ccccccccccccccccccccc.
.jjjjjjjjjjjjjjjjjjjjjjjjj.eeeeeeeeeeeeeee.bbbbbbbb.ccccccccccccccccccccc.
...........................eeeeeeeeeeeeeee.bbbbbbbb.ccccccccccccccccccccc.
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee.bbbbbbbb.ccccccccccccccccccccc.
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee................................
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.kkkkkkkkkkkkkk.eeeeeeeeeeeeeee.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii................................fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
.iiiiiiiiii.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.fffffffffffffff.gggggggggggggg.
..........................................................................
Sample Output
You are to print the location (with (0, 0) being the top-left), width, height and filled character of each tile on the screen, like this:
41×6 tile of character 'a' located at (1,1)
8×16 tile of character 'b' located at (43,1)
21×4 tile of character 'c' located at (52,13)
21×11 tile of character 'd' located at (52,1)
15×14 tile of character 'e' located at (27,8)
15×11 tile of character 'f' located at (43,18)
14×11 tile of character 'g' located at (59,18)
30×6 tile of character 'h' located at (12,23)
10×13 tile of character 'i' located at (1,16)
25×7 tile of character 'j' located at (1,8)
14×6 tile of character 'k' located at (12,16)
Sample Inputs and Outputs
Input
4 4
xx.z
xx..
..yy
z.yy
Output
2×2 tile of character 'x' located at (0,0)
1×1 tile of character 'z' located at (0,3)
2×2 tile of character 'y' located at (2,2)
1×1 tile of character 'z' located at (3,0)
Input
10 10
..........
.@@@@@.ss.
.@@@@@.ss.
.......ss.
.\\\\\.ss.
.\\\\\....
.\\\\\.\\.
.......\\.
./////.\\.
..........
Output
5×2 tile of character '@' located at (1,1)
5×3 tile of character '\' located at (1,4)
5×1 tile of character '/' located at (1,8)
2×4 tile of character 's' located at (7,1)
2×3 tile of character '\' located at (7,6)
Input
74 30
..........................................................................
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
................................bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.ddddddddddddddddd.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
...................cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.bbbbbbbbbb.kkkkkkkkkkkkkkkk.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.............................jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee.ffffff.cccccccccccc.hhhhhhhhhhhhhhhhhhhhhhhhhhh.jjjjjjjjjjjjj.
.eeeeeeeeee...............................................................
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
.eeeeeeeeee.gggggggggggggggggggggggggggggggggggggg.iiiiiiiiiiiiiiiiiiiiii.
..........................................................................
Output
30×8 tile of character 'a' located at (1,1)
17×3 tile of character 'd' located at (1,10)
10×15 tile of character 'e' located at (1,14)
6×7 tile of character 'f' located at (12,14)
38×7 tile of character 'g' located at (12,22)
12×11 tile of character 'c' located at (19,10)
10×16 tile of character 'b' located at (32,1)
27×3 tile of character 'h' located at (32,18)
16×16 tile of character 'k' located at (43,1)
22×7 tile of character 'i' located at (51,22)
13×20 tile of character 'j' located at (60,1)
Finally...
Got a good idea for a challenge? Head on over to /r/DailyProgrammer_Ideas, write it out, and we might post it on this subreddit!
We're currently recruiting some moderators to join our team. If you're interested, read the sticky by clicking here.
4
u/hutsboR 3 0 Feb 03 '15 edited Feb 04 '15
Clever little thing I thought of -
Dart:
Output:
2
u/Elite6809 1 1 Feb 03 '15
My solution in F#: https://gist.github.com/Quackmatic/fcdde8d7833d560ff63e
Thanks to
jose_on IRC for some tips on idiomatic F#. Nifty little language!7
u/adrian17 1 4 Feb 03 '15 edited Feb 03 '15
Python 3:
A bit shorter but less readable version:
3
u/fvandepitte 0 0 Feb 04 '15 edited Feb 04 '15
C++, don't know if it is good, but i get results... Feedback please..
Output:
EDIT: Cleaned up some lines
1
u/VikingofRock Feb 04 '15 edited Feb 04 '15
I'm having trouble understanding how this
works with this:
Is the former a valid input, even though the 'a' block and the 'b' block are not separated by
.s?2
u/mbcook Feb 04 '15 edited Feb 04 '15
Haven't gotten to do one of these in a while, this looked fun.
I used Haskell, it's posted here on GitHub: https://github.com/MBCook/MetroTile/blob/master/meltdown.hs
Output perfectly matches the samples, even handles touching tiles correctly.
I took this as an opportunity to practice folds. I figured I could use a fold (ended up using mapAccumL) to combine the rows in order to generate the list of tiles, worked out pretty well. Most of the source file is either datatypes to make things easy or the setup in the main function.
It took me about 1.5-2h, mostly because it's been a few months since I touched Haskell and had to keep looking things up. A good chunk of that time was spent on three annoying bugs.
2
u/krismaz 0 1 Feb 04 '15
Python 3:
If we take the additional precondition that no two tiles of the same character are ever touching, we can solve this with some pretty strange operations on the standard collections.
Doing it by for loops is obviously the way to go, but not nearly as fun.
2
u/lukz 2 0 Feb 04 '15
vbscript
Solved using one-dimensional array used to hold the image data.
Code
Sample output
2
u/yitz Feb 04 '15 edited Feb 04 '15
Haskell:
2
u/codeman869 Feb 06 '15 edited Feb 06 '15
Ruby, not happy with my solution because I had to take some hints and edited because I suck at formatting
locateTiles(4,4,"xx.z. xx... ..yy. z.yy. .....")
1
u/snarf2888 Feb 03 '15
Solution in C. Fun challenge!
1
u/dohaqatar7 1 1 Feb 03 '15
Java
There's nothing special about this solution. I passes over the grid looking for indices that aren't part of a tile. When one is found, a tile is constructed with that index as the upper left hand corner and all the indices inside the tile are as such.
1
u/dongas420 Feb 04 '15
Perl:
1
u/minikomi Feb 04 '15
Racket:
Results:
1
u/[deleted] Feb 04 '15
Good ol' Python 3.4, as per usual I spend a lot of time dilly dallying over small decisions and afterwards am still unsure about them! Ah well, still playing with the function annotations (not for actual type checking but just for documentation).
After looking at the other approaches mine is definitely inefficient! Ah well, still works :p
1
u/curtmack Feb 04 '15 edited Feb 04 '15
Clojure
This was fun. I had to spend some time optimizing the solution because I was on a laptop using Ideone to code it, so I had an execution timeout of 5 seconds. Cramming that 74x30 test case into 5 seconds took some doing.
Behavior on invalid inputs is undefined and it probably crashes somewhere.
Spoilered-out explanation of how it works, since I notice my algorithm is very different from what other people are doing:
1
u/ChiefSnoopy Feb 04 '15
Not as pretty as I'd hoped, but threw it together and it gets the job done.
Python 3
1
u/CzechsMix 0 0 Feb 04 '15 edited Feb 04 '15
C/C++ (depending on your preferred console output EDIT: and memory allocation syntax)
EDIT EDIT: decided I hated that cout string in printRects, abstracted it to printRect
1
u/Godspiral 3 3 Feb 04 '15 edited Feb 04 '15
In J, slightly different output in that x0,y0 and x1,y1 coordinates are provided (row column format) :
or last one
1
u/Syrak Feb 04 '15
Rust! Though probably far from idiomatic. This solution relies a lot on the formatting restrictions on the input. One thing that struck me was that
SliceExt::position_elemtakes a reference...1
u/Goggalor Feb 04 '15
C# solution. "Test1.txt", etc are the given tests copy/pasted to text files.
1
u/aceinyourface Feb 05 '15
Another Nim solution. It turned out a lot like some of the other Python solutions.
1
u/jmillikan2 Feb 05 '15 edited Feb 06 '15
More Haskell from a Haskell newbie. Tests out on my system. May be dumb - it was late when I started and later when I finished.
A squeezed version (tuples for types, less names) which is basically the same:
1
u/next4q Feb 05 '15
Java
1
u/beforan Feb 06 '15
A while before I had a chance to get round to this one, but it's done now! Was able to re-use bits from the flood fill challenge, since I essentially used the same recursive checks to find the tile bounds, once a tile character was detected.
Lua 5.2
1
u/efabs Feb 06 '15
Please critique! Decided to use while loops instead of for loops so that I could easily skip the interior of known rectangles. It also deals with invalid input where different symbols touch. Also the input just takes the ascii screen, not dimensions first
Python 2.7:
1
u/mrepper Feb 07 '15 edited Feb 21 '17
[deleted]
0.5840
1
u/The_Jare Feb 08 '15
Scala (practicing functional styles)
Relying on folds like this produces rather unreadable code, and I ended up having lots of compiler trouble with the various collection / sequence / iterator types and idiosyncracies. OTOH the algorithm itself worked on first try!
1
u/kornjaca01 Feb 11 '15
Java
1
u/pfif Feb 17 '15
Python 3.4 POO Solution. Maybe I am too fond of POO ... Feedback, anyone ?
https://gist.github.com/anonymous/8d94ae8dd8fbee3c8616
1
u/[deleted] Feb 20 '15
Python 2 solution
0
u/[deleted] Feb 03 '15
[deleted] — view deleted comment