A Suguru is a logic puzzle about a grid diagram split up in zone. A zone with n cells should contain each of the numbers 1 to n once. Two cells that touch (horizontally, vertically or diagonally) must never contain the same number.

Above, we show some steps in solving an example Tectonic. At the bottom of the image, we show how a tectonic is represented in prolog. Each zone in the grid is assigned an atom (a,b,c,d and e). The grid is represented as a matrix, each cell in it is represented as a pair zone-Var. zone is the atom representing the zone, Var is a variable that should be bound to the value of the cell.

Write a predicate solution(+Field,-Result) that unifies Result with a matrix of variables bound to the correct values of the cells (without zone markers).

Example:

?- Field=[
      [a-_,a-1,b-_,b-4],
      [c-_,b-_,b-2,b-1],
      [c-_,c-_,c-3,c-5],
      [d-_,d-_,d-2,d-_],
      [e-_,e-_,e-_,e-_]],
   solution(Field, OV),
   maplist(portray_clause, OV).
[2, 1, 3, 4].
[4, 5, 2, 1].
[2, 1, 3, 5].
[3, 4, 2, 1].
[2, 1, 3, 4].
Field = ...
OV = ...

Tip: To reduce the number of required constraints you could limit the constraints preventing touching identical number. Because the numbers in a zone must be distinct, we only need constraint betweens touching cells of different zones.

Tip: In the boilerplate, we predefine a only_vars/2 predicate that unifies its second argument with its first argument without field markers.