Solve the nonogram
fastest-codegame
Nonograms, also known as Hankie or Picross, are fascinating. They are really simple is essence, but to solve the most complexe one, some tricks have to be learned.
Basics
Nonograms are usually presented this way :
1
2 1 2
3 2 1 2 3
+-----------
3|
2 2|
1 1 1|
2 2|
3|
The numbers in lines and columns determine how much box in a row will be present, and how much sets there will be on this line/columns.
The seconde line says 2 2
which means "2 boxes in a row, at least one space, 2 boxes in a row"
Let's give it a try with the 5x5 sample above.
I will use #
for boxes and .
for blank confirmed.
1
2 1 2
3 2 1 2 3
+-----------
3|
2 2|
1 1 1|
2 2|
3|
As we said, second line says 2,some spaces,2.
As we are playing on a 5x5 board, there's only one space remaining after
putting the boxes, so their position is certain.
1
2 1 2
3 2 1 2 3
+-----------
3|
2 2| # # . # #
1 1 1|
2 2|
3|
There's some other 2 2 rows, let's fill them !
1
2 1 2
3 2 1 2 3
+-----------
3| # #
2 2| # # . # #
1 1 1| . .
2 2| # # . # #
3| # #
We can say this puzzle is over :
Look at the 1 1 1 rows, they have already 2 blank confirmed
which means there's only 3 spaces left. We can fill these, and complete the
puzzle.
1
2 1 2
3 2 1 2 3
+-----------
3| # # #
2 2| # # . # #
1 1 1| # . # . #
2 2| # # . # #
3| # # #
We didn't checked all confirmed blank, but it's not the matter, we only need
to check the boxes. Note that the 3 rows has been useless to solve this
puzzle.
There you got the basics, but some tips on the wikipedia page could be useful.
Goal
Your job is to write a program in the language you want to solve nonograms.
There shouldn't be any problem, but here's some loopholes that are forbidden, just in case :).
I'd also like you to write which down command have to be run to execute your code, and
Input
The input can be graphic, an array, a string via stdin, or whatever you want. You may hard-code it, I want to know how fast your program is to solve nonograms, not how fast it is to parse datas.
I'll provide two format for each test case. A ASCII-Art'd one, as shown above, and one structured as an array in the form :
[[columns],[lines]]
columns and line will also be noted the same way, the array for the sample :
[[[3],[2,2],[1,1,1],[2,2],[3]],[[3],[2,2],[1,1,1],[2,2],[3]]]
Output
You must produce the solved nonogram, formated as you want as long as it is clear. It should be outputed via stdout, or your language closest alternative, as an Image, Ascii-art or left on top of the stack.
Test Cases
Puzzles will never be greater than 99*99, nor smaller than 2*2.They all are solvable by using basic techniques, the ones shown on wikipedia are far more than enough.
I might add some test cases latter, but big ones take time, a lot of time. If you want to give it a try, post one with your answer, and it will be added if it is solvable without any guess. Which means solving only those one won't be necessary nice, you need to be able to solve any "basic" nonogram : No multi-row - multi depth contradiction looking. Even contradiction shouldn't be necessary
5x5
[[[3],[2,2],[1,1,1],[2,2],[3]],[[3],[2,2],[1,1,1],[2,2],[3]]]
1
2 1 2
3 2 1 2 3
+-----------
3|
2 2|
1 1 1|
2 2|
3|
10x10
[[[3],[2,2],[1,1,1],[2,2],[3],[3,1],[2,7],[1,1,1,1],[2,2,2],[3,3]],[[3],[2,2],[1,1,1],[3,2,2],[2,2,3],[1,1,1,1],[2,4,1],[3,1,2],[4],[1]]]
1
1 1 2
2 1 2 3 2 1 2 3
3 2 1 2 3 1 7 1 2 3
+--------------------
3|
2 2|
1 1 1|
3 2 2|
2 2 3|
1 1 1 1|
2 4 1|
3 1 2|
4|
1|
30x30
[[[10,6,10],[9,8,9],[7,10,7],[7,14,6],[6,15,5],[6,15,5],[5,17,4],[5,19,4],[26,3],[4,2,10,2,3],[4,1,8,1,3],[3,12,3],[3,9,3],[3,16,3],[3,16,3][3,16,3][3,16,3][3,16,3],[3,9,3],[3,12,3],[3,1,8,1,3],[4,2,10,2,3],[4,25],[4,19,4],[5,17,5],[6,16,5],[6,12,7],[8,10,8],[8,8,10],[10,6,10]]
4 4 3 4
2 1 1 2
10 9 7 7 6 6 5 5 10 8 3 3 3 3 3 3 3 3 3 810 4 5 6 6 8 810
6 810141515171926 2 112 91616161616 912 1 2 41917161210 8 6
10 9 7 6 5 5 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 325 4 5 5 7 81011
+------------------------------------------------------------
30|
30|
30|
11 9|
9 6|
6 3 1 1 3 5|
4 3 1 1 3 3|
2 3 2 2 4 3|
2 6 5 4 1|
1 6 5 5 1|
7 5 6|
9 7 8|
30|
30|
30|
30|
30|
30|
28|
26 1|
1 7 1 5 1 6 2|
2 6 1 3 1 4 2|
2 5 1 3 1 4 3|
3 3 1 1 1 3 4|
4 3 1 3 4|
6 3 3 6|
8 8|
30|
30|
30|
Solution
For those who are interested, here's the solution for the test cases.
5x5
1
2 1 2
3 2 1 2 3
+----------
3| . # # # .
2 2| # # . # #
1 1 1| # . # . #
2 2| # # . # #
3| . # # # .
10x10
1
1 1 2
2 1 2 3 2 1 2 3
3 2 1 2 3 1 7 1 2 3
+--------------------
3| . . . . . . # # # .
2 2| . . . . . # # . # #
1 1 1| . . . . . # . # . #
3 2 2| . # # # . # # . # #
2 2 3| # # . # # . # # # .
1 1 1 1| # . # . # . # . . .
2 4 1| # # . # # # # . . #
3 1 2| . # # # . # # . # #
4| . . . . . . # # # #
1| . . . . . . # . . .
30x30
Hope you'll like it, it took me a lot of time :)
4 4 3 4
2 1 1 2
10 9 7 7 6 6 5 5 10 8 3 3 3 3 3 3 3 3 3 810 4 5 6 6 8 810
6 810141515171926 2 112 91616161616 912 1 2 41917161210 8 6
10 9 7 6 5 5 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 325 4 5 5 7 81011
+------------------------------------------------------------
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
11 9| # # # # # # # # # # # . . . . . . . . . . # # # # # # # # #
9 6| # # # # # # # # # . . . . . . . . . . . . . . . # # # # # #
6 3 1 1 3 5| # # # # # # . . # # # . . # . . . # . . # # # . . # # # # #
4 3 1 1 3 3| # # # # . . . # # # . . . # . . . # . . . # # # . . . # # #
2 3 2 2 4 3| # # . . . . # # # . . . . # # . # # . . . . # # # # . # # #
2 6 5 4 1| # # . # # # # # # . . . . # # # # # . . . . # # # # . . . #
1 6 5 5 1| # . . # # # # # # . . . . # # # # # . . . . # # # # # . . #
7 5 6| . . # # # # # # # . . . . # # # # # . . . . # # # # # # . .
9 7 8| . # # # # # # # # # . . # # # # # # # . . # # # # # # # # .
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
28| . # # # # # # # # # # # # # # # # # # # # # # # # # # # # .
26 1| . . # # # # # # # # # # # # # # # # # # # # # # # # # # . #
1 7 1 5 1 6 2| # . . # # # # # # # . # . # # # # # . # . # # # # # # . # #
2 6 1 3 1 4 2| # # . # # # # # # . . # . . # # # . . # . . # # # # . . # #
2 5 1 3 1 4 3| # # . . # # # # # . . # . . # # # . . # . . # # # # . # # #
3 3 1 1 1 3 4| # # # . . . # # # . . # . . . # . . . # . . # # # . # # # #
4 3 1 3 4| # # # # . . . # # # . . . . . # . . . . . # # # . . # # # #
6 3 3 6| # # # # # # . . # # # . . . . . . . . . # # # . # # # # # #
8 8| # # # # # # # # . . . . . . . . . . . . . . # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
30| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
60x60
Not completed, not tested
60| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
6 3 1 1 335| # # # # # # . . # # # . . # . . . # . . # # # . . # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
6 3 1 1 3 6 6 5 5 1| # # # # # # . . # # # . . # . . . # . . # # # . . # # # # # # . . # # # # # # . . . . # # # # # . . . . # # # # # . . #
647| . # # # # # # . . . . . . # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
5 1 1 3 2 1 4| # # # # # . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . # . # # # . # # . # . . . . # # # #
1 2 1 2 2 1 4| . # . # # . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . # # . # # . # . . . . # # # #
1 2 1 2 1 3| # . . . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . # # . # . . . . . # # #
2 2 1 1 2| # # . . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . . # . . . . . . . . # #
3 2 2| # # # . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # #
3 2 1| # # # . . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
3 3 1| . # # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
1 8| # . # # # # # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 4 4 1| # . # # # # . # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
1 3 2 2| # . # # # . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # #
1 3 2 1| # . . # # # . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
2 4 3 1| # # . # # # # . . . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # .
4 3 3 2| # # # # . # # # . . . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # #
3 4 3 1| . # # # . . # # # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
1 3 8| # . # # # . # # # # # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 3 1 5 1| # . # # # . # . # # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #
1 5 4 2| # . # # # # # . # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # #
2 4 2 3 2| # # . # # # # . # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # .
2 2 2 1 3 2 1| # # . # # . # # . # . . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . #
1 2 2 1 3 2 2| . # . # # . # # . # . . . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # #
3 1 2 2 4 2 2| # # # . # . # # . # # . # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # .
3 1 2 4 3 2 2 1| # # # . # . # # . # # # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . #
1 2 2 3 2 3 2 2 2| # . # # . # # . # # # . # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . # #
1 2 2 3 2 3 2 2 2| # . # # . # # . # # # . # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . # # .
4 2 6 4 2 2 2 1| # # # # . # # . # # # # # # . # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . # # . #
6 6 4 2 2 2 2| . # # # # # # . # # # # # # . # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # . # # . # # . # #
3 3 2 3 5 3 2 2 2| # # # . # # # . # # . # # # . # # # # # . . . . . . . . . . . . . . . . . . . . . . . . . . . . # # # . # # . # # . # #
1 1 3 2 6 3 4 2 2 2| # . # . # # # . # # . # # # # # # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . . # # # # . # # . # # . # #
1 5 2 2 1 1 3 2 2 2 2 1| # . # # # # # . # # . # # . # . # . # # # . . . . . . . . . . . . . . . . . . . . . . . . . # # . . # # . # # . # # . #
1 3 1 2 2 1 1 4 3 2 2 2| # . # # # . # . # # . # # . # . # . # # # # . . . . . . . . . . . . . . . . . . . . . . . # # # . . . # # . # # . # # .
1 3 1 2 2 3 4 4 2 2 2| # . # # # . # . # # . # # . # # # . . # # # # . . . . . . . . . . . . . . . . . . . . . # # # # . . . . # # . # # . # #
1 1 1 1 2 2 3 4 2 2 2 2 1| # . # . # . # . # # . # # . # # # . . # # # # . . . . . . . . . . . . . . . . . . . . # # . . # # . . . . # # . # # . #
3 6 2 4 1 3 3 2 2 2| # # # . # # # # # # . # # . # # # # . # . # # # . . . . . . . . . . . . . . . . . . # # # . . . # # . . . . # # . # # .
2 2 3 2 4 1 4 4 2 2 2| . # # . # # . # # # . # # . # # # # . # . # # # # . . . . . . . . . . . . . . . . # # # # . . . . # # . . . . # # . # #
2 2 3 2 1 1 1 4 2 2 2 2 1| # # . . # # . # # # . # # . # . . # . # . # # # # . . . . . . . . . . . . . . . # # . . # # . . . . # # . . . . # # . #
2 6 2 1 2 1 2 3 3 2 2 2| # # . . # # # # # # . # # . # . # # . # . # # . # # # . . . . . . . . . . . . # # # . . . # # . . . . # # . . . . # # .
4 5 4 2 1 1 3 4 2 2 2| # # # # . # # # # # . # # # # . # # . # . # . . # # # . . . . . . . . . . . # # # # . . . . # # . . . . # # . . . . # #
3 5 4 2 1 1 2 2 2 2 2 1| . # # # . # # # # # . # # # # . # # . # . # . . # # . . . . . . . . . . . # # . . # # . . . . # # . . . . # # . . . . #
8 4 4 1 2 1 2 2 2 2 1| # # # # # # # # . # # # # . # # # # . # . # # . # . . . . . . . . . . . # # . . . . # # . . . . # # . . . . # # . . # .
3 4 1 2 4 1 3 2 2 2 2 2| # # # . # # # # . # . # # . # # # # . # . # # # . . . . . . . . . . . # # . . . . . . # # . . . . # # . . . . # # . # #
3 6 2 4 1 6 4 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # # # . . . . . # # # # . . . . . . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 2 2 2 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . # # . . . # # . # # . . . . . . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 5 1 1 3 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # # . # . . . # . # # # . . . . . . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 212 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # . # # # # # # # # # # # # . . . . . . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 2 1 1 3 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # . # . . . . # . . . . # # # . . . . . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 1 4 2 2 4| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # # # . . . . . . # # . . . . # # . . . . # # # #
3 6 2 4 1 4 1 2 2 2 2 4| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # . # # . . . . . # # . . . . # # . . . . # # # #
3 6 2 4 1 4 1 2 2 2 2 3| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # . . # # . . . . # # . . . . # # . . . . # # # .
3 6 2 4 1 4 1 2 2 2 2 4| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # . . . # # . . . # # . . . . # # . . . . # # # #
3 6 2 4 1 4 1 2 2 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # . . . # # . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 1 2 2 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . . . . # . . . . # # . . . # # . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 3 2 2 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . . . # # # . . . # # . . . # # . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 1 1 1 2 2 2 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . . # . # . # . . # # . . . # # . . . # # . . . . # # . . . . # # . #
3 6 2 4 1 4 2 1 2 2 2 2 1 2 2 1| # # # . # # # # # # . # # . # # # # . # . # # # # . # # . # . # # . # # . . . # # . . . # # . . # . # # . . . . # # . #
3 6 2 4 1 4 2 1 2 2 2 2 1 2 4| # # # . # # # # # # . # # . # # # # . # . # # # # . # # . # . # # . # # . . . # # . . . # # . . # . # # . . . # # # # .
3 6 2 4 1 4 2 1 6 4 412| # # # . # # # # # # . # # . # # # # . # . # # # # . # # . # . # # # # # # . # # # # . # # # # . # # # # # # # # # # # #
Winning criteria
Puzzle solving might be long, who will find the best heuristics? Who will find the best implementation? Fastest code will win (code will be running on my computer, i5-4440, and must not use more than 4GB of RAM).
You'll be scored using the 60*60 nonogram sandbox note : have to be added
. Other test cases are here to help you while developing your submission.
If there's a need of a Tie-breaker, i'll provide more complex test case (maybe a 90*90?)
Sandbox
I have two questions for the sandbox :
Do I need to put more explanations?
Is there too much grammar/others faults? (I'm not native, sorry :()
As suggested by @steveverrill, I changed it to a fastest-code contest, some basic things changed. It makes much more sense, thanks.
I added the first part of the 60x60 nonogram, still have to form the array, and do the columns. I know it is ugly, but I wasn't able to come with a nice one which would be viable AND long to solve. T