@altnovatrail/widgets 0.1.2 → 0.2.0
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 +148 -3
- package/kinds/cells/cells.schema.json +716 -8
- 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/knight-after-no-parity.json +31 -0
- package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
- 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 +184 -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/polyomino-one-pieces.json +28 -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 +70 -5
- package/lib/cores/kind.js +2 -2
- package/lib/cores/search.d.ts +47 -10
- package/lib/cores/search.js +129 -37
- 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/index.d.ts +8 -0
- package/lib/kinds/cells/index.js +10 -2
- package/lib/kinds/cells/kind.d.ts +3 -2
- package/lib/kinds/cells/kind.js +23 -8
- package/lib/kinds/cells/knight.d.ts +1 -1
- package/lib/kinds/cells/number.d.ts +6 -0
- package/lib/kinds/cells/number.js +32 -0
- package/lib/kinds/cells/operation.d.ts +27 -7
- package/lib/kinds/cells/paper.d.ts +10 -2
- package/lib/kinds/cells/paper.js +109 -13
- package/lib/kinds/cells/path.d.ts +38 -0
- package/lib/kinds/cells/path.js +288 -0
- package/lib/kinds/cells/piece.d.ts +27 -7
- package/lib/kinds/cells/piece.js +202 -37
- package/lib/kinds/cells/polyomino.d.ts +45 -0
- package/lib/kinds/cells/polyomino.js +461 -0
- package/lib/kinds/cells/registry.js +35 -4
- package/lib/kinds/cells/schema.js +45 -4
- 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
|
@@ -4,6 +4,10 @@ export declare const colors: readonly ["red", "blue", "green", "yellow"];
|
|
|
4
4
|
export type Color = (typeof colors)[number];
|
|
5
5
|
/** The most rows, as there are letters, and the most columns. */
|
|
6
6
|
export declare const maxSide = 26;
|
|
7
|
+
/** The greatest first number of a table of numbers: its numbers stay short enough to read in a cell. */
|
|
8
|
+
export declare const maxFirst = 9999;
|
|
9
|
+
/** The greatest number a table of numbers holds, in its last cell. */
|
|
10
|
+
export declare const maxNumber: number;
|
|
7
11
|
/** The whole form of a cell's name, as the schema has it. */
|
|
8
12
|
export declare const cellPattern: RegExp;
|
|
9
13
|
/** What a cell carries: a colour, a whole number written in it, or both. */
|
|
@@ -20,6 +24,15 @@ export interface Sheet {
|
|
|
20
24
|
readonly walls: ReadonlySet<string>;
|
|
21
25
|
/** The marks the scene gives its cells. */
|
|
22
26
|
readonly marks: ReadonlyMap<number, Mark>;
|
|
27
|
+
/** The marks the scene gives nodes of the grid, by their names; none where it gives none. */
|
|
28
|
+
readonly nodeMarks?: ReadonlyMap<string, Mark>;
|
|
29
|
+
/** The marks the scene gives sides of cells, by their names; none where it gives none. */
|
|
30
|
+
readonly sideMarks?: ReadonlyMap<string, Mark>;
|
|
31
|
+
/**
|
|
32
|
+
* The number of the first cell, A1, where the sheet is a table of numbers: each cell holds the
|
|
33
|
+
* number after the one before it in reading order, a hole too, though nothing lies on it.
|
|
34
|
+
*/
|
|
35
|
+
readonly first?: number;
|
|
23
36
|
}
|
|
24
37
|
/** A state of the kind: the cells' marks under "marks", and a member for each operation that keeps one. */
|
|
25
38
|
export type State = {
|
|
@@ -29,6 +42,11 @@ export type State = {
|
|
|
29
42
|
export declare function cellName(sheet: Sheet, index: number): string;
|
|
30
43
|
/** The index of a cell by its name, or undefined where the name is malformed or off the sheet. */
|
|
31
44
|
export declare function cellAt(sheet: Sheet, name: string): number | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The first number of a table of numbers, where the sheet is one; else undefined, with an error at
|
|
47
|
+
* path whose words end in what: why what is read there needs the numbers.
|
|
48
|
+
*/
|
|
49
|
+
export declare function readTable(sheet: Sheet, path: string, errors: SceneError[], what: string): number | undefined;
|
|
32
50
|
/** Whether two cells share a side. */
|
|
33
51
|
export declare function sideBySide(sheet: Sheet, a: number, b: number): boolean;
|
|
34
52
|
/** The key of the wall between two cells, the same whichever comes first. */
|
|
@@ -41,7 +59,8 @@ export declare function readCell(sheet: Sheet, value: unknown, path: string, err
|
|
|
41
59
|
export declare function readMark(value: unknown, path: string, errors: SceneError[]): Mark | undefined;
|
|
42
60
|
/**
|
|
43
61
|
* The sheet of a scene: its size, its holes — never every cell —, its walls between cells side by
|
|
44
|
-
* side and off the holes, and the marks of its cells, none on a hole
|
|
62
|
+
* side and off the holes, and the marks of its cells, none on a hole, and of its nodes and sides,
|
|
63
|
+
* none beside holes alone; no cell named twice; and the first number, where it is a table of numbers.
|
|
45
64
|
*/
|
|
46
65
|
export declare function readSheet(value: unknown, path: string, errors: SceneError[]): Sheet | undefined;
|
|
47
66
|
/** The marks as a state holds them: a member for each marked cell, by its name. */
|
package/lib/kinds/cells/sheet.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
// The sheet of the kind cells: rows and columns of cells, cells cut out, walls between cells, and
|
|
2
|
-
// the marks a cell carries — a colour, a number. A cell is named as
|
|
3
|
-
// it: its row by a letter from the top, A to Z, and its column by a number from the left, 1 to 26,
|
|
4
|
-
// so "B3" is the third cell of the second row. Inside, a cell is its index in reading order.
|
|
2
|
+
// the marks a cell, a node or a side of the grid carries — a colour, a number. A cell is named as
|
|
3
|
+
// the tasks of the course name it: its row by a letter from the top, A to Z, and its column by a number from the left, 1 to 26,
|
|
4
|
+
// so "B3" is the third cell of the second row. Inside, a cell is its index in reading order. A sheet
|
|
5
|
+
// may be a table of numbers: its cells numbered in reading order from a first number, so that a row
|
|
6
|
+
// of the table is a row of the sheet and the table's width the sheet's columns.
|
|
5
7
|
import { array, integer, isObject, map, object, pointer, string } from "../../cores/json.js";
|
|
8
|
+
import { isSheetNode, isSheetSide, nodeAt, nodePattern, sideAt, sidePattern } from "./side.js";
|
|
6
9
|
/** The colours a cell may carry, by name: the drawing gives each its tone. */
|
|
7
10
|
export const colors = ["red", "blue", "green", "yellow"];
|
|
8
11
|
/** The most rows, as there are letters, and the most columns. */
|
|
9
12
|
export const maxSide = 26;
|
|
13
|
+
/** The greatest first number of a table of numbers: its numbers stay short enough to read in a cell. */
|
|
14
|
+
export const maxFirst = 9_999;
|
|
15
|
+
/** The greatest number a table of numbers holds, in its last cell. */
|
|
16
|
+
export const maxNumber = maxFirst + maxSide * maxSide - 1;
|
|
10
17
|
/** The whole form of a cell's name, as the schema has it. */
|
|
11
18
|
export const cellPattern = /^[A-Z]([1-9]|1[0-9]|2[0-6])$/;
|
|
12
19
|
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
@@ -36,6 +43,16 @@ export function cellAt(sheet, name) {
|
|
|
36
43
|
// The sheet's own names are the canonical ones: a name the pattern refuses, as "A01", is none.
|
|
37
44
|
return namesOf(sheet).index.get(name);
|
|
38
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The first number of a table of numbers, where the sheet is one; else undefined, with an error at
|
|
48
|
+
* path whose words end in what: why what is read there needs the numbers.
|
|
49
|
+
*/
|
|
50
|
+
export function readTable(sheet, path, errors, what) {
|
|
51
|
+
if (sheet.first === undefined) {
|
|
52
|
+
errors.push({ path, message: `the sheet gives no numbers: ${what}` });
|
|
53
|
+
}
|
|
54
|
+
return sheet.first;
|
|
55
|
+
}
|
|
39
56
|
/** Whether two cells share a side. */
|
|
40
57
|
export function sideBySide(sheet, a, b) {
|
|
41
58
|
const [ra, ca] = [Math.floor(a / sheet.cols), a % sheet.cols];
|
|
@@ -89,11 +106,12 @@ export function readMark(value, path, errors) {
|
|
|
89
106
|
}
|
|
90
107
|
/**
|
|
91
108
|
* The sheet of a scene: its size, its holes — never every cell —, its walls between cells side by
|
|
92
|
-
* side and off the holes, and the marks of its cells, none on a hole
|
|
109
|
+
* side and off the holes, and the marks of its cells, none on a hole, and of its nodes and sides,
|
|
110
|
+
* none beside holes alone; no cell named twice; and the first number, where it is a table of numbers.
|
|
93
111
|
*/
|
|
94
112
|
export function readSheet(value, path, errors) {
|
|
95
113
|
const before = errors.length;
|
|
96
|
-
const o = object(value, path, errors, ["rows", "cols"], ["holes", "walls", "marks"]);
|
|
114
|
+
const o = object(value, path, errors, ["rows", "cols"], ["holes", "walls", "marks", "node_marks", "side_marks", "numbers"]);
|
|
97
115
|
if (o === undefined) {
|
|
98
116
|
return undefined;
|
|
99
117
|
}
|
|
@@ -157,7 +175,67 @@ export function readSheet(value, path, errors) {
|
|
|
157
175
|
marks.set(cell, mark);
|
|
158
176
|
}
|
|
159
177
|
}
|
|
160
|
-
|
|
178
|
+
const placed = { ...bare, holes };
|
|
179
|
+
const nodeMarks = readPlaceMarks(o.node_marks, pointer(path, "node_marks"), errors, {
|
|
180
|
+
pattern: nodePattern,
|
|
181
|
+
what: "a node's name, as v1:h1",
|
|
182
|
+
on: (name) => {
|
|
183
|
+
const node = nodeAt(placed, name);
|
|
184
|
+
return node !== undefined && isSheetNode(placed, node);
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
const sideMarks = readPlaceMarks(o.side_marks, pointer(path, "side_marks"), errors, {
|
|
188
|
+
pattern: sidePattern,
|
|
189
|
+
what: "a side's name, as v1:A or h1:1",
|
|
190
|
+
on: (name) => {
|
|
191
|
+
const side = sideAt(placed, name);
|
|
192
|
+
return side !== undefined && isSheetSide(placed, side);
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
let first;
|
|
196
|
+
if (o.numbers !== undefined) {
|
|
197
|
+
const numbersPath = pointer(path, "numbers");
|
|
198
|
+
const numbers = object(o.numbers, numbersPath, errors, ["first"]);
|
|
199
|
+
first =
|
|
200
|
+
numbers === undefined
|
|
201
|
+
? undefined
|
|
202
|
+
: integer(numbers.first, pointer(numbersPath, "first"), errors, 0, maxFirst);
|
|
203
|
+
}
|
|
204
|
+
if (errors.length > before) {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
rows,
|
|
209
|
+
cols,
|
|
210
|
+
holes,
|
|
211
|
+
walls,
|
|
212
|
+
marks,
|
|
213
|
+
...(nodeMarks.size > 0 ? { nodeMarks } : {}),
|
|
214
|
+
...(sideMarks.size > 0 ? { sideMarks } : {}),
|
|
215
|
+
...(first === undefined ? {} : { first }),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The marks of nodes or of sides, by the place's name: each name matches the pattern, which
|
|
220
|
+
* admits one name for each place, and names a place on the sheet with a cell beside it that is no
|
|
221
|
+
* hole.
|
|
222
|
+
*/
|
|
223
|
+
function readPlaceMarks(value, path, errors, places) {
|
|
224
|
+
const marks = new Map();
|
|
225
|
+
const members = value === undefined ? {} : (map(value, path, errors) ?? {});
|
|
226
|
+
for (const [name, item] of Object.entries(members)) {
|
|
227
|
+
const at = pointer(path, name);
|
|
228
|
+
const mark = readMark(item, at, errors);
|
|
229
|
+
if (string(name, at, errors, places.pattern, places.what) === undefined)
|
|
230
|
+
continue;
|
|
231
|
+
if (!places.on(name)) {
|
|
232
|
+
errors.push({ path: at, message: "off the sheet, or beside holes alone: a mark on nothing" });
|
|
233
|
+
}
|
|
234
|
+
else if (mark !== undefined) {
|
|
235
|
+
marks.set(name, mark);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return marks;
|
|
161
239
|
}
|
|
162
240
|
/** The marks as a state holds them: a member for each marked cell, by its name. */
|
|
163
241
|
export function marksJson(sheet, marks) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Sheet } from "./sheet.js";
|
|
2
|
+
/** The whole form of a side's name, as the schema has it. */
|
|
3
|
+
export declare const sidePattern: RegExp;
|
|
4
|
+
/** The whole form of a node's name, as the schema has it. */
|
|
5
|
+
export declare const nodePattern: RegExp;
|
|
6
|
+
/** A side of a cell: on a vertical line k, beside the row at; on a horizontal line k, beside the column at. */
|
|
7
|
+
export interface Side {
|
|
8
|
+
readonly vertical: boolean;
|
|
9
|
+
/** The line, counted from 0, the left or top edge. */
|
|
10
|
+
readonly k: number;
|
|
11
|
+
/** The row of a side on a vertical line, the column of one on a horizontal line, from 0. */
|
|
12
|
+
readonly at: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function sideName(side: Side): string;
|
|
15
|
+
/** A side by its name, or undefined where the name is malformed or off the sheet. */
|
|
16
|
+
export declare function sideAt(sheet: Sheet, name: string): Side | undefined;
|
|
17
|
+
/** The cells on either side of a side, left or top first, each undefined beyond the sheet's edge. */
|
|
18
|
+
export declare function sideCells(sheet: Sheet, side: Side): readonly [number | undefined, number | undefined];
|
|
19
|
+
/** Whether a side is a side of a cell of the sheet: no hole lies on both sides of it, nor beyond the edge. */
|
|
20
|
+
export declare function isSheetSide(sheet: Sheet, side: Side): boolean;
|
|
21
|
+
/** A node, by its vertical line and its horizontal one, counted from 0. */
|
|
22
|
+
export interface GridNode {
|
|
23
|
+
readonly v: number;
|
|
24
|
+
readonly h: number;
|
|
25
|
+
}
|
|
26
|
+
/** A node by its name, or undefined where the name is malformed or off the sheet. */
|
|
27
|
+
export declare function nodeAt(sheet: Sheet, name: string): GridNode | undefined;
|
|
28
|
+
/** Whether a node is a corner of a cell of the sheet that is no hole. */
|
|
29
|
+
export declare function isSheetNode(sheet: Sheet, node: GridNode): boolean;
|
|
30
|
+
/** Every side of the grid, a cell's or not: the vertical lines from the left, each from the top, then the horizontal ones from the top, each from the left. */
|
|
31
|
+
export declare function gridSides(sheet: Sheet): readonly Side[];
|
|
32
|
+
/** The two nodes at the ends of a side. */
|
|
33
|
+
export declare function sideEnds(side: Side): readonly [GridNode, GridNode];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
2
|
+
/** The whole form of a side's name, as the schema has it. */
|
|
3
|
+
export const sidePattern = /^(v(0|[1-9]|1[0-9]|2[0-6]):[A-Z]|h(0|[1-9]|1[0-9]|2[0-6]):([1-9]|1[0-9]|2[0-6]))$/;
|
|
4
|
+
/** The whole form of a node's name, as the schema has it. */
|
|
5
|
+
export const nodePattern = /^v(0|[1-9]|1[0-9]|2[0-6]):h(0|[1-9]|1[0-9]|2[0-6])$/;
|
|
6
|
+
export function sideName(side) {
|
|
7
|
+
return side.vertical ? `v${side.k}:${letters[side.at]}` : `h${side.k}:${side.at + 1}`;
|
|
8
|
+
}
|
|
9
|
+
/** A side by its name, or undefined where the name is malformed or off the sheet. */
|
|
10
|
+
export function sideAt(sheet, name) {
|
|
11
|
+
if (name.search(sidePattern) === -1) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const [line, place] = name.split(":");
|
|
15
|
+
const vertical = line[0] === "v";
|
|
16
|
+
const k = Number(line.slice(1));
|
|
17
|
+
const at = vertical ? letters.indexOf(place) : Number(place) - 1;
|
|
18
|
+
const [lines, across] = vertical ? [sheet.cols, sheet.rows] : [sheet.rows, sheet.cols];
|
|
19
|
+
return k <= lines && at < across ? { vertical, k, at } : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** The cells on either side of a side, left or top first, each undefined beyond the sheet's edge. */
|
|
22
|
+
export function sideCells(sheet, side) {
|
|
23
|
+
if (side.vertical) {
|
|
24
|
+
const row = side.at * sheet.cols;
|
|
25
|
+
return [
|
|
26
|
+
side.k > 0 ? row + side.k - 1 : undefined,
|
|
27
|
+
side.k < sheet.cols ? row + side.k : undefined,
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
return [
|
|
31
|
+
side.k > 0 ? (side.k - 1) * sheet.cols + side.at : undefined,
|
|
32
|
+
side.k < sheet.rows ? side.k * sheet.cols + side.at : undefined,
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/** Whether a side is a side of a cell of the sheet: no hole lies on both sides of it, nor beyond the edge. */
|
|
36
|
+
export function isSheetSide(sheet, side) {
|
|
37
|
+
return sideCells(sheet, side).some((cell) => cell !== undefined && !sheet.holes.has(cell));
|
|
38
|
+
}
|
|
39
|
+
/** A node by its name, or undefined where the name is malformed or off the sheet. */
|
|
40
|
+
export function nodeAt(sheet, name) {
|
|
41
|
+
if (name.search(nodePattern) === -1) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const [v, h] = name.split(":").map((part) => Number(part.slice(1)));
|
|
45
|
+
return v <= sheet.cols && h <= sheet.rows ? { v, h } : undefined;
|
|
46
|
+
}
|
|
47
|
+
/** Whether a node is a corner of a cell of the sheet that is no hole. */
|
|
48
|
+
export function isSheetNode(sheet, node) {
|
|
49
|
+
for (const row of [node.h - 1, node.h]) {
|
|
50
|
+
for (const col of [node.v - 1, node.v]) {
|
|
51
|
+
if (row >= 0 &&
|
|
52
|
+
row < sheet.rows &&
|
|
53
|
+
col >= 0 &&
|
|
54
|
+
col < sheet.cols &&
|
|
55
|
+
!sheet.holes.has(row * sheet.cols + col)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
/** Every side of the grid, a cell's or not: the vertical lines from the left, each from the top, then the horizontal ones from the top, each from the left. */
|
|
63
|
+
export function gridSides(sheet) {
|
|
64
|
+
const sides = [];
|
|
65
|
+
for (let k = 0; k <= sheet.cols; k++) {
|
|
66
|
+
for (let at = 0; at < sheet.rows; at++)
|
|
67
|
+
sides.push({ vertical: true, k, at });
|
|
68
|
+
}
|
|
69
|
+
for (let k = 0; k <= sheet.rows; k++) {
|
|
70
|
+
for (let at = 0; at < sheet.cols; at++)
|
|
71
|
+
sides.push({ vertical: false, k, at });
|
|
72
|
+
}
|
|
73
|
+
return sides;
|
|
74
|
+
}
|
|
75
|
+
/** The two nodes at the ends of a side. */
|
|
76
|
+
export function sideEnds(side) {
|
|
77
|
+
return side.vertical
|
|
78
|
+
? [
|
|
79
|
+
{ v: side.k, h: side.at },
|
|
80
|
+
{ v: side.k, h: side.at + 1 },
|
|
81
|
+
]
|
|
82
|
+
: [
|
|
83
|
+
{ v: side.at, h: side.k },
|
|
84
|
+
{ v: side.at + 1, h: side.k },
|
|
85
|
+
];
|
|
86
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Goal, Operation } from "./operation.js";
|
|
2
|
+
import { type State } from "./sheet.js";
|
|
3
|
+
export interface SieveOptions {
|
|
4
|
+
/** The widths the numbers may be counted out by, in the scene's order. */
|
|
5
|
+
readonly by: readonly number[];
|
|
6
|
+
/** The remainder a number left keeps, less than every width. */
|
|
7
|
+
readonly left: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The widths the state has counted out by, ascending, or undefined where its member is no such
|
|
11
|
+
* list of the scene's widths: a state reported from outside the kind may hold one.
|
|
12
|
+
*/
|
|
13
|
+
export declare function countedOf(options: SieveOptions, state: State): readonly number[] | undefined;
|
|
14
|
+
/** Whether a number is left, not crossed out by any width counted out. */
|
|
15
|
+
export declare function isLeft(options: SieveOptions, counted: readonly number[], number: number): boolean;
|
|
16
|
+
export declare const sieve: Operation<SieveOptions>;
|
|
17
|
+
export interface SieveLeastOptions {
|
|
18
|
+
readonly sieve: SieveOptions;
|
|
19
|
+
/** The number the numbers counted lie above; -1 where any number counts. */
|
|
20
|
+
readonly above: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A number of the table above the goal's is left; the key is the greatest of the least such
|
|
24
|
+
* number, which each width counted out raises or leaves as it was.
|
|
25
|
+
*/
|
|
26
|
+
export declare const sieveLeast: Goal<SieveLeastOptions>;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// The sieve of a table of numbers: the numbers counted out by a width the scene gives, each number
|
|
2
|
+
// that leaves another remainder than the one given crossed out, as a statement counts things out
|
|
3
|
+
// in twos or in threes with one left over. What a state keeps is which widths were counted out,
|
|
4
|
+
// since the numbers crossed out follow from them; the goal asks the least number left.
|
|
5
|
+
import { array, integer, isObject, object, pointer } from "../../cores/json.js";
|
|
6
|
+
import { maxNumber, readTable } from "./sheet.js";
|
|
7
|
+
const member = "sieve";
|
|
8
|
+
function refuse(reason) {
|
|
9
|
+
return { ok: false, reason };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The widths the state has counted out by, ascending, or undefined where its member is no such
|
|
13
|
+
* list of the scene's widths: a state reported from outside the kind may hold one.
|
|
14
|
+
*/
|
|
15
|
+
export function countedOf(options, state) {
|
|
16
|
+
const value = state[member];
|
|
17
|
+
if (!Array.isArray(value)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const widths = value;
|
|
21
|
+
const ok = widths.every((width, i) => typeof width === "number" &&
|
|
22
|
+
options.by.includes(width) &&
|
|
23
|
+
(i === 0 || widths[i - 1] < width));
|
|
24
|
+
return ok ? widths : undefined;
|
|
25
|
+
}
|
|
26
|
+
/** Whether a number is left, not crossed out by any width counted out. */
|
|
27
|
+
export function isLeft(options, counted, number) {
|
|
28
|
+
return counted.every((width) => number % width === options.left);
|
|
29
|
+
}
|
|
30
|
+
export const sieve = {
|
|
31
|
+
name: "sieve",
|
|
32
|
+
schema: {
|
|
33
|
+
options: {
|
|
34
|
+
description: "The numbers of a table of numbers counted out by a width, as things are counted out in twos or threes: each number whose remainder by the width is not left is crossed out.",
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
by: {
|
|
38
|
+
description: "The widths the numbers may be counted out by, each a number of the table on no hole, which a tap counts out by, none given twice.",
|
|
39
|
+
type: "array",
|
|
40
|
+
items: {
|
|
41
|
+
description: "A width, a count of numbers.",
|
|
42
|
+
type: "integer",
|
|
43
|
+
minimum: 2,
|
|
44
|
+
maximum: maxNumber,
|
|
45
|
+
},
|
|
46
|
+
minItems: 1,
|
|
47
|
+
uniqueItems: true,
|
|
48
|
+
},
|
|
49
|
+
left: {
|
|
50
|
+
description: "The remainder a number keeps, by each width counted out, to be left; 0 by default, and less than every width.",
|
|
51
|
+
type: "integer",
|
|
52
|
+
minimum: 0,
|
|
53
|
+
maximum: maxNumber,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
required: ["by"],
|
|
57
|
+
unevaluatedProperties: false,
|
|
58
|
+
},
|
|
59
|
+
move: {
|
|
60
|
+
description: "The numbers counted out by one of the widths.",
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: {
|
|
63
|
+
by: {
|
|
64
|
+
description: "The width.",
|
|
65
|
+
type: "integer",
|
|
66
|
+
minimum: 2,
|
|
67
|
+
maximum: maxNumber,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
required: ["by"],
|
|
71
|
+
unevaluatedProperties: false,
|
|
72
|
+
},
|
|
73
|
+
state: {
|
|
74
|
+
member,
|
|
75
|
+
schema: {
|
|
76
|
+
description: "The widths the numbers have been counted out by, ascending.",
|
|
77
|
+
type: "array",
|
|
78
|
+
items: { description: "A width.", type: "integer", minimum: 2, maximum: maxNumber },
|
|
79
|
+
uniqueItems: true,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
read(value, sheet, path, errors) {
|
|
84
|
+
const before = errors.length;
|
|
85
|
+
const o = object(value, path, errors, ["by"], ["left"]);
|
|
86
|
+
if (o === undefined) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const first = readTable(sheet, path, errors, "a sieve counts them out");
|
|
90
|
+
const byPath = pointer(path, "by");
|
|
91
|
+
const by = [];
|
|
92
|
+
// The learner counts out by a width by a tap on its number, so the table holds it.
|
|
93
|
+
const onTable = (width) => first === undefined ||
|
|
94
|
+
(width >= first &&
|
|
95
|
+
width < first + sheet.rows * sheet.cols &&
|
|
96
|
+
!sheet.holes.has(width - first));
|
|
97
|
+
array(o.by, byPath, errors, 1)?.forEach((item, i) => {
|
|
98
|
+
const width = integer(item, pointer(byPath, i), errors, 2, maxNumber);
|
|
99
|
+
if (width !== undefined && by.includes(width)) {
|
|
100
|
+
errors.push({ path: pointer(byPath, i), message: "a width given twice" });
|
|
101
|
+
}
|
|
102
|
+
else if (width !== undefined && !onTable(width)) {
|
|
103
|
+
errors.push({
|
|
104
|
+
path: pointer(byPath, i),
|
|
105
|
+
message: "a number of the table, no hole: a tap on it counts out by it",
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else if (width !== undefined) {
|
|
109
|
+
by.push(width);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const leftPath = pointer(path, "left");
|
|
113
|
+
const left = o.left === undefined ? 0 : integer(o.left, leftPath, errors, 0, maxNumber);
|
|
114
|
+
if (left !== undefined && by.some((width) => left >= width)) {
|
|
115
|
+
errors.push({ path: leftPath, message: "less than every width: no remainder reaches it" });
|
|
116
|
+
}
|
|
117
|
+
return errors.length > before || left === undefined ? undefined : { by, left };
|
|
118
|
+
},
|
|
119
|
+
start: () => [],
|
|
120
|
+
play(_sheet, options, state, move) {
|
|
121
|
+
const width = isObject(move) && Object.keys(move).length === 1 ? move.by : undefined;
|
|
122
|
+
if (typeof width !== "number" || !options.by.includes(width)) {
|
|
123
|
+
return refuse("no-width");
|
|
124
|
+
}
|
|
125
|
+
const counted = countedOf(options, state);
|
|
126
|
+
if (counted === undefined) {
|
|
127
|
+
return refuse("no-sieve");
|
|
128
|
+
}
|
|
129
|
+
if (counted.includes(width)) {
|
|
130
|
+
return refuse("counted");
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
state: { ...state, [member]: [...counted, width].sort((a, b) => a - b) },
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
moves(_sheet, options, state) {
|
|
138
|
+
const counted = countedOf(options, state);
|
|
139
|
+
return counted === undefined
|
|
140
|
+
? []
|
|
141
|
+
: options.by.filter((width) => !counted.includes(width)).map((width) => ({ by: width }));
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
/** The least number of the table left above the goal's, none on a hole; undefined where none is. */
|
|
145
|
+
function leastLeft(sheet, goal, state) {
|
|
146
|
+
const counted = countedOf(goal.sieve, state);
|
|
147
|
+
if (counted === undefined) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
const first = sheet.first;
|
|
151
|
+
for (let cell = Math.max(0, goal.above + 1 - first); cell < sheet.rows * sheet.cols; cell++) {
|
|
152
|
+
if (!sheet.holes.has(cell) && isLeft(goal.sieve, counted, first + cell)) {
|
|
153
|
+
return first + cell;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* A number of the table above the goal's is left; the key is the greatest of the least such
|
|
160
|
+
* number, which each width counted out raises or leaves as it was.
|
|
161
|
+
*/
|
|
162
|
+
export const sieveLeast = {
|
|
163
|
+
name: "sieve_least",
|
|
164
|
+
schema: {
|
|
165
|
+
description: "A number of the table above the given one is left, none on a hole; the key is the greatest the least such number grows to as the widths are counted out. The scene allows the operation sieve, and a number of the table is left once every width is counted out.",
|
|
166
|
+
type: "object",
|
|
167
|
+
properties: {
|
|
168
|
+
above: {
|
|
169
|
+
description: "The number the numbers left lie above; any number of the table by default.",
|
|
170
|
+
type: "integer",
|
|
171
|
+
minimum: 0,
|
|
172
|
+
maximum: maxNumber,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
unevaluatedProperties: false,
|
|
176
|
+
},
|
|
177
|
+
read(value, sheet, path, errors, operations) {
|
|
178
|
+
const before = errors.length;
|
|
179
|
+
const o = object(value, path, errors, [], ["above"]);
|
|
180
|
+
const above = o?.above === undefined ? -1 : integer(o.above, pointer(path, "above"), errors, 0, maxNumber);
|
|
181
|
+
if (!operations.has(sieve.name)) {
|
|
182
|
+
errors.push({ path, message: "the scene allows no operation sieve, so nothing is left" });
|
|
183
|
+
}
|
|
184
|
+
// Where the sieve's own parameters do not read, the scene is refused for them alone.
|
|
185
|
+
const options = operations.get(sieve.name) ?? { by: [], left: 0 };
|
|
186
|
+
if (errors.length > before || above === undefined) {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
const goal = { sieve: options, above };
|
|
190
|
+
// Each width counted out leaves the least number as it was or raises it, so the key is the
|
|
191
|
+
// least number left once every width is: on the table, or the key would be another.
|
|
192
|
+
const all = { marks: {}, [member]: [...options.by].sort((a, b) => a - b) };
|
|
193
|
+
// A sheet with no numbers is the sieve's own error.
|
|
194
|
+
if (sheet.first !== undefined && leastLeft(sheet, goal, all) === undefined) {
|
|
195
|
+
errors.push({
|
|
196
|
+
path,
|
|
197
|
+
message: "no number of the table above it is left once every width is counted out",
|
|
198
|
+
});
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
return goal;
|
|
202
|
+
},
|
|
203
|
+
meets: (sheet, goal, state) => leastLeft(sheet, goal, state) !== undefined,
|
|
204
|
+
asks: (sheet, goal) => ({
|
|
205
|
+
key: "max",
|
|
206
|
+
measure: (state) => leastLeft(sheet, goal, state),
|
|
207
|
+
}),
|
|
208
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const step: import("./operation.js").Operation<
|
|
1
|
+
export declare const step: import("./operation.js").Operation<import("./piece.js").Start>, stepOn: import("./operation.js").Goal<import("./piece.js").On>, stepReach: import("./operation.js").Goal<import("./piece.js").Reach>;
|
package/lib/kinds/cells/step.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// in a fenced field: a wall between the two cells stops it, as a hole does.
|
|
3
3
|
import { piece } from "./piece.js";
|
|
4
4
|
import { sideBySide, wallKey } from "./sheet.js";
|
|
5
|
-
export const { operation: step, goal: stepOn } = piece({
|
|
5
|
+
export const { operation: step, goal: stepOn, reach: stepReach, } = piece({
|
|
6
6
|
name: "step",
|
|
7
7
|
description: "A piece that steps across a side of its cell into the next cell, never into a hole or through a wall.",
|
|
8
8
|
refusal(sheet, from, to) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altnovatrail/widgets",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "NovaTrail's widget kinds: each kind's rules and solver over its states, and the shell they share.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
"types": "./lib/cores/search.d.ts",
|
|
31
31
|
"default": "./lib/cores/search.js"
|
|
32
32
|
},
|
|
33
|
+
"./cores/enumerate": {
|
|
34
|
+
"types": "./lib/cores/enumerate.d.ts",
|
|
35
|
+
"default": "./lib/cores/enumerate.js"
|
|
36
|
+
},
|
|
33
37
|
"./cores/random": {
|
|
34
38
|
"types": "./lib/cores/random.d.ts",
|
|
35
39
|
"default": "./lib/cores/random.js"
|