@logic-pad/core
Version:
38 lines (37 loc) • 1.56 kB
JavaScript
import Z3Module from './z3Module.js';
import { instance as viewpointInstance, } from '../../../symbols/viewpointSymbol.js';
import { Point, reduceCells } from 'grilops';
import { DIRECTIONS } from '../../../primitives.js';
import { move } from '../../../dataHelper.js';
import { convertDirection } from '../utils.js';
export default class ViewpointModule extends Z3Module {
constructor() {
super(...arguments);
Object.defineProperty(this, "id", {
enumerable: true,
configurable: true,
writable: true,
value: viewpointInstance.id
});
}
encode(grid, ctx) {
const symbols = grid.symbols.get(this.id);
// optimizations
if (!symbols || symbols.length === 0) {
return;
}
// encode for real
for (const symbol of symbols) {
const x = Math.floor(symbol.x);
const y = Math.floor(symbol.y);
const origin = ctx.grid.cellAt(new Point(y, x));
const sumTerms = [];
DIRECTIONS.forEach(direction => {
const startPos = move({ x, y }, direction);
sumTerms.push(reduceCells(ctx.grid.ctx, ctx.grid, new Point(startPos.y, startPos.x), convertDirection(direction), ctx.ctx.Int.val(0), (acc, cell) => ctx.ctx.If(cell.eq(origin), acc.add(1), acc), (_, cell) => cell.neq(origin)));
});
ctx.solver.add(ctx.ctx.Sum(ctx.ctx.Int.val(1), ...sumTerms).eq(symbol.number));
}
}
}
export const instance = new ViewpointModule();