@altnovatrail/widgets 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cores/vector.schema.json +82 -3
- package/kinds/cells/cells.handles.json +145 -0
- package/kinds/cells/cells.schema.json +652 -4
- package/kinds/cells/vectors/bishop-grid-56-d1-1.json +105 -4
- package/kinds/cells/vectors/counter-cases.json +123 -0
- package/kinds/cells/vectors/counter-div-56-d1-3.json +57 -0
- package/kinds/cells/vectors/counter-div-56-d2-1.json +773 -0
- package/kinds/cells/vectors/counter-div-56-d2-3.json +65 -0
- package/kinds/cells/vectors/counter-div-56-d4-1.json +57 -0
- package/kinds/cells/vectors/counter-div-56-d4-2.json +106 -0
- package/kinds/cells/vectors/cut-cases.json +163 -0
- package/kinds/cells/vectors/cut-gaps-56-d2-1.json +142 -0
- package/kinds/cells/vectors/cut-grid-56-d2-4.json +45 -0
- package/kinds/cells/vectors/cut-grid-56-d4-1.json +154 -0
- package/kinds/cells/vectors/cut-grid-56-d4-2.json +136 -0
- package/kinds/cells/vectors/cut-invalid.json +48 -0
- package/kinds/cells/vectors/domino-grid-56-d3-2.json +238 -0
- package/kinds/cells/vectors/domino-limit.json +60 -0
- package/kinds/cells/vectors/domino-par-56-d3-2.json +194 -0
- package/kinds/cells/vectors/domino-par-56-d4-3.json +127 -0
- package/kinds/cells/vectors/figure-apart.json +47 -0
- package/kinds/cells/vectors/fold-12x12.json +79 -0
- package/kinds/cells/vectors/fold-14x14.json +79 -0
- package/kinds/cells/vectors/fold-26x26.json +79 -0
- package/kinds/cells/vectors/garden-enum-56-d3-4.json +96 -6
- package/kinds/cells/vectors/goat-grid-56-d4-4.json +325 -5
- package/kinds/cells/vectors/mosaic-grid-56-d2-2.json +84 -0
- package/kinds/cells/vectors/nodes-gaps-56-d2-2.json +44 -0
- package/kinds/cells/vectors/nodes-gaps-56-d4-2.json +70 -0
- package/kinds/cells/vectors/number-div-56-d2-2.json +49 -0
- package/kinds/cells/vectors/numbers-div-56-d3-1.json +24 -0
- package/kinds/cells/vectors/numbers-invalid.json +56 -0
- package/kinds/cells/vectors/paper-26x26.json +743 -0
- package/kinds/cells/vectors/paper-cube-12-d2-1.json +43 -19
- package/kinds/cells/vectors/paper-cube-12-d5-1.json +47 -4
- package/kinds/cells/vectors/paper-cube-56-d4-1.json +56 -5
- package/kinds/cells/vectors/paper-drawn-lines.json +149 -0
- package/kinds/cells/vectors/paper-grid-56-d1-2.json +46 -5
- package/kinds/cells/vectors/paper-turn-invalid.json +4 -4
- package/kinds/cells/vectors/path-6x6.json +119 -0
- package/kinds/cells/vectors/path-beside.json +108 -0
- package/kinds/cells/vectors/path-enum-12-d3-5.json +187 -0
- package/kinds/cells/vectors/path-enum-34-d4-3.json +412 -0
- package/kinds/cells/vectors/path-enum-56-d3-3.json +411 -0
- package/kinds/cells/vectors/path-enum-56-d3-5.json +351 -0
- package/kinds/cells/vectors/path-invalid.json +37 -0
- package/kinds/cells/vectors/path-limit.json +58 -0
- package/kinds/cells/vectors/path-maze.json +297 -0
- package/kinds/cells/vectors/path-unreachable.json +76 -0
- package/kinds/cells/vectors/perimeter-grid-56-d2-1.json +86 -0
- package/kinds/cells/vectors/perimeter-grid-56-d3-1.json +102 -0
- package/kinds/cells/vectors/perimeter-grid-56-d3-3.json +112 -0
- package/kinds/cells/vectors/perimeter-grid-56-d4-3.json +289 -0
- package/kinds/cells/vectors/polyomino-cases.json +191 -0
- package/kinds/cells/vectors/polyomino-grid-56-d3-4.json +205 -0
- package/kinds/cells/vectors/polyomino-grid-56-d4-1.json +137 -0
- package/kinds/cells/vectors/polyomino-grid-56-d5-3.json +157 -0
- package/kinds/cells/vectors/polyomino-invalid.json +45 -0
- package/kinds/cells/vectors/route-par-56-d3-3.json +245 -0
- package/kinds/cells/vectors/sides-gaps-56-d3-2.json +44 -0
- package/kinds/cells/vectors/sieve-div-56-d3-2.json +140 -0
- package/kinds/cells/vectors/tear-gaps-56-d3-1.json +84 -0
- package/kinds/cells/vectors/tear-limit.json +56 -0
- package/lib/cores/enumerate.d.ts +33 -0
- package/lib/cores/enumerate.js +77 -0
- package/lib/cores/kind.d.ts +55 -5
- package/lib/cores/search.d.ts +27 -8
- package/lib/cores/search.js +85 -28
- package/lib/kinds/cells/bishop.d.ts +1 -1
- package/lib/kinds/cells/bishop.js +1 -1
- package/lib/kinds/cells/counter.d.ts +26 -0
- package/lib/kinds/cells/counter.js +249 -0
- package/lib/kinds/cells/cut.d.ts +46 -0
- package/lib/kinds/cells/cut.js +400 -0
- package/lib/kinds/cells/figure.d.ts +13 -0
- package/lib/kinds/cells/figure.js +114 -0
- package/lib/kinds/cells/holes.d.ts +6 -0
- package/lib/kinds/cells/holes.js +57 -0
- package/lib/kinds/cells/kind.js +4 -2
- package/lib/kinds/cells/number.d.ts +6 -0
- package/lib/kinds/cells/number.js +32 -0
- package/lib/kinds/cells/operation.d.ts +12 -6
- package/lib/kinds/cells/paper.d.ts +10 -2
- package/lib/kinds/cells/paper.js +90 -8
- package/lib/kinds/cells/path.d.ts +38 -0
- package/lib/kinds/cells/path.js +288 -0
- package/lib/kinds/cells/piece.d.ts +10 -1
- package/lib/kinds/cells/piece.js +87 -3
- package/lib/kinds/cells/polyomino.d.ts +45 -0
- package/lib/kinds/cells/polyomino.js +450 -0
- package/lib/kinds/cells/registry.js +35 -4
- package/lib/kinds/cells/schema.js +41 -3
- package/lib/kinds/cells/sheet.d.ts +20 -1
- package/lib/kinds/cells/sheet.js +84 -6
- package/lib/kinds/cells/side.d.ts +33 -0
- package/lib/kinds/cells/side.js +86 -0
- package/lib/kinds/cells/sieve.d.ts +26 -0
- package/lib/kinds/cells/sieve.js +208 -0
- package/lib/kinds/cells/step.d.ts +1 -1
- package/lib/kinds/cells/step.js +1 -1
- package/package.json +5 -1
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// Cuts along the sides of cells, which part the sheet: a part is the cells joined across sides
|
|
2
|
+
// that no cut, no wall and no hole parts, so a wall bounds a part as a cut does, and a cut a
|
|
3
|
+
// statement draws is a wall of the scene's sheet. A cut runs as the scene has it: along one side;
|
|
4
|
+
// along the side's line through the part it lies in, from edge to edge of the part, as a sheet of
|
|
5
|
+
// stamps is torn; or along the side's whole line across the sheet, as a cake is cut. The state
|
|
6
|
+
// keeps the sides cut, so a goal reads the parts; and the goals that count the nodes and sides of
|
|
7
|
+
// the grid read the cuts as edges.
|
|
8
|
+
import { integer, object, pointer, string } from "../../cores/json.js";
|
|
9
|
+
import { wallKey } from "./sheet.js";
|
|
10
|
+
import { gridSides, isSheetSide, sideCells, sideEnds, sideName, } from "./side.js";
|
|
11
|
+
export const alongs = ["side", "part", "line"];
|
|
12
|
+
// Worked out once for each sheet, since every move and goal reads the sides: a sheet never
|
|
13
|
+
// changes once read.
|
|
14
|
+
const innerBySheet = new WeakMap();
|
|
15
|
+
function innerOf(sheet) {
|
|
16
|
+
let found = innerBySheet.get(sheet);
|
|
17
|
+
if (found === undefined) {
|
|
18
|
+
const sides = gridSides(sheet).filter((side) => side.k > 0 && side.k < (side.vertical ? sheet.cols : sheet.rows));
|
|
19
|
+
const cells = sides.map((side) => sideCells(sheet, side));
|
|
20
|
+
found = {
|
|
21
|
+
sides,
|
|
22
|
+
names: sides.map(sideName),
|
|
23
|
+
index: new Map(sides.map((side, i) => [sideName(side), i])),
|
|
24
|
+
cells,
|
|
25
|
+
open: cells.map(([a, b]) => !sheet.holes.has(a) && !sheet.holes.has(b) && !sheet.walls.has(wallKey(a, b))),
|
|
26
|
+
};
|
|
27
|
+
innerBySheet.set(sheet, found);
|
|
28
|
+
}
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
// The cuts of each state read so far, by the array of its member "cut", for each sheet: null for
|
|
32
|
+
// a member the rules never make.
|
|
33
|
+
const cutsBySheet = new WeakMap();
|
|
34
|
+
/**
|
|
35
|
+
* The sides a state has cut, by their index among the sheet's sides between cells; undefined where
|
|
36
|
+
* its member "cut" is no list of sides a cut may part, each once, in the sheet's order of sides —
|
|
37
|
+
* a state reported from outside the kind may hold one, and then no cut is played from it and no
|
|
38
|
+
* goal is met.
|
|
39
|
+
*/
|
|
40
|
+
export function cutsOf(sheet, state) {
|
|
41
|
+
const value = state.cut;
|
|
42
|
+
if (!Array.isArray(value)) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
let bySheet = cutsBySheet.get(sheet);
|
|
46
|
+
if (bySheet === undefined) {
|
|
47
|
+
bySheet = new WeakMap();
|
|
48
|
+
cutsBySheet.set(sheet, bySheet);
|
|
49
|
+
}
|
|
50
|
+
let cuts = bySheet.get(value);
|
|
51
|
+
if (cuts === undefined) {
|
|
52
|
+
const inner = innerOf(sheet);
|
|
53
|
+
const read = new Set();
|
|
54
|
+
let last = -1;
|
|
55
|
+
for (const name of value) {
|
|
56
|
+
const i = typeof name === "string" ? inner.index.get(name) : undefined;
|
|
57
|
+
if (i === undefined || i <= last || !inner.open[i]) {
|
|
58
|
+
read.clear();
|
|
59
|
+
last = Number.MAX_SAFE_INTEGER;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
read.add(i);
|
|
63
|
+
last = i;
|
|
64
|
+
}
|
|
65
|
+
cuts = last === Number.MAX_SAFE_INTEGER ? null : read;
|
|
66
|
+
bySheet.set(value, cuts);
|
|
67
|
+
}
|
|
68
|
+
return cuts ?? undefined;
|
|
69
|
+
}
|
|
70
|
+
// The parts of each set of cuts read so far: the moves from a state, its runs and its goal read
|
|
71
|
+
// the same set, which cutsOf gives once for the state, for one sheet.
|
|
72
|
+
const partsByCuts = new WeakMap();
|
|
73
|
+
/** The parts the cuts and the sheet's walls and holes make of the sheet. */
|
|
74
|
+
export function partsOf(sheet, cuts) {
|
|
75
|
+
const known = partsByCuts.get(cuts);
|
|
76
|
+
if (known !== undefined) {
|
|
77
|
+
return known;
|
|
78
|
+
}
|
|
79
|
+
const inner = innerOf(sheet);
|
|
80
|
+
const next = Array.from({ length: sheet.rows * sheet.cols }, () => []);
|
|
81
|
+
inner.cells.forEach(([a, b], i) => {
|
|
82
|
+
if (inner.open[i] && !cuts.has(i)) {
|
|
83
|
+
next[a].push(b);
|
|
84
|
+
next[b].push(a);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const of = new Array(sheet.rows * sheet.cols).fill(-1);
|
|
88
|
+
const sizes = [];
|
|
89
|
+
for (let first = 0; first < of.length; first++) {
|
|
90
|
+
if (of[first] !== -1 || sheet.holes.has(first))
|
|
91
|
+
continue;
|
|
92
|
+
const part = sizes.length;
|
|
93
|
+
of[first] = part;
|
|
94
|
+
let size = 0;
|
|
95
|
+
for (const queue = [first]; queue.length > 0;) {
|
|
96
|
+
const cell = queue.pop();
|
|
97
|
+
size++;
|
|
98
|
+
for (const other of next[cell]) {
|
|
99
|
+
if (of[other] === -1) {
|
|
100
|
+
of[other] = part;
|
|
101
|
+
queue.push(other);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
sizes.push(size);
|
|
106
|
+
}
|
|
107
|
+
const parts = { of, sizes };
|
|
108
|
+
partsByCuts.set(cuts, parts);
|
|
109
|
+
return parts;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The sides a cut from side i parts, as the scene runs it: the side alone; the sides along its
|
|
113
|
+
* line next to it, both ways, while both their cells lie in its part; or its whole line.
|
|
114
|
+
*/
|
|
115
|
+
function runOf(sheet, options, cuts, i) {
|
|
116
|
+
if (options.along === "side") {
|
|
117
|
+
return [i];
|
|
118
|
+
}
|
|
119
|
+
const inner = innerOf(sheet);
|
|
120
|
+
const side = inner.sides[i];
|
|
121
|
+
const length = side.vertical ? sheet.rows : sheet.cols;
|
|
122
|
+
// The sides of one line are next to each other in the sheet's order.
|
|
123
|
+
const first = i - side.at;
|
|
124
|
+
if (options.along === "line") {
|
|
125
|
+
return Array.from({ length }, (_, at) => first + at);
|
|
126
|
+
}
|
|
127
|
+
const { of } = partsOf(sheet, cuts);
|
|
128
|
+
const [a] = inner.cells[i];
|
|
129
|
+
const part = of[a];
|
|
130
|
+
const inPart = (at) => inner.cells[first + at].every((cell) => of[cell] === part);
|
|
131
|
+
let [from, to] = [side.at, side.at];
|
|
132
|
+
while (from > 0 && inPart(from - 1))
|
|
133
|
+
from--;
|
|
134
|
+
while (to < length - 1 && inPart(to + 1))
|
|
135
|
+
to++;
|
|
136
|
+
return Array.from({ length: to - from + 1 }, (_, at) => first + from + at);
|
|
137
|
+
}
|
|
138
|
+
function refuse(reason) {
|
|
139
|
+
return { ok: false, reason };
|
|
140
|
+
}
|
|
141
|
+
export const cut = {
|
|
142
|
+
name: "cut",
|
|
143
|
+
schema: {
|
|
144
|
+
options: {
|
|
145
|
+
description: "Cuts along the sides between cells, which part the sheet: a part is the cells joined across sides that no cut, no wall and no hole parts.",
|
|
146
|
+
type: "object",
|
|
147
|
+
properties: {
|
|
148
|
+
along: {
|
|
149
|
+
description: "How far a cut runs from the side a move names: side, that side alone; part, along its line through the part it lies in, from edge to edge of the part, as a sheet of stamps is torn; line, along its whole line across the sheet. side where left out.",
|
|
150
|
+
enum: [...alongs],
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
unevaluatedProperties: false,
|
|
154
|
+
},
|
|
155
|
+
move: {
|
|
156
|
+
description: "A cut from a side between two cells, run as the scene has it.",
|
|
157
|
+
type: "object",
|
|
158
|
+
properties: {
|
|
159
|
+
side: { description: "The side the cut starts from.", $ref: "#/$defs/side" },
|
|
160
|
+
},
|
|
161
|
+
required: ["side"],
|
|
162
|
+
unevaluatedProperties: false,
|
|
163
|
+
},
|
|
164
|
+
state: {
|
|
165
|
+
member: "cut",
|
|
166
|
+
schema: {
|
|
167
|
+
description: "The sides cut, each once: the vertical lines' from the left, each from the top, then the horizontal lines' from the top, each from the left.",
|
|
168
|
+
type: "array",
|
|
169
|
+
items: { description: "A side cut.", $ref: "#/$defs/side" },
|
|
170
|
+
uniqueItems: true,
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
read(value, _sheet, path, errors) {
|
|
175
|
+
const before = errors.length;
|
|
176
|
+
const o = object(value, path, errors, [], ["along"]);
|
|
177
|
+
const along = o?.along === undefined
|
|
178
|
+
? "side"
|
|
179
|
+
: string(o.along, pointer(path, "along"), errors, alongs, `one of ${alongs.join(", ")}`);
|
|
180
|
+
return errors.length > before || along === undefined ? undefined : { along };
|
|
181
|
+
},
|
|
182
|
+
start: () => [],
|
|
183
|
+
play(sheet, options, state, move) {
|
|
184
|
+
const errors = [];
|
|
185
|
+
const o = object(move, "", errors, ["side"]);
|
|
186
|
+
const name = o === undefined ? undefined : o.side;
|
|
187
|
+
const inner = innerOf(sheet);
|
|
188
|
+
const i = typeof name === "string" && errors.length === 0 ? inner.index.get(name) : undefined;
|
|
189
|
+
if (i === undefined) {
|
|
190
|
+
return refuse("not-between");
|
|
191
|
+
}
|
|
192
|
+
const cuts = cutsOf(sheet, state);
|
|
193
|
+
if (cuts === undefined) {
|
|
194
|
+
return refuse("no-cut");
|
|
195
|
+
}
|
|
196
|
+
const [a, b] = inner.cells[i];
|
|
197
|
+
if (sheet.holes.has(a) || sheet.holes.has(b)) {
|
|
198
|
+
return refuse("beside-hole");
|
|
199
|
+
}
|
|
200
|
+
if (sheet.walls.has(wallKey(a, b))) {
|
|
201
|
+
return refuse("wall");
|
|
202
|
+
}
|
|
203
|
+
if (cuts.has(i)) {
|
|
204
|
+
return refuse("cut");
|
|
205
|
+
}
|
|
206
|
+
const after = new Set(cuts);
|
|
207
|
+
for (const side of runOf(sheet, options, cuts, i)) {
|
|
208
|
+
if (inner.open[side])
|
|
209
|
+
after.add(side);
|
|
210
|
+
}
|
|
211
|
+
const cut = [...after].sort((x, y) => x - y).map((side) => inner.names[side]);
|
|
212
|
+
return { ok: true, state: { ...state, cut } };
|
|
213
|
+
},
|
|
214
|
+
moves(sheet, options, state) {
|
|
215
|
+
const cuts = cutsOf(sheet, state);
|
|
216
|
+
if (cuts === undefined) {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
const inner = innerOf(sheet);
|
|
220
|
+
// One move for each run of sides a cut parts: its first side the cut leaves whole.
|
|
221
|
+
const covered = new Set();
|
|
222
|
+
const moves = [];
|
|
223
|
+
inner.sides.forEach((_, i) => {
|
|
224
|
+
if (!inner.open[i] || cuts.has(i) || covered.has(i))
|
|
225
|
+
return;
|
|
226
|
+
for (const side of runOf(sheet, options, cuts, i))
|
|
227
|
+
covered.add(side);
|
|
228
|
+
moves.push({ side: inner.names[i] });
|
|
229
|
+
});
|
|
230
|
+
return moves;
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
/** The parts of the state, or undefined where it keeps cuts the rules never make. */
|
|
234
|
+
function stateParts(sheet, goal, state) {
|
|
235
|
+
const cuts = goal.cuts ? cutsOf(sheet, state) : new Set();
|
|
236
|
+
return cuts === undefined ? undefined : partsOf(sheet, cuts);
|
|
237
|
+
}
|
|
238
|
+
/** Adds an error where the scene allows no cut. */
|
|
239
|
+
function needsCut(operations, path, errors) {
|
|
240
|
+
if (!operations.has("cut")) {
|
|
241
|
+
errors.push({ path, message: "the scene allows no operation cut, so the sheet is never cut" });
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const empty = (description) => ({
|
|
245
|
+
description,
|
|
246
|
+
type: "object",
|
|
247
|
+
unevaluatedProperties: false,
|
|
248
|
+
});
|
|
249
|
+
/** The parts of the sheet; the key is the most there are. */
|
|
250
|
+
export const parts = {
|
|
251
|
+
name: "parts",
|
|
252
|
+
schema: empty("Met by every state; the key is the most parts the sheet is in, a part the cells joined across sides that no cut, no wall and no hole parts. On a sheet only to look at, its one state's parts."),
|
|
253
|
+
read(value, _sheet, path, errors, operations) {
|
|
254
|
+
return object(value, path, errors, []) === undefined
|
|
255
|
+
? undefined
|
|
256
|
+
: { cuts: operations.get("cut") !== undefined };
|
|
257
|
+
},
|
|
258
|
+
meets: (sheet, goal, state) => stateParts(sheet, goal, state) !== undefined,
|
|
259
|
+
asks: (sheet, goal) => ({
|
|
260
|
+
key: "max",
|
|
261
|
+
measure: (state) => stateParts(sheet, goal, state)?.sizes.length ?? 0,
|
|
262
|
+
}),
|
|
263
|
+
};
|
|
264
|
+
/** Every cell apart, a part of its own; the key is the fewest cuts. */
|
|
265
|
+
export const cellsApart = {
|
|
266
|
+
name: "cells_apart",
|
|
267
|
+
schema: empty("Every cell that is no hole is a part of its own; the key is the fewest cuts. The scene allows the operation cut."),
|
|
268
|
+
read(value, _sheet, path, errors, operations) {
|
|
269
|
+
const before = errors.length;
|
|
270
|
+
object(value, path, errors, []);
|
|
271
|
+
needsCut(operations, path, errors);
|
|
272
|
+
return errors.length > before ? undefined : { cuts: true };
|
|
273
|
+
},
|
|
274
|
+
meets(sheet, goal, state) {
|
|
275
|
+
const found = stateParts(sheet, goal, state);
|
|
276
|
+
return found?.sizes.every((size) => size === 1) === true;
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
/** The sheet cut into parts of as many cells each; the key counts the ways. */
|
|
280
|
+
export const cutWays = {
|
|
281
|
+
name: "cut_ways",
|
|
282
|
+
schema: {
|
|
283
|
+
description: "The sheet is cut into as many parts as named, each of as many cells, every side cut lying between two parts; the key counts the ways, two ways one where they part the same cells. The scene allows the operation cut alone, so that its states are the ways.",
|
|
284
|
+
type: "object",
|
|
285
|
+
properties: {
|
|
286
|
+
parts: {
|
|
287
|
+
description: "How many parts, at least 2; the cells that are no hole divide by it.",
|
|
288
|
+
type: "integer",
|
|
289
|
+
minimum: 2,
|
|
290
|
+
maximum: 676,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
required: ["parts"],
|
|
294
|
+
unevaluatedProperties: false,
|
|
295
|
+
},
|
|
296
|
+
read(value, sheet, path, errors, operations) {
|
|
297
|
+
const before = errors.length;
|
|
298
|
+
const o = object(value, path, errors, ["parts"]);
|
|
299
|
+
const count = o === undefined ? undefined : integer(o.parts, pointer(path, "parts"), errors, 2, 676);
|
|
300
|
+
const cells = sheet.rows * sheet.cols - sheet.holes.size;
|
|
301
|
+
if (count !== undefined && cells % count !== 0) {
|
|
302
|
+
errors.push({
|
|
303
|
+
path: pointer(path, "parts"),
|
|
304
|
+
message: `the sheet's ${cells} cells do not part into ${count} parts of as many cells`,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
needsCut(operations, path, errors);
|
|
308
|
+
const others = [...operations]
|
|
309
|
+
.filter(([named, options]) => named !== "cut" && options !== undefined)
|
|
310
|
+
.map(([named]) => named);
|
|
311
|
+
if (operations.get("cut") !== undefined && others.length > 0) {
|
|
312
|
+
errors.push({
|
|
313
|
+
path,
|
|
314
|
+
message: `the scene allows ${others.join(", ")} beside cut: a count of ways counts the cut's states alone`,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
return errors.length > before || count === undefined ? undefined : { parts: count };
|
|
318
|
+
},
|
|
319
|
+
meets(sheet, goal, state) {
|
|
320
|
+
const cuts = cutsOf(sheet, state);
|
|
321
|
+
if (cuts === undefined) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
const { of, sizes } = partsOf(sheet, cuts);
|
|
325
|
+
const cells = innerOf(sheet).cells;
|
|
326
|
+
return (sizes.length === goal.parts &&
|
|
327
|
+
sizes.every((size) => size === sizes[0]) &&
|
|
328
|
+
[...cuts].every((i) => {
|
|
329
|
+
const [a, b] = cells[i];
|
|
330
|
+
return of[a] !== of[b];
|
|
331
|
+
}));
|
|
332
|
+
},
|
|
333
|
+
asks: () => ({ key: "count" }),
|
|
334
|
+
};
|
|
335
|
+
/** The sides of cells, each once, that the count takes: every one, or the edges alone. */
|
|
336
|
+
function countedSides(sheet, goal, state) {
|
|
337
|
+
const cuts = goal.cuts ? cutsOf(sheet, state) : new Set();
|
|
338
|
+
if (cuts === undefined) {
|
|
339
|
+
return undefined;
|
|
340
|
+
}
|
|
341
|
+
const inner = innerOf(sheet);
|
|
342
|
+
return gridSides(sheet).filter((side) => {
|
|
343
|
+
if (!isSheetSide(sheet, side))
|
|
344
|
+
return false;
|
|
345
|
+
if (!goal.edge)
|
|
346
|
+
return true;
|
|
347
|
+
const i = inner.index.get(sideName(side));
|
|
348
|
+
return i === undefined || !inner.open[i] || cuts.has(i);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
function nodeKey(node) {
|
|
352
|
+
return `${node.v}:${node.h}`;
|
|
353
|
+
}
|
|
354
|
+
function countSchema(what) {
|
|
355
|
+
return {
|
|
356
|
+
description: `Met by every state; the key is the count of ${what} of the cells that are no hole, each once, the most there are. On a sheet only to look at, its one state's.`,
|
|
357
|
+
type: "object",
|
|
358
|
+
properties: {
|
|
359
|
+
edge: {
|
|
360
|
+
description: "Only those on an edge: the sheet's own, a hole's, a wall or a side cut. Every one where left out.",
|
|
361
|
+
type: "boolean",
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
unevaluatedProperties: false,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function readCount(value, _sheet, path, errors, operations) {
|
|
368
|
+
const o = object(value, path, errors, [], ["edge"]);
|
|
369
|
+
if (o === undefined) {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
if (o.edge !== undefined && typeof o.edge !== "boolean") {
|
|
373
|
+
errors.push({ path: pointer(path, "edge"), message: "true or false" });
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
return { cuts: operations.get("cut") !== undefined, edge: o.edge === true };
|
|
377
|
+
}
|
|
378
|
+
/** The nodes of the grid, every one or those on an edge; the key is the most. */
|
|
379
|
+
export const nodes = {
|
|
380
|
+
name: "nodes",
|
|
381
|
+
schema: countSchema("the nodes of the grid, the corners"),
|
|
382
|
+
read: readCount,
|
|
383
|
+
meets: (sheet, goal, state) => countedSides(sheet, goal, state) !== undefined,
|
|
384
|
+
asks: (sheet, goal) => ({
|
|
385
|
+
key: "max",
|
|
386
|
+
measure: (state) => new Set(countedSides(sheet, goal, state)?.flatMap((side) => sideEnds(side).map(nodeKey)))
|
|
387
|
+
.size,
|
|
388
|
+
}),
|
|
389
|
+
};
|
|
390
|
+
/** The sides of cells, every one or those on an edge; the key is the most. */
|
|
391
|
+
export const sides = {
|
|
392
|
+
name: "sides",
|
|
393
|
+
schema: countSchema("the sides"),
|
|
394
|
+
read: readCount,
|
|
395
|
+
meets: (sheet, goal, state) => countedSides(sheet, goal, state) !== undefined,
|
|
396
|
+
asks: (sheet, goal) => ({
|
|
397
|
+
key: "max",
|
|
398
|
+
measure: (state) => countedSides(sheet, goal, state)?.length ?? 0,
|
|
399
|
+
}),
|
|
400
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Goal } from "./operation.js";
|
|
2
|
+
import { type Color } from "./sheet.js";
|
|
3
|
+
export interface FigureOptions {
|
|
4
|
+
readonly color: Color;
|
|
5
|
+
/** How many cells the figure has, where the goal names it. */
|
|
6
|
+
readonly cells?: number;
|
|
7
|
+
/** How long a cell's side is, in the units the key counts. */
|
|
8
|
+
readonly side: number;
|
|
9
|
+
}
|
|
10
|
+
/** Each side of a cell of the figure that no other cell of it shares: on the sheet's edge or beside a cell outside. */
|
|
11
|
+
export declare const perimeter: Goal<FigureOptions>;
|
|
12
|
+
/** The figure's cells, each a square of the side. */
|
|
13
|
+
export declare const area: Goal<FigureOptions>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// The goals of a figure: the cells of a colour, joined by their sides into one figure, of as many
|
|
2
|
+
// cells as the goal names where it names a number; the key is the greatest perimeter or area of
|
|
3
|
+
// such a figure, a cell's side as long as the goal gives. A sheet only to look at has one state,
|
|
4
|
+
// so the key is its figure's measure; with paint, the greatest of a figure the learner can paint.
|
|
5
|
+
// A wall is no gap in a figure, and a hole, which never carries a colour, is outside it.
|
|
6
|
+
import { integer, object, pointer } from "../../cores/json.js";
|
|
7
|
+
import { readColor } from "./color.js";
|
|
8
|
+
import { colors, marksOf, maxSide } from "./sheet.js";
|
|
9
|
+
/** The longest side of a cell a goal names: a key stays far within the safe integers. */
|
|
10
|
+
const longestSide = 100;
|
|
11
|
+
/** The cells of the colour, by index, where they make one figure; else undefined. */
|
|
12
|
+
function figureOf(sheet, color, state) {
|
|
13
|
+
const marks = marksOf(sheet, state);
|
|
14
|
+
const cells = [...marks].filter(([, mark]) => mark.color === color).map(([cell]) => cell);
|
|
15
|
+
const first = cells[0];
|
|
16
|
+
if (first === undefined) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const inside = new Set(cells);
|
|
20
|
+
const joined = new Set([first]);
|
|
21
|
+
for (let layer = [first]; layer.length > 0;) {
|
|
22
|
+
const next = [];
|
|
23
|
+
for (const cell of layer) {
|
|
24
|
+
for (const side of sides(sheet, cell)) {
|
|
25
|
+
if (side !== undefined && inside.has(side) && !joined.has(side)) {
|
|
26
|
+
joined.add(side);
|
|
27
|
+
next.push(side);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
layer = next;
|
|
32
|
+
}
|
|
33
|
+
return joined.size === inside.size ? cells : undefined;
|
|
34
|
+
}
|
|
35
|
+
/** The cells across each of a cell's four sides, undefined where the side is the sheet's edge. */
|
|
36
|
+
function sides(sheet, cell) {
|
|
37
|
+
const [row, col] = [Math.floor(cell / sheet.cols), cell % sheet.cols];
|
|
38
|
+
return [
|
|
39
|
+
row > 0 ? cell - sheet.cols : undefined,
|
|
40
|
+
row < sheet.rows - 1 ? cell + sheet.cols : undefined,
|
|
41
|
+
col > 0 ? cell - 1 : undefined,
|
|
42
|
+
col < sheet.cols - 1 ? cell + 1 : undefined,
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
function schema(measure) {
|
|
46
|
+
return {
|
|
47
|
+
description: `The cells of the colour are joined by their sides into one figure, of as many cells as cells names where it names them; the key is the greatest ${measure} of such a figure.`,
|
|
48
|
+
type: "object",
|
|
49
|
+
properties: {
|
|
50
|
+
color: { description: "The colour of the figure's cells.", enum: [...colors] },
|
|
51
|
+
cells: {
|
|
52
|
+
description: "How many cells the figure has; any number where it is left out.",
|
|
53
|
+
type: "integer",
|
|
54
|
+
minimum: 1,
|
|
55
|
+
maximum: maxSide * maxSide,
|
|
56
|
+
},
|
|
57
|
+
side: {
|
|
58
|
+
description: "How long a cell's side is, in the units of the key; 1 where it is left out.",
|
|
59
|
+
type: "integer",
|
|
60
|
+
minimum: 1,
|
|
61
|
+
maximum: longestSide,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
required: ["color"],
|
|
65
|
+
unevaluatedProperties: false,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function read(value, _sheet, path, errors) {
|
|
69
|
+
const before = errors.length;
|
|
70
|
+
const o = object(value, path, errors, ["color"], ["cells", "side"]);
|
|
71
|
+
if (o === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const color = readColor({ color: o.color }, path, errors);
|
|
75
|
+
const cells = o.cells === undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: integer(o.cells, pointer(path, "cells"), errors, 1, maxSide * maxSide);
|
|
78
|
+
const side = o.side === undefined ? 1 : integer(o.side, pointer(path, "side"), errors, 1, longestSide);
|
|
79
|
+
if (errors.length > before || color === undefined || side === undefined) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
return cells === undefined ? { color, side } : { color, cells, side };
|
|
83
|
+
}
|
|
84
|
+
function meets(sheet, goal, state) {
|
|
85
|
+
const figure = figureOf(sheet, goal.color, state);
|
|
86
|
+
return figure !== undefined && (goal.cells === undefined || figure.length === goal.cells);
|
|
87
|
+
}
|
|
88
|
+
function figureGoal(name, measure, of) {
|
|
89
|
+
return {
|
|
90
|
+
name,
|
|
91
|
+
schema: schema(measure),
|
|
92
|
+
read,
|
|
93
|
+
meets,
|
|
94
|
+
asks: (sheet, goal) => ({
|
|
95
|
+
key: "max",
|
|
96
|
+
measure: (state) => of(sheet, figureOf(sheet, goal.color, state) ?? [], goal.side),
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** Each side of a cell of the figure that no other cell of it shares: on the sheet's edge or beside a cell outside. */
|
|
101
|
+
export const perimeter = figureGoal("perimeter", "perimeter", (sheet, figure, side) => {
|
|
102
|
+
const inside = new Set(figure);
|
|
103
|
+
let count = 0;
|
|
104
|
+
for (const cell of figure) {
|
|
105
|
+
for (const across of sides(sheet, cell)) {
|
|
106
|
+
if (across === undefined || !inside.has(across)) {
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return count * side;
|
|
112
|
+
});
|
|
113
|
+
/** The figure's cells, each a square of the side. */
|
|
114
|
+
export const area = figureGoal("area", "area", (_sheet, figure, side) => figure.length * side * side);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The goal of the punch: the paper is punched and opened, through a given cell where the scene
|
|
2
|
+
// names one, and the key is the greatest count of holes of an open sheet that meets it. Where the
|
|
3
|
+
// scene draws the lines of a statement's folds and names its cell, that is the statement's count,
|
|
4
|
+
// since its folds take every line drawn and lay the most layers on the cell.
|
|
5
|
+
import { isObject, object, pointer } from "../../cores/json.js";
|
|
6
|
+
import { cellAt, readCell } from "./sheet.js";
|
|
7
|
+
/**
|
|
8
|
+
* The cells punched, each once, where the state's paper is punched; undefined while it is folded or
|
|
9
|
+
* where it is no paper. A name that is no cell of the sheet, or a hole, is left out: a state
|
|
10
|
+
* reported from outside the kind may hold one, and no punch goes through it.
|
|
11
|
+
*/
|
|
12
|
+
function punchedOf(sheet, state) {
|
|
13
|
+
const paper = state.paper;
|
|
14
|
+
if (!isObject(paper) || !Array.isArray(paper.punched)) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
const cells = new Set();
|
|
18
|
+
for (const name of paper.punched) {
|
|
19
|
+
const cell = typeof name === "string" ? cellAt(sheet, name) : undefined;
|
|
20
|
+
if (cell !== undefined && !sheet.holes.has(cell)) {
|
|
21
|
+
cells.add(cell);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return cells;
|
|
25
|
+
}
|
|
26
|
+
export const paperHoles = {
|
|
27
|
+
name: "paper_holes",
|
|
28
|
+
schema: {
|
|
29
|
+
description: "The paper is punched and opened, through the cell where the goal names one; the key is the most holes the open sheet has. The scene allows the operation paper.",
|
|
30
|
+
type: "object",
|
|
31
|
+
properties: {
|
|
32
|
+
cell: { description: "A cell a hole is to go through, not a hole.", $ref: "#/$defs/cell" },
|
|
33
|
+
},
|
|
34
|
+
unevaluatedProperties: false,
|
|
35
|
+
},
|
|
36
|
+
read(value, sheet, path, errors, operations) {
|
|
37
|
+
const before = errors.length;
|
|
38
|
+
const o = object(value, path, errors, [], ["cell"]);
|
|
39
|
+
const at = pointer(path, "cell");
|
|
40
|
+
const cell = o?.cell === undefined ? undefined : readCell(sheet, o.cell, at, errors);
|
|
41
|
+
if (cell !== undefined && sheet.holes.has(cell)) {
|
|
42
|
+
errors.push({ path: at, message: "a hole: no punch goes through it" });
|
|
43
|
+
}
|
|
44
|
+
if (!operations.has("paper")) {
|
|
45
|
+
errors.push({ path, message: "the scene allows no operation paper, so nothing is punched" });
|
|
46
|
+
}
|
|
47
|
+
return errors.length > before ? undefined : cell === undefined ? {} : { cell };
|
|
48
|
+
},
|
|
49
|
+
meets(sheet, goal, state) {
|
|
50
|
+
const punched = punchedOf(sheet, state);
|
|
51
|
+
return punched !== undefined && (goal.cell === undefined || punched.has(goal.cell));
|
|
52
|
+
},
|
|
53
|
+
asks: (sheet) => ({
|
|
54
|
+
key: "max",
|
|
55
|
+
measure: (state) => punchedOf(sheet, state)?.size ?? 0,
|
|
56
|
+
}),
|
|
57
|
+
};
|
package/lib/kinds/cells/kind.js
CHANGED
|
@@ -66,7 +66,7 @@ export function cellsKind(registry, limit) {
|
|
|
66
66
|
// map, which ones the scene meant is unknown: the goal reads them as all the kind's, and the
|
|
67
67
|
// map's own error stands alone.
|
|
68
68
|
const allowed = map(o.operations, "/operations", errors);
|
|
69
|
-
const allows = new
|
|
69
|
+
const allows = new Map(allowed === undefined ? [...operations.keys()].map((named) => [named, undefined]) : []);
|
|
70
70
|
for (const [named, value] of Object.entries(allowed ?? {})) {
|
|
71
71
|
const at = pointer("/operations", named);
|
|
72
72
|
const op = operations.get(named);
|
|
@@ -74,11 +74,12 @@ export function cellsKind(registry, limit) {
|
|
|
74
74
|
errors.push({ path: at, message: "no operation of the kind has this name" });
|
|
75
75
|
continue;
|
|
76
76
|
}
|
|
77
|
-
allows.
|
|
77
|
+
allows.set(named, undefined);
|
|
78
78
|
if (sheet !== undefined) {
|
|
79
79
|
const read = part(op.read.bind(op), value, sheet, at, errors);
|
|
80
80
|
if (read !== undefined) {
|
|
81
81
|
chosen.set(named, read.options);
|
|
82
|
+
allows.set(named, read.options);
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
}
|
|
@@ -146,5 +147,6 @@ export function cellsKind(registry, limit) {
|
|
|
146
147
|
},
|
|
147
148
|
hasGoal: (scene) => scene.goal !== null,
|
|
148
149
|
meets: (scene, state) => scene.goal?.goal.meets(scene.sheet, scene.goal.options, state) ?? false,
|
|
150
|
+
asks: (scene) => scene.goal?.goal.asks?.(scene.sheet, scene.goal.options) ?? { key: "fewest" },
|
|
149
151
|
};
|
|
150
152
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The goal of a number read off a table of numbers: the cell a statement finds by its row and its
|
|
2
|
+
// column, and the key the number it holds. Every state meets it, so on a table only to look at the
|
|
3
|
+
// key is read off the one state, as a statement's answer is read off the table.
|
|
4
|
+
import { object, pointer } from "../../cores/json.js";
|
|
5
|
+
import { readCell, readTable } from "./sheet.js";
|
|
6
|
+
export const numberAt = {
|
|
7
|
+
name: "number_at",
|
|
8
|
+
schema: {
|
|
9
|
+
description: "The number a cell of a table of numbers holds, found by its row and its column; every state meets it, and the key is the number.",
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
cell: { description: "The cell, not a hole.", $ref: "#/$defs/cell" },
|
|
13
|
+
},
|
|
14
|
+
required: ["cell"],
|
|
15
|
+
unevaluatedProperties: false,
|
|
16
|
+
},
|
|
17
|
+
read(value, sheet, path, errors) {
|
|
18
|
+
const before = errors.length;
|
|
19
|
+
const o = object(value, path, errors, ["cell"]);
|
|
20
|
+
const first = readTable(sheet, path, errors, "no cell holds one to read");
|
|
21
|
+
const at = pointer(path, "cell");
|
|
22
|
+
const cell = o === undefined ? undefined : readCell(sheet, o.cell, at, errors);
|
|
23
|
+
if (cell !== undefined && sheet.holes.has(cell)) {
|
|
24
|
+
errors.push({ path: at, message: "a hole: its number is cut out" });
|
|
25
|
+
}
|
|
26
|
+
return errors.length > before || first === undefined || cell === undefined
|
|
27
|
+
? undefined
|
|
28
|
+
: { number: first + cell };
|
|
29
|
+
},
|
|
30
|
+
meets: () => true,
|
|
31
|
+
asks: (_sheet, goal) => ({ key: "max", measure: () => goal.number }),
|
|
32
|
+
};
|