24
4
A nonogram is a two-dimensional logic puzzle that looks something like this (screenshots from the game Pixelo, my favourite nonogram game):

The goal of the game is to figure out what image those numbers are encoding. The rules are simple: A number on a column or row means that somewhere in that column or row, that many boxes are filled in in a row. For example, the bottom row in the above image must have no boxes filled in, while the one above it must have all of its boxes filled. The third row from the bottom has 8 filled boxes, and they will all be in a row.
Two or more numbers for the same column or row mean that there are multiple "runs" of filled boxes, with at least one space between, with those lengths. The order is preserved. For example, there are three filled in boxes on the very right column of the above image, at least one space below them, and then one more filled in box.
Here's that same puzzle, almost completed:

(The Xs aren't important, they're just a hint the player leaves for themself to say "This square is definitely not filled in". Think flags in Minesweeper. They have no rules meaning.)
Hopefully you can see that, for example, the middle columns with hints that say "2 2" have two 2-length runs of filled in boxes.
Your mission, should you choose to accept it, is to write a program or function that will create a puzzle like this. You are given the size of the board as a single integer (5 <= n <= 50) on stdin or as an argument (there's no reason why a nonogram puzzle has to be square, but for this challenge it will be). After that, you will be given a series of 1s and 0s representing filled and unfilled squares in the image, respectively. The first n of them are the top row, then the next row, etc. You will return or print to stdout a board of 2*1 cells (because they look better, and it gives you room for 2-digit hints for a column), all of them empty, with hints corresponding to the input data.
Output Format

Sample
Input:
./nonogram <<< '5 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1 1 1 0'
OR
n(5,[0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0])
Image:

Output:
1
2 1 2
3 2 1 2 3
+----------
3|
2 2|
1 1 1|
2 2|
3|
Input:
./nonogram <<< '15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1'
Image:

Output:
1 1
1 1 3 3 5 5 3 3 1
7 2 3 2 4 2 3 210 2 3 0 4 215
+------------------------------
2|
1|
1|
1|
1|
1 1|
3 3 1 1|
1 5 1 1|
3 5 3|
1 5 1|
1 3 1|
1 1 1 1 1|
1 1 1 1 1 1 1 1|
11 3|
11 3|
Clarifications
- Your output need not be a solvable puzzle. Not all nonograms are solvable, but that isn't your concern. Just output the hints that correspond to the input, whether they make for a good puzzle or not.
- A program that takes arguments on the command line is allowed. This is kind of stated above, but it's possible to get the wrong idea. That's what clarifications are for.
- Printing a
0for a row or column that has no filled in boxes is mandatory. I don't say this with words anywhere but it's in the sample data.
I'm just about done with my solution. Can we assume that there will not be a 3-digit number of boxes in a row or column? – afuous – 2014-06-03T15:43:46.817
2@voidpigeon:
5<=n<=50is the spec, so there cannot be any 3-digit numbers – Kyle Kanos – 2014-06-03T15:51:37.657After posting this question, I started working on a solution myself. I won't post it yet (as per this meta answer), but I'll post my byte count so you guys have something to strive for: 404 bytes in Python 2.7
– undergroundmonorail – 2014-06-03T16:33:40.440Doesn't your first example output contain one more
-than it should? – Ventero – 2014-06-03T16:37:06.203@Ventro You are correct! I knew how I would write a program to do this, but I didn't actually do it until just now, so my sample outputs were by hand. Oops! (I also screwed up the second sample output, but I fixed it before there were any answers.) – undergroundmonorail – 2014-06-03T16:38:32.680