Nonogram solver

Revision as of 13:04, 21 November 2013 by rosettacode>Bearophile (First draft of the task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Each row and column of a rectangular grid is annotated with the lengths of its distinct runs of occupied cells. Using only these lengths you should find one valid configuration of empty and occupied cells (or show a failure message):

Nonogram solver is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
  Problem:                    Solution:

  . . . . . . . .  3          . # # # . . . .  3           
  . . . . . . . .  2 1        # # . # . . . .  2 1         
  . . . . . . . .  3 2        . # # # . . # #  3 2         
  . . . . . . . .  2 2        . . # # . . # #  2 2         
  . . . . . . . .  6          . . # # # # # #  6           
  . . . . . . . .  1 5        # . # # # # # .  1 5         
  . . . . . . . .  6          # # # # # # . .  6           
  . . . . . . . .  1          . . . . # . . .  1           
  . . . . . . . .  2          . . . # # . . .  2           
  1 3 1 7 5 3 4 3             1 3 1 7 5 3 4 3              
  2 1 5 1                     2 1 5 1

The problem above could be represented by two lists of lists:

X = [[3], [2,1], [3,2], [2,2], [6], [1,5], [6], [1], [2]]
Y = [[1,2], [3,1], [1,5], [7,1], [5], [3], [4], [3]]

A more compact representation of the same problem uses strings, where the letters represent the numbers, A=1, B=2, etc:

x = "C BA CB BB F AE F A B"
y = "AB CA AE GA E C D C"

For this task try to solve the problems read from a "nonogram_problems.txt" file, copied from this:

C BA CB BB F AE F A B
AB CA AE GA E C D C

F CAC ACAC CN AAA AABB EBB EAA ECCC HCCC
D D AE CD AE A DA BBB CC AAB BAA AAB DA AAB AAA BAB AAA CD BBA DA

CA BDA ACC BD CCAC CBBAC BBBBB BAABAA ABAD AABB BBH BBBD ABBAAA CCEA AACAAB BCACC ACBH DCH ADBE ADBB DBE ECE DAA DB CC
BC CAC CBAB BDD CDBDE BEBDF ADCDFA DCCFB DBCFC ABDBA BBF AAF BADB DBF AAAAD BDG CEF CBDB BBB FC

E BCB BEA BH BEK AABAF ABAC BAA BFB OD JH BADCF Q Q R AN AAN EI H G
E CB BAB AAA AAA AC BB ACC ACCA AGB AIA AJ AJ ACE AH BAF CAG DAG FAH FJ GJ ADK ABK BL CM


This task is the problem n.98 of the "99 Prolog Problems" by Werner Hett (also thanks to Paul Singleton for the idea and the examples):
https://sites.google.com/site/prologsite/prolog-problems

Some Haskell solutions:
http://www.haskell.org/haskellwiki/99_questions/Solutions/98

Bonus Problem: generate nonograms with unique solutions, of desired height and width.