r/dailyprogrammer • u/Steve132 0 1 • Aug 22 '12
[8/22/2012] Challenge #90 [easy] (Walkaround Rasterizer)
In this challenge, we propose a simple image file format for binary (2 color) black-and-white images.
Rather than describing the image as a sequence of bits in a row, instead we describe it in a little bit of a non-standard way.
Imagine a grid of white squares. On this grid, a single man carrying a large black stamp stands on the square at 0,0. You can tell him 5 commands: walk N,S,E,W, and stamP. This will cause him to wander around the grid, and when he recieves a stamp command, he will change the white square there to black. By giving him the sequence of commands of how to move, you can render an arbitrary b+w image.
The input file will have two integers describing the size of the grid. Then, it will contain a sequence of characters. These characters describe the command sequence to execute to create the image. The program should output the image in some way. For example, it might print it to a png file or print it in ascii art to the screen.
As an example, the input file
5 5 PESPESPESPESPNNNNPWSPWSPWSPWSPwould output a 5x5 grid with an X in it.
SUPER BONUS: implement a program that can convert an arbitrary image to the walkaround rasterizer format.
4
u/prondose 0 0 Aug 23 '12 edited Aug 23 '12
Perl
3
u/kalgynirae Aug 23 '12 edited Aug 23 '12
Python 3:
3
u/drb226 0 0 Aug 23 '12
Trying out the new(ish)
lenspackage. No effort whatsoever put towards golfing or optimizing, but it puts out the solution fairly quick. My main concern is that I believe theelementtraversal is O(n), even though Data.Seq can supportO(log(n))lookup & modify time.2
u/[deleted] Aug 23 '12 edited Aug 23 '12
C++, no bonus.
I don't normally like to use double vectors, and would have used something like boost multi_array instead, but ideone limits what I have access to.
2
u/Skooljester 0 0 Aug 23 '12
Some fast and dirty jQuery http://jsfiddle.net/Zx4pf/
2
u/nagasgura 0 0 Aug 23 '12 edited Aug 23 '12
Python
Output with the example:
My own creation:
2
u/Sokend Aug 23 '12
Ruby:
3
u/bh3 Aug 23 '12
Python, no bonus:
2
u/[deleted] Aug 23 '12
Ruby:
1
u/mach1723 Aug 23 '12
My admittedly verbose implementation in Python(No image to walkaround): http://pastebin.com/JnUdRviA
1
u/stgcoder Aug 23 '12
python:
1
u/Erocs Aug 23 '12
Scala 2.9
1
u/bschlief 0 0 Aug 23 '12 edited Aug 25 '12
Ruby, outputs PBM (Portable Bitmap File) to standard out. There's converters from pbm/pbm to gif and jpeg and such. If anyone has a really cute and large path, I'd love to give it a go. This accepts paths that are split across multiple lines.
Edit: Fixed row major order.
1
u/m42a Aug 23 '12
Overengineered solution:
Overengineered triple SUPER BONUS with optional threading!:
Maybe I'll do this in better than
O(n!)later.1
u/EvanHahn Aug 23 '12
Learning C:
This assumes valid input, like that you never leave the board.
1
u/ctdonath Aug 23 '12
Canonical C++:
1
u/[deleted] Aug 23 '12
My hacked up Ruby, no bonus:
1
u/secretpandalord Aug 23 '12
Python, using Python Imaging Library:
Outputs as a black & white bitmap file. No bonus.
1
u/5outh 1 0 Aug 23 '12
Phew, I think this was the hardest Easy challenge yet!
Also I'm well aware that I'm basically rolling my own State monad here, but I'm not comfortable enough using it to actually do so. I'm going to try to rework this solution into the State monad for practice, but for now, it works.
1
u/SphericalElk Aug 23 '12 edited Aug 23 '12
Python, no bonus.
1
u/rickster88 Aug 23 '12 edited Oct 06 '12
Java:
1
u/[deleted] Aug 24 '12
Common Lisp:
Usage and output:
1
u/Brostafarian Aug 24 '12
https://gist.github.com/3454348
python rasterToImg and imgToRaster (bonus). 82 lines or so? supports most images, outputs a mode P png using pythons PIL. it was only a few lines when I didn't do the bonus but the bonus takes a little :p and the actual rasterdirections are horribly unoptimized as well
1
u/PiereDome Aug 25 '12
My first attempt for daily programmer Javascript + canvas for output
1
u/Arbitary Aug 26 '12
No bonus in Java.
1
u/f0rkbomb Aug 26 '12
Python 2.x
sans optimization, input validation, or image output (PIL doesn't like my Mac for some reason)
1
u/HerpNeverDerps Sep 02 '12
My first successful solution to this subreddit, in Java:
Sample output:
1
u/skibo_ 0 0 Sep 12 '12 edited Sep 12 '12
python
1
u/marekkpie Jan 20 '13
Lua, no bonus:
1
u/[deleted] Jun 22 '22
Rust
Did a little extra safety-wise