+ int i;
+ for (i = 0; i < len; i++)
+ row1[i] ^= row2[i];
+}
+
+static char *solve_game(game_state *state, game_state *currstate,
+ char *aux, char **error)
+{
+ int w = state->w, h = state->h, wh = w * h;
+ unsigned char *equations, *solution, *shortest;
+ int *und, nund;
+ int rowsdone, colsdone;
+ int i, j, k, len, bestlen;
+ char *ret;
+
+ /*
+ * Set up a list of simultaneous equations. Each one is of
+ * length (wh+1) and has wh coefficients followed by a value.
+ */
+ equations = snewn((wh + 1) * wh, unsigned char);
+ for (i = 0; i < wh; i++) {
+ for (j = 0; j < wh; j++)
+ equations[i * (wh+1) + j] = currstate->matrix->matrix[j*wh+i];
+ equations[i * (wh+1) + wh] = currstate->grid[i] & 1;
+ }
+
+ /*
+ * Perform Gaussian elimination over GF(2).
+ */
+ rowsdone = colsdone = 0;
+ nund = 0;
+ und = snewn(wh, int);
+ do {
+ /*
+ * Find the leftmost column which has a 1 in it somewhere
+ * outside the first `rowsdone' rows.
+ */
+ j = -1;
+ for (i = colsdone; i < wh; i++) {
+ for (j = rowsdone; j < wh; j++)
+ if (equations[j * (wh+1) + i])
+ break;
+ if (j < wh)
+ break; /* found one */
+ /*
+ * This is a column which will not have an equation
+ * controlling it. Mark it as undetermined.
+ */
+ und[nund++] = i;
+ }
+
+ /*
+ * If there wasn't one, then we've finished: all remaining
+ * equations are of the form 0 = constant. Check to see if
+ * any of them wants 0 to be equal to 1; this is the
+ * condition which indicates an insoluble problem
+ * (therefore _hopefully_ one typed in by a user!).
+ */
+ if (i == wh) {
+ for (j = rowsdone; j < wh; j++)
+ if (equations[j * (wh+1) + wh]) {
+ *error = "No solution exists for this position";
+ sfree(equations);
+ sfree(und);
+ return NULL;
+ }
+ break;
+ }
+
+ /*
+ * We've found a 1. It's in column i, and the topmost 1 in
+ * that column is in row j. Do a row-XOR to move it up to
+ * the topmost row if it isn't already there.
+ */
+ assert(j != -1);
+ if (j > rowsdone)
+ rowxor(equations + rowsdone*(wh+1), equations + j*(wh+1), wh+1);
+
+ /*
+ * Do row-XORs to eliminate that 1 from all rows below the
+ * topmost row.
+ */
+ for (j = rowsdone + 1; j < wh; j++)
+ if (equations[j*(wh+1) + i])
+ rowxor(equations + j*(wh+1),
+ equations + rowsdone*(wh+1), wh+1);
+
+ /*
+ * Mark this row and column as done.
+ */
+ rowsdone++;
+ colsdone = i+1;
+
+ /*
+ * If we've done all the rows, terminate.
+ */
+ } while (rowsdone < wh);
+
+ /*
+ * If we reach here, we have the ability to produce a solution.
+ * So we go through _all_ possible solutions (each
+ * corresponding to a set of arbitrary choices of those
+ * components not directly determined by an equation), and pick
+ * one requiring the smallest number of flips.
+ */
+ solution = snewn(wh, unsigned char);
+ shortest = snewn(wh, unsigned char);
+ memset(solution, 0, wh);
+ bestlen = wh + 1;
+ while (1) {
+ /*
+ * Find a solution based on the current values of the
+ * undetermined variables.
+ */
+ for (j = rowsdone; j-- ;) {
+ int v;
+
+ /*
+ * Find the leftmost set bit in this equation.
+ */
+ for (i = 0; i < wh; i++)
+ if (equations[j * (wh+1) + i])
+ break;
+ assert(i < wh); /* there must have been one! */
+
+ /*
+ * Compute this variable using the rest.
+ */
+ v = equations[j * (wh+1) + wh];
+ for (k = i+1; k < wh; k++)
+ if (equations[j * (wh+1) + k])
+ v ^= solution[k];
+
+ solution[i] = v;
+ }
+
+ /*
+ * Compare this solution to the current best one, and
+ * replace the best one if this one is shorter.
+ */
+ len = 0;
+ for (i = 0; i < wh; i++)
+ if (solution[i])
+ len++;
+ if (len < bestlen) {
+ bestlen = len;
+ memcpy(shortest, solution, wh);
+ }
+
+ /*
+ * Now increment the binary number given by the
+ * undetermined variables: turn all 1s into 0s until we see
+ * a 0, at which point we turn it into a 1.
+ */
+ for (i = 0; i < nund; i++) {
+ solution[und[i]] = !solution[und[i]];
+ if (solution[und[i]])
+ break;
+ }
+
+ /*
+ * If we didn't find a 0 at any point, we have wrapped
+ * round and are back at the start, i.e. we have enumerated
+ * all solutions.
+ */
+ if (i == nund)
+ break;
+ }
+
+ /*
+ * We have a solution. Produce a move string encoding the
+ * solution.
+ */
+ ret = snewn(wh + 2, char);
+ ret[0] = 'S';
+ for (i = 0; i < wh; i++)
+ ret[i+1] = shortest[i] ? '1' : '0';
+ ret[wh+1] = '\0';
+
+ sfree(shortest);
+ sfree(solution);
+ sfree(equations);
+ sfree(und);
+
+ return ret;
+}
+
+static int game_can_format_as_text_now(game_params *params)
+{
+ return TRUE;