/* Suguru puzzle in Picat. From https://krazydad.com/suguru/ """ Suguru, also known as Tectonics or Number Blocks were invented in Japan by Naoki Inaba, a very prolific puzzle designer. ... Suguru puzzles are quite different than Sudoku, so you'll want to read these rules carefully: You'll see that the grid is subdivided into containers or cages, each of which is 1 to 5 cells in size. You need to fill each container with unique digits, counting up from 1. So for example a 2-square container contains the numbers 1 and 2. A 5-square container contains the numbers from 1 to 5. Adjacent (touching) cells may never contain the same number, and this includes diagonally adjacent cells. That's it! """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> puzzle(1,X,Cages), suguru(X,Cages), foreach(Row in X) println(Row) end, fail, nl. go => true. % % Solve a Suguru puzzle % suguru(X,Cages) => N = X.len, % decision variables X :: 1..max([len(C) : C in Cages]), % max length of the cages % constraints % % cages % foreach(C in Cages) all_different([X[I,J] : [I,J] in C]), min([X[I,J] : [I,J] in C]) #= 1, max([X[I,J] : [I,J] in C]) #= C.len end, % % all neighbours are distinct, including diagonals % foreach(I in 1..N, J in 1..N) foreach(A in -1..1, member(I+A, 1..N), B in -1..1, member(J+B,1..N), (A != 0 ; B != 0)) X[I,J] #!= X[I+A,J+B] end end, solve($[],X). % % The first example at https://krazydad.com/suguru/ % (5x5) % puzzle(1,Grid,Cages) => Grid = [[1,_,_,5,_], [_,_,_,_,_], [1,_,2,_,4], [_,_,_,_,_], [_,3,_,_,_] ], Cages = [ [[1,1],[1,2],[1,3],[2,1]], [[1,4],[1,5],[2,5],[3,5],[4,5]], [[2,2],[2,3],[3,1],[3,2],[4,1]], [[2,4],[3,3],[3,4],[4,2],[4,3]], [[4,4],[5,1],[5,2],[5,3],[5,4]], [[5,5]] ].