Problem
The witch puzzle has been introduced in model witches. In this section, we show how to generate new witch puzzles.
Modeling Steps
In fact, all we need to do is to generate a correct data table a_{d,k}. The idea is simple: Start with a “solution”, that is, start with a table that already is a solution by placing the blocks 1, 2,… in position (1, 1), (1, 2), … and all blocks with rotation 1. Then permute randomly the columns of table a_{d,k}, and finally shift each column by a random number. This is explained below.
We need the data for a 4 × 4 puzzle, that is, the sets are modified correspondingly:

We introduce a set of columns/rows: i,j ∈{1,…, 4}.

The number of rotations is unchanged as: d ∈{1,…, 4}.

The number of blocks is now 16 (= 4 ⋅ 4), or more generally, it is the cardinality of rows times the cardinalities of columns (in LPL: #i*#j ).

The first step in generating a correct a_{d,k} is to randomly fill it with digits in the interval [1, 8] (we still want 4 bitmaps, we could also consider puzzles with five bitmaps). The code for this is:
integer parameter a{d,k}:=Trunc(Rnd(1,8.99));

Next, we make sure that the horizontal half bitmaps correspond, that is, the assignment a_{4,k+1} = 9 – a_{2,k} for all kk mod 4 ⁄= 0 modifies some elements of a_{d,k}. Note that k mod 4 ⁄= 0 always corresponds to the last row j of the square. In LPL code this gives:
{kk%#i<>0} (a[4,k+1]:=9a[2,k]);

We must also assure that the vertical blocks match, hence: a_{1,k+4} = 9 – a_{3,k} for all kk ≤ 16 – 4. Note that block k + 4 is the block just below block k. The LPL code for this is:
{kk<=#k#i}(a[1,k+#i]:=9a[3,k]);
At this point, we got a correct data table a_{d,k} with at least one solution. In the next steps, we “scramble” the table in such a way that it is still a correct one.

Exchanging two rows in table a_{d,k} correspond to interchanging the positions of two blocks within the square. Hence, this is an invariant with respect to a solution. In the code we do this by exchanging several times two randomly chosen rows:
{k} (n := Trunc(Rnd(1,#k+1)),
m := Trunc(Rnd(1,#k+1)),
{d} (temp:=a[d,n], a[d,n]:=a[d,m], a[d,m]:=temp)); 
“Shifting a column” means to move all elements downwards (modulo 4) by a given positive number. This operation corresponds to a rotation of the corresponding block and this is also an invariant with respect to a solution. Hence, the final step is to shift each column by a randomly chosen amount 1, 2, 3, or 4:
{k} (n:=Trunc(Rnd(1,5)), m:=a[1,k],
if(n=1,({dd>1} (a[d1,k]:=a[d,k]),a[4,k]:=m),
n=2,(a[1,k]:=a[3,k],a[3,k]:=m, m:=a[2,k], a[2,k]:=a[4,k], a[4,k]:=m),
n=3,(a[1,k]:=a[4,k],a[4,k]:=a[3,k],a[3,k]:=a[2,k],a[2,k]:=m)));Note that a shift by 4 does not move any element, therefore it can be ignored.
At this point we have a correct and scrambled table. Note that solving a 4 × 4 puzzle is more involving but still possible.
References
[1] MatMod. Homepage for Learning Mathematical Modeling : https://matmod.ch.