UNPKG

@logic-pad/core

Version:
49 lines (48 loc) 1.66 kB
import Z3Module from './z3Module.js'; import { instance as regionAreaInstance, } from '../../../rules/regionAreaRule.js'; export default class RegionAreaModule extends Z3Module { constructor() { super(...arguments); Object.defineProperty(this, "id", { enumerable: true, configurable: true, writable: true, value: regionAreaInstance.id }); } encode(grid, ctx) { const rules = grid.rules.filter(rule => rule.id === this.id); // optimizations if (rules.length === 0) { return; } const colorMap = new Map(); for (const rule of rules) { if (colorMap.has(rule.color)) { if (colorMap.get(rule.color) !== rule.size) { ctx.solver.add(ctx.ctx.Bool.val(false)); return; } } else { colorMap.set(rule.color, rule.size); } } for (const [color, count] of colorMap.entries()) { if (count < 0 || count > grid.getColorCount(color).max) { ctx.solver.add(ctx.ctx.Bool.val(false)); return; } } // encode for real const rc = ctx.regionConstrainer; for (const [color, count] of colorMap.entries()) { for (const [p, cell] of ctx.grid.grid.entries()) { ctx.solver.add(cell .eq(ctx.symbolSet.indices[color]) .implies(rc.regionSizeGrid.get(p).eq(count))); } } } } export const instance = new RegionAreaModule();