ILP Part 88 — Magical Tangram

This is the eightieth eighth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve this task. We have a board with some letters on it. Letters are assigned numbers from zero to nine. Letters are connected with lines. Spaces inside regions have numbers which indicate what’s the biggest difference between two directly connected letters on the side of the region. Also, the biggest difference may appear only once on the side.

Solution:

First, we create variables for letters in line 1-4. Next, we define a helper function which calculates differences between connected letters (9-15), then it makes sure that all differences are not greater than the allowed maximum (19) and counts how many differences are equal to the maximum. Finally, it makes sure that exactly one difference is equal to the maximum (line 23).

Next, we create regions in lines 26-32. And we also make sure that all letters are different (line 34).

Output: