70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
import id from UnitJS.Fun;
|
|
import size from Config;
|
|
import {asAFunction, iter, map, set, square} from Grid.Util;
|
|
import {diagonal, zero} from Geometry.Vector;
|
|
import * as CellSet from Geometry.CellSet;
|
|
|
|
return {
|
|
fork: fork,
|
|
setCells: setCells,
|
|
start: start,
|
|
zoneNames: zoneNames
|
|
};
|
|
|
|
function start(coloring) {
|
|
var empty = Array.from({length: size});
|
|
return {
|
|
constellation: square(size),
|
|
getColor: asAFunction(coloring),
|
|
missing: CellSet.rectangle(zero(), diagonal(size)),
|
|
zones: {
|
|
colors: getColors(coloring),
|
|
rows: empty.map(function(_, i) {return CellSet.row(i);}),
|
|
columns: empty.map(function(_, i) {return CellSet.column(i);})
|
|
}
|
|
};
|
|
}
|
|
|
|
function getColors(grid) {
|
|
var colors = Array.from({length: size});
|
|
iter(grid, function(color, cell) {
|
|
if(colors[color] == undefined) {
|
|
colors[color] = new CellSet.CellSet();
|
|
}
|
|
colors[color].add(cell);
|
|
});
|
|
return colors;
|
|
}
|
|
|
|
function fork(state) {
|
|
var zones = {};
|
|
for(var name in state.zones) {
|
|
zones[name] = state.zones[name].map(function(s) {return s.copy();});
|
|
}
|
|
return {
|
|
constellation: map(state.constellation, id),
|
|
getColor: state.getColor,
|
|
missing: state.missing.copy(),
|
|
zones: zones
|
|
};
|
|
}
|
|
|
|
function setCells(solvingState, value, cellSet) {
|
|
var forgetList = allZones(solvingState).concat(solvingState.missing);
|
|
cellSet.iter(function(cell) {
|
|
set(solvingState.constellation, cell, value);
|
|
forgetList.forEach(function(solverSet) {
|
|
solverSet.remove(cell);
|
|
});
|
|
});
|
|
}
|
|
|
|
function zoneNames(solvingState) {
|
|
return Object.keys(solvingState.zones);
|
|
}
|
|
|
|
function allZones(solvingState) {
|
|
return zoneNames(solvingState).flatMap(function(name) {
|
|
return solvingState.zones[name];
|
|
});
|
|
}
|