Skip to content

Instantly share code, notes, and snippets.

@ctbo
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctbo/93b2333274324f6c8b65 to your computer and use it in GitHub Desktop.
Save ctbo/93b2333274324f6c8b65 to your computer and use it in GitHub Desktop.
Solver for c't Cover Puzzle at www.ct.de/600
// solver.js: Solve the puzzle at www.ct.de/600
// by Harald Bögeholz (c't) & Alexander Jost (info@alexanderjost.com)
(function () {
"use strict";
var solve600 = 1; // 0: solve for c't 1 / 1: solve for c't 600
var numCols = 0;
while($('#pos0-' + numCols + '-0').length > 0)
numCols++;
var numRows = 0;
while($('#pos0-0-' + numRows).length > 0)
numRows++;
var matrix = [];
for (var r1 = 0; r1 < numRows; ++r1)
for (var c1 = 0; c1 < numCols; ++c1) {
var v = [];
for (var r2 = 0; r2 < numRows; ++r2)
for (var c2 = 0; c2 < numCols; ++c2)
v.push(Math.abs(r1-r2)+Math.abs(c1-c2) <= 1 ? 1 : 0);
var cp = $('#pos0-' + c1 + '-' + r1);
v.push(solve600 ^ (cp.hasClass('state1') ? 1 : 0));
matrix.push(v);
}
for (var c=0; c < numCols*numRows; ++c) {
var r = c;
while (!matrix[r][c])
++r;
if (r != c) {
var tmp = matrix[c];
matrix[c] = matrix[r];
matrix[r] = tmp;
}
for (r=0; r < numCols*numRows; ++r)
if (r != c && matrix[r][c])
for (i=0; i < numCols*numRows+1; ++i)
matrix[r][i] ^= matrix[c][i];
}
var i=0;
for (var r=0; r < numRows; ++r)
for (var c=0; c < numCols; ++c)
if (matrix[i++][numRows*numCols])
$('#cell-' + c + '-' + r)[0].click();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment