Constellations/js/Solver/SingleCell.js

43 lines
862 B
JavaScript

import * as Strategy from Solver.Strategy;
import {diagonal, plus} from Geometry.Vector;
return {
find: find
};
function find(zones, lines) {
var cellSets = zones.concat(lines.rows).concat(lines.columns);
return Strategy.map(
stepOfCell(cellSets),
Strategy.tryEach(cellSets.map(getSingleCellIn))
);
}
function getSingleCellIn(cellSet) {
return function() {
if(cellSet.size() == 1) {
return cellSet.getAll(function(v) {return v;})[0];
}
};
}
function stepOfCell(cellSets) {
return function(cell) {
return {
reason: 'singleCell',
empty: toEmpty(cell).intersection(cellSets),
star: CellSet.cells([cell])
};
};
}
function toEmpty(cell) {
var around = CellSet.rectangle(plus(cell, diagonal(-1)), diagonal(2));
around.remove(v);
return CellSet.union([
around,
CellSet.row(v.getRow()),
CellSet.column(v.getColumn())
]);
}