Skip to content

Instantly share code, notes, and snippets.

@ctbo ctbo/solver.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.