shithub: puzzles

Download patch

ref: 0bd1a8057841386754f9f4a8a268616c7ce80e80
parent: 91735e5019be84d2fa693c5d40746c818ace28f8
author: Ben Harris <bjh21@bjh21.me.uk>
date: Sat Apr 1 14:54:29 EDT 2023

Magnets: add a check that magnets don't wrap between lines

There was nothing in Magnet's description validation to prevent there
being the left end of a magnet at the right end of a row and the right
end of a magnet at the left end of the row below.  Indeed as far as I
can such a game (e.g. 3x3:..2,2..,...,1.1,TLRB*LRLR) plays entirely
correctly except that one magnet is discontinuous.

While this worked, it was entirely an artefact of the particular memory
layout that Magnets uses and shouldn't have been allowed, so I've added
an additional validation rule to stop it.

--- a/magnets.c
+++ b/magnets.c
@@ -521,6 +521,8 @@
     for (idx = 0; idx < state->wh; idx++) {
         if (state->common->dominoes[idx] < 0 ||
             state->common->dominoes[idx] >= state->wh ||
+            (state->common->dominoes[idx] % state->w != idx % state->w &&
+             state->common->dominoes[idx] / state->w != idx / state->w) ||
             state->common->dominoes[state->common->dominoes[idx]] != idx) {
             *prob = "Domino descriptions inconsistent";
             goto done;