A Nonogram is a Japanese picture puzzle in which a hidden picture can be found. This image can be formed by colouring the boxes of a rectangular lattice black or white, taking into account the set of integers that is specified for each row and each column of the grid. These numbers indicate how many consecutive black squares there should be in that row or column. For example, if the grid indicates the series 4 8 3 , this means that the row or column is made up of sets of four, eight, and three consecutive black boxes, in that order, and that there is at least one white box between each of the consecutive series.

nonogram

Assignment

For this task, you must solve a simplified version of the Nonogram puzzle. As with the original puzzle, the sequences of black boxes in each row are provided. The description for the columns, however, is no longer needed. For each set of consecutive black boxes is now defined by a couple of integers $$(s, l)$$, where $$s$$ and $$l$$ indicate the start position and the length of the series, respectively. The far-left cell of a row is at position zero.

vereenvoudigd nonogram

Note that the order in which the sequences of consecutive black boxes are given no longer plays a role. A row of a Nonogram puzzle can thus be defined by a container (a list, tuple, collection, …) of tuples. Each of these tuples consist of two integers that indicate the start position and the length of the series, respectively, of the consecutive black boxes. Asked:

Example

In the following example session we assume that the file stupid.puzzle.txt is in the current directory. Click on the name of the solution file to see the solution that was generated for the puzzle.

>>> width([(2, 12)])
14
>>> width(((1, 3), (7, 2), (12, 3)))
15
>>> width({(10, 5), (1, 2), (5, 3)})
15

>>> line([(2, 12)])
'  ############'
>>> line(((1, 3), (7, 2), (12, 3)), 20)
' ###   ##   ###     '
>>> line({(10, 5), (1, 2), (5, 3)})
' ##  ###  #####'

>>> nonogram('stupid.puzzle.txt', 'stupid.solution.txt')