~jleightcap/nonolog

fb28cb4ed2d618c2bcf3a4a0cd2b5a550b245113 — jleightcap 5 months ago 3df83f1 main
README
2 files changed, 126 insertions(+), 4 deletions(-)

A README.md
M nono.prolog
A README.md => README.md +125 -0
@@ 0,0 1,125 @@
# nonolog

solve nonogram/picross puzzles, that are:
- square: same number of rows are columns (duh)
- monochromatic: only single colored, boolean states
- and nothing fancier

## usage, examples

start up a prolog repl and load definitions,

```bash
$ swipl
?- [nono].
true.

```

to solve the example puzzle

```
     11 3
     22112
    +-----+
  2 |     |
2 2 |     |
  1 |     |
  3 |     |
2 1 |     |
    +-----+
```

`picross/4` takes an array of row restrictions, array of column restrictions, the size, and corresponding matrix.

```prolog
?- picross([[2], [2, 2], [1], [3], [2, 1]], [[1, 2], [1, 2], [1], [3, 1], [2]], 5, X).
00011
11011
00010
11100
11010
X = [[0, 0, 0, 1, 1], [1, 1, 0, 1, 1], [0, 0, 0, 1, 0], [1, 1, 1, 0, 0], [1, 1, 0, 1|...]] .
```

this is the expected solution,

```
     11 3
     22112
    +-----+
  2 |   XX|
2 2 |XX XX|
  1 |   X |
  3 |XXX  |
2 1 |XX X |
    +-----+
```

with under-constrained inputs, this detects *all possible* solutions, so for a less constrained input:

```
   221
  +---+
1 |   |
2 |   |
2 |   |
  +---+
```

two solutions are found;

```prolog
?- picross([[1], [2], [2]], [[2], [2], [1]], 3, X).
100
110
011
X = [[1, 0, 0], [1, 1, 0], [0, 1, 1]] ;
001
110
110
X = [[0, 0, 1], [1, 1, 0], [1, 1, 0]] ;
```

corresponding to both valid outputs,

```
   221       221
  +---+     +---+
1 |X  |   1 |  X|
2 |XX |   2 |XX |
2 | XX|   2 |XX |
  +---+     +---+
```

and because this is constraint-based prolog, this works in the opposite direction.
if we wanted to generate the constraints to generate the puzzle

```
+---+
|XXX|
| X |
|X X|
+---+
```

```prolog
?- picross(X, Y, 3, [[1, 1, 1], [0, 1, 0], [1, 0, 1]]).
111
010
101
X = [[3], [1], [1, 1]],
Y = [[1, 1], [2], [1, 1]] ;
```

which are the constraints;

```
      1 1
      121
     +---+
   3 |XXX|
   1 | X |
 1 1 |X X|
     +---+
```

M nono.prolog => nono.prolog +1 -4
@@ 15,8 15,6 @@
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

% try this out: https://swish.swi-prolog.org/

:- use_module(library(clpfd)). % just used for transpose/2

% solve a given picross puzzle.


@@ 81,8 79,7 @@ decode(N, E, X) :-
% ?- add_encode(1, 1, [1, 2, 3], X).
% X = [3, 3].
add_encode(1, 1, [], X) :- X = [2].
add_encode(0, 0, S, X) :- X = S.
add_encode(0, 1, S, X) :- X = S.
add_encode(0, _, S, X) :- X = S.
add_encode(1, 0, S, X) :- X = [1|S].
add_encode(1, 1, S, X) :-
    X = [A|R],