@altnovatrail/widgets 0.2.0 → 0.2.1
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/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +321 -0
- package/kinds/line/vectors/cases.json +213 -0
- package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
- package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
- package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
- package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
- package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
- package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
- package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
- package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
- package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
- package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
- package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
- package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
- package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
- package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
- package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
- package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
- package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
- package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
- package/kinds/line/vectors/invalid-line.json +34 -0
- package/kinds/line/vectors/invalid-ring.json +34 -0
- package/kinds/line/vectors/invalid.json +43 -0
- package/kinds/line/vectors/jump-unreachable.json +104 -0
- package/kinds/line/vectors/limit.json +40 -0
- package/kinds/line/vectors/op-not-in-scene.json +47 -0
- package/kinds/line/vectors/ring-cut.json +104 -0
- package/kinds/line/vectors/ring-every.json +107 -0
- package/kinds/line/vectors/ring-jump.json +135 -0
- package/lib/cores/arith.d.ts +62 -0
- package/lib/cores/arith.js +197 -0
- package/lib/cores/kind.d.ts +10 -0
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.js +36 -8
- package/lib/kinds/cells/kind.js +11 -64
- package/lib/kinds/cells/operation.d.ts +6 -0
- package/lib/kinds/cells/polyomino.d.ts +14 -0
- package/lib/kinds/cells/polyomino.js +100 -24
- package/lib/kinds/line/cut.d.ts +2 -0
- package/lib/kinds/line/cut.js +63 -0
- package/lib/kinds/line/goals.d.ts +22 -0
- package/lib/kinds/line/goals.js +137 -0
- package/lib/kinds/line/index.d.ts +13 -0
- package/lib/kinds/line/index.js +17 -0
- package/lib/kinds/line/jump.d.ts +10 -0
- package/lib/kinds/line/jump.js +95 -0
- package/lib/kinds/line/kind.d.ts +25 -0
- package/lib/kinds/line/kind.js +125 -0
- package/lib/kinds/line/line.d.ts +63 -0
- package/lib/kinds/line/line.js +157 -0
- package/lib/kinds/line/operation.d.ts +69 -0
- package/lib/kinds/line/operation.js +1 -0
- package/lib/kinds/line/posts.d.ts +11 -0
- package/lib/kinds/line/posts.js +85 -0
- package/lib/kinds/line/registry.d.ts +2 -0
- package/lib/kinds/line/registry.js +11 -0
- package/lib/kinds/line/schema.d.ts +4 -0
- package/lib/kinds/line/schema.js +157 -0
- package/package.json +9 -2
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// The goals of the kind line. A statement of counting gaps asks a number of a built line — how many
|
|
2
|
+
// gaps between six trees, how long a fence of eight posts — or how many moves build it — how many
|
|
3
|
+
// cuts make four pieces, how many jumps reach the top —, so one goal names counters: the values a
|
|
4
|
+
// state's counters are to have, and, where it asks a number of the state rather than the fewest
|
|
5
|
+
// moves, the counter whose greatest or least value it asks. The other asks a post on every place of
|
|
6
|
+
// a stretch, as pages read from one to another, both included, or bushes round a flowerbed.
|
|
7
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
8
|
+
import { integer, isObject, map, object, pointer, string } from "../../cores/json.js";
|
|
9
|
+
import { lastPlace, placeOn, placesOf, readPlace } from "./line.js";
|
|
10
|
+
/** The greatest value a goal asks a counter to have. */
|
|
11
|
+
export const maxValue = 1_000_000_000;
|
|
12
|
+
const counterName = (description) => ({ description, $ref: "#/$defs/counter" });
|
|
13
|
+
export const counters = {
|
|
14
|
+
name: "counters",
|
|
15
|
+
schema: {
|
|
16
|
+
description: "A state whose counters have the values given; the key is the fewest moves to one, or, where max or min names a counter, its greatest or least value over such states. At least one of is, max and min; never both max and min.",
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
is: {
|
|
20
|
+
description: "The counters, each by its name, with the value it is to have.",
|
|
21
|
+
type: "object",
|
|
22
|
+
propertyNames: { $ref: "#/$defs/counter" },
|
|
23
|
+
additionalProperties: {
|
|
24
|
+
description: "The value the counter is to have.",
|
|
25
|
+
type: "integer",
|
|
26
|
+
minimum: 0,
|
|
27
|
+
maximum: maxValue,
|
|
28
|
+
},
|
|
29
|
+
minProperties: 1,
|
|
30
|
+
},
|
|
31
|
+
max: counterName("The counter whose greatest value the key is."),
|
|
32
|
+
min: counterName("The counter whose least value the key is."),
|
|
33
|
+
},
|
|
34
|
+
minProperties: 1,
|
|
35
|
+
// Never both max and min. Both are named again here: a validator in strict mode, as Ajv, takes
|
|
36
|
+
// a required member only where the same schema names it.
|
|
37
|
+
not: {
|
|
38
|
+
properties: {
|
|
39
|
+
max: { description: "Max, never beside min." },
|
|
40
|
+
min: { description: "Min, never beside max." },
|
|
41
|
+
},
|
|
42
|
+
required: ["max", "min"],
|
|
43
|
+
},
|
|
44
|
+
unevaluatedProperties: false,
|
|
45
|
+
},
|
|
46
|
+
read(value, _line, path, errors, known) {
|
|
47
|
+
const before = errors.length;
|
|
48
|
+
const o = object(value, path, errors, [], ["is", "max", "min"]);
|
|
49
|
+
if (o === undefined) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const counter = (name, at) => {
|
|
53
|
+
const named = string(name, at, errors, /^[a-z][a-z0-9_]*$/, "a counter's name");
|
|
54
|
+
if (named !== undefined && !known.has(named)) {
|
|
55
|
+
errors.push({ path: at, message: "no counter of the scene has this name" });
|
|
56
|
+
}
|
|
57
|
+
return named === undefined ? undefined : known.get(named);
|
|
58
|
+
};
|
|
59
|
+
const is = [];
|
|
60
|
+
if (o.is !== undefined) {
|
|
61
|
+
const isPath = pointer(path, "is");
|
|
62
|
+
for (const [name, item] of Object.entries(map(o.is, isPath, errors) ?? {})) {
|
|
63
|
+
const at = pointer(isPath, name);
|
|
64
|
+
const count = counter(name, at);
|
|
65
|
+
const wanted = integer(item, at, errors, 0, maxValue);
|
|
66
|
+
if (count !== undefined && wanted !== undefined) {
|
|
67
|
+
is.push([count, wanted, name]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const max = o.max === undefined ? undefined : counter(o.max, pointer(path, "max"));
|
|
72
|
+
const min = o.min === undefined ? undefined : counter(o.min, pointer(path, "min"));
|
|
73
|
+
if (o.max !== undefined && o.min !== undefined) {
|
|
74
|
+
errors.push({ path, message: "max or min, never both" });
|
|
75
|
+
}
|
|
76
|
+
const key = o.max !== undefined ? "max" : "min";
|
|
77
|
+
const measured = key === "max" ? max : min;
|
|
78
|
+
if (o[key] === undefined && (!isObject(o.is) || Object.keys(o.is).length === 0)) {
|
|
79
|
+
errors.push({ path, message: "a counter's value to have, or a counter to measure" });
|
|
80
|
+
}
|
|
81
|
+
if (errors.length > before) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return measured === undefined
|
|
85
|
+
? { is }
|
|
86
|
+
: { is, asks: { key, count: measured, name: String(o[key]) } };
|
|
87
|
+
},
|
|
88
|
+
meets: (_line, goal, state) => goal.is.every(([count, wanted]) => count(state) === wanted),
|
|
89
|
+
asks: (_line, goal) => goal.asks === undefined ? { key: "fewest" } : { key: goal.asks.key, measure: goal.asks.count },
|
|
90
|
+
};
|
|
91
|
+
export const every = {
|
|
92
|
+
name: "every",
|
|
93
|
+
schema: {
|
|
94
|
+
description: "A post on every place from one to another, both included; the whole line where neither is given.",
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
from: {
|
|
98
|
+
description: "The first place of the stretch; the line's first where left out.",
|
|
99
|
+
$ref: "#/$defs/place",
|
|
100
|
+
},
|
|
101
|
+
to: {
|
|
102
|
+
description: "The last place of the stretch, never before from on an open line, and on a ring past the last place round to it where before from; the line's last where left out.",
|
|
103
|
+
$ref: "#/$defs/place",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
unevaluatedProperties: false,
|
|
107
|
+
},
|
|
108
|
+
read(value, line, path, errors) {
|
|
109
|
+
const before = errors.length;
|
|
110
|
+
const o = object(value, path, errors, [], ["from", "to"]);
|
|
111
|
+
if (o === undefined) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const from = o.from === undefined ? line.first : readPlace(line, o.from, pointer(path, "from"), errors);
|
|
115
|
+
const to = o.to === undefined ? lastPlace(line) : readPlace(line, o.to, pointer(path, "to"), errors);
|
|
116
|
+
if (from !== undefined && to !== undefined && to < from && !line.closed) {
|
|
117
|
+
errors.push({ path: pointer(path, "to"), message: "a place no earlier than from" });
|
|
118
|
+
}
|
|
119
|
+
return from === undefined || to === undefined || errors.length > before
|
|
120
|
+
? undefined
|
|
121
|
+
: { from, to };
|
|
122
|
+
},
|
|
123
|
+
meets(line, stretch, state) {
|
|
124
|
+
const posts = new Set(placesOf(line, state, "posts"));
|
|
125
|
+
// On a ring the stretch goes forward from its first place, past the last to the first where it
|
|
126
|
+
// ends before it starts.
|
|
127
|
+
const count = line.closed
|
|
128
|
+
? divideWithRemainder(stretch.to - stretch.from, line.places).remainder + 1
|
|
129
|
+
: stretch.to - stretch.from + 1;
|
|
130
|
+
for (let i = 0; i < count; i++) {
|
|
131
|
+
if (!posts.has(placeOn(line, stretch.from, i))) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
},
|
|
137
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The most states a scene's whole state graph may hold, at a build and on a device alike: the
|
|
3
|
+
* same as the kind cells', which bounds a hint on a phone; posts on eleven free places, the
|
|
4
|
+
* course's largest scene of posts, take 2,048 states.
|
|
5
|
+
*/
|
|
6
|
+
export declare const limit = 3000;
|
|
7
|
+
/**
|
|
8
|
+
* The most work a scene's whole state graph may take, as the search core counts it, at a build and
|
|
9
|
+
* on a device alike: the same as the kind cells'. A state of the line carries a few numbers, so the
|
|
10
|
+
* course's scenes take a small part of it; a change of it proves every scene again.
|
|
11
|
+
*/
|
|
12
|
+
export declare const workLimit = 6000000;
|
|
13
|
+
export declare const line: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./line.js").State, import("./kind.js").Move>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The kind line as a module's build and the widget take it: the registry's operations and goals,
|
|
2
|
+
// within the kind's one limit of states and one of work.
|
|
3
|
+
import { lineKind } from "./kind.js";
|
|
4
|
+
import { registry } from "./registry.js";
|
|
5
|
+
/**
|
|
6
|
+
* The most states a scene's whole state graph may hold, at a build and on a device alike: the
|
|
7
|
+
* same as the kind cells', which bounds a hint on a phone; posts on eleven free places, the
|
|
8
|
+
* course's largest scene of posts, take 2,048 states.
|
|
9
|
+
*/
|
|
10
|
+
export const limit = 3_000;
|
|
11
|
+
/**
|
|
12
|
+
* The most work a scene's whole state graph may take, as the search core counts it, at a build and
|
|
13
|
+
* on a device alike: the same as the kind cells'. A state of the line carries a few numbers, so the
|
|
14
|
+
* course's scenes take a small part of it; a change of it proves every scene again.
|
|
15
|
+
*/
|
|
16
|
+
export const workLimit = 6_000_000;
|
|
17
|
+
export const line = lineKind(registry, limit, workLimit);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Operation } from "./operation.js";
|
|
2
|
+
export interface Jumper {
|
|
3
|
+
/** The place it starts on. */
|
|
4
|
+
readonly from: number;
|
|
5
|
+
/** The places one jump spans. */
|
|
6
|
+
readonly step: number;
|
|
7
|
+
/** Whether it may jump back too. */
|
|
8
|
+
readonly back: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare const jump: Operation<Jumper>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// A jumper on the line — a frog on stones, a boy on slabs, a girl going up floor by floor — that
|
|
2
|
+
// stands on a place and jumps a step of places at a time: forward, towards greater numbers, and
|
|
3
|
+
// back where the scene allows it, never off an open line; on a ring it goes on round it, past the
|
|
4
|
+
// last place to the first. The state keeps its place under "at".
|
|
5
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
6
|
+
import { integer, object, pointer } from "../../cores/json.js";
|
|
7
|
+
import { lastPlace, maxPlaces, placeOn, readBoolean, readPlace, } from "./line.js";
|
|
8
|
+
const directions = ["forward", "back"];
|
|
9
|
+
function refuse(reason) {
|
|
10
|
+
return { ok: false, reason };
|
|
11
|
+
}
|
|
12
|
+
/** The jumper's place in a state; its start where the state holds no place of the line. */
|
|
13
|
+
function placeOf(line, jumper, state) {
|
|
14
|
+
const at = state.at;
|
|
15
|
+
return Number.isInteger(at) && at >= line.first && at <= lastPlace(line)
|
|
16
|
+
? at
|
|
17
|
+
: jumper.from;
|
|
18
|
+
}
|
|
19
|
+
export const jump = {
|
|
20
|
+
name: "jump",
|
|
21
|
+
schema: {
|
|
22
|
+
options: {
|
|
23
|
+
description: "A jumper that starts on a place and jumps a step of places at a time, forward and, where the scene allows it, back, never off an open line and round a ring.",
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
from: { description: "The place it starts on, by its number.", $ref: "#/$defs/place" },
|
|
27
|
+
step: {
|
|
28
|
+
description: "The places one jump spans, fewer than the line's places.",
|
|
29
|
+
type: "integer",
|
|
30
|
+
minimum: 1,
|
|
31
|
+
maximum: maxPlaces - 1,
|
|
32
|
+
},
|
|
33
|
+
back: {
|
|
34
|
+
description: "Whether it may jump back, towards smaller numbers, too; false where left out.",
|
|
35
|
+
type: "boolean",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
required: ["from", "step"],
|
|
39
|
+
unevaluatedProperties: false,
|
|
40
|
+
},
|
|
41
|
+
move: {
|
|
42
|
+
description: "A jump: forward, towards greater numbers, or back.",
|
|
43
|
+
enum: [...directions],
|
|
44
|
+
},
|
|
45
|
+
state: {
|
|
46
|
+
member: "at",
|
|
47
|
+
schema: { description: "The place the jumper stands on.", $ref: "#/$defs/place" },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
read(value, line, path, errors) {
|
|
51
|
+
const before = errors.length;
|
|
52
|
+
const o = object(value, path, errors, ["from", "step"], ["back"]);
|
|
53
|
+
if (o === undefined) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const from = readPlace(line, o.from, pointer(path, "from"), errors);
|
|
57
|
+
const step = integer(o.step, pointer(path, "step"), errors, 1, line.places - 1);
|
|
58
|
+
const back = o.back === undefined ? false : readBoolean(o.back, pointer(path, "back"), errors);
|
|
59
|
+
return from === undefined || step === undefined || back === undefined || errors.length > before
|
|
60
|
+
? undefined
|
|
61
|
+
: { from, step, back };
|
|
62
|
+
},
|
|
63
|
+
start: (_line, jumper) => jumper.from,
|
|
64
|
+
play(line, jumper, state, move) {
|
|
65
|
+
if (move !== "forward" && move !== "back") {
|
|
66
|
+
return refuse("not-a-jump");
|
|
67
|
+
}
|
|
68
|
+
if (move === "back" && !jumper.back) {
|
|
69
|
+
return refuse("no-back");
|
|
70
|
+
}
|
|
71
|
+
const to = placeOn(line, placeOf(line, jumper, state), move === "forward" ? jumper.step : -jumper.step);
|
|
72
|
+
if (to < line.first || to > lastPlace(line)) {
|
|
73
|
+
return refuse("off-line");
|
|
74
|
+
}
|
|
75
|
+
return { ok: true, state: { ...state, at: to } };
|
|
76
|
+
},
|
|
77
|
+
moves: (_line, jumper) => (jumper.back ? directions : ["forward"]),
|
|
78
|
+
counters: {
|
|
79
|
+
at: {
|
|
80
|
+
description: "The number of the place the jumper stands on.",
|
|
81
|
+
value: (line, jumper, state) => placeOf(line, jumper, state),
|
|
82
|
+
},
|
|
83
|
+
distance: {
|
|
84
|
+
description: "The length from the jumper's start to where it stands, their count of places apart times the spacing; on a ring, the shorter way round.",
|
|
85
|
+
value(line, jumper, state) {
|
|
86
|
+
const apart = placeOf(line, jumper, state) - jumper.from;
|
|
87
|
+
if (!line.closed) {
|
|
88
|
+
return Math.abs(apart) * line.spacing;
|
|
89
|
+
}
|
|
90
|
+
const on = divideWithRemainder(apart, line.places).remainder;
|
|
91
|
+
return Math.min(on, line.places - on) * line.spacing;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Json, Kind } from "../../cores/kind.js";
|
|
2
|
+
import { type Line, type State } from "./line.js";
|
|
3
|
+
import type { Goal, Operation, Registry } from "./operation.js";
|
|
4
|
+
export interface Scene {
|
|
5
|
+
readonly line: Line;
|
|
6
|
+
/** The operations the scene allows, with their parameters, in the registry's order. */
|
|
7
|
+
readonly operations: readonly {
|
|
8
|
+
readonly op: Operation;
|
|
9
|
+
readonly options: unknown;
|
|
10
|
+
}[];
|
|
11
|
+
readonly goal: {
|
|
12
|
+
readonly goal: Goal;
|
|
13
|
+
readonly options: unknown;
|
|
14
|
+
} | null;
|
|
15
|
+
}
|
|
16
|
+
/** A move: one member, named by its operation, holding what the operation's move holds. */
|
|
17
|
+
export type Move = {
|
|
18
|
+
readonly [operation: string]: Json;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The kind line with the operations and goals of the registry and the given limits of states and
|
|
22
|
+
* of work, none of work where none is given.
|
|
23
|
+
* Throws where the registry repeats a name, a member of the state or a counter.
|
|
24
|
+
*/
|
|
25
|
+
export declare function lineKind(registry: Registry, limit: number, workLimit?: number): Kind<Scene, State, Move>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The kind line, assembled from a registry of operations and goals: a scene is a line, the
|
|
2
|
+
// operations it allows with their parameters, and at most one goal. A move names one operation,
|
|
3
|
+
// which plays it by its own rules; the kind only reads the line and dispatches.
|
|
4
|
+
import { map, object, pointer } from "../../cores/json.js";
|
|
5
|
+
import { checkRegistry, dispatch, inRegistryOrder, readGoal, readPart, takeName, } from "../../cores/registry.js";
|
|
6
|
+
import { readLine } from "./line.js";
|
|
7
|
+
import { lineCounters } from "./posts.js";
|
|
8
|
+
/**
|
|
9
|
+
* Throws where the registry could not make a kind: a name given twice, a member of the state or a
|
|
10
|
+
* counter shared.
|
|
11
|
+
*/
|
|
12
|
+
function check(registry) {
|
|
13
|
+
checkRegistry("line", registry.version, [...registry.operations, ...registry.goals]);
|
|
14
|
+
const members = new Set(["posts"]);
|
|
15
|
+
const counted = new Set(Object.keys(lineCounters));
|
|
16
|
+
for (const op of registry.operations) {
|
|
17
|
+
const member = op.schema.state?.member;
|
|
18
|
+
if ((member === undefined) !== (op.start === undefined)) {
|
|
19
|
+
throw new Error(`the operation ${op.name} keeps a member of the state and starts it, or neither`);
|
|
20
|
+
}
|
|
21
|
+
if (member !== undefined) {
|
|
22
|
+
takeName(members, member, "a member of the state");
|
|
23
|
+
}
|
|
24
|
+
for (const counter of Object.keys(op.counters ?? {})) {
|
|
25
|
+
takeName(counted, counter, "a counter's name");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** The counters of an operation bound to a scene's line and parameters, by their names. */
|
|
30
|
+
function bound(op, line, options) {
|
|
31
|
+
return Object.entries(op.counters ?? {}).map(([named, counter]) => [
|
|
32
|
+
named,
|
|
33
|
+
(state) => counter.value(line, options, state),
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The kind line with the operations and goals of the registry and the given limits of states and
|
|
38
|
+
* of work, none of work where none is given.
|
|
39
|
+
* Throws where the registry repeats a name, a member of the state or a counter.
|
|
40
|
+
*/
|
|
41
|
+
export function lineKind(registry, limit, workLimit = Number.MAX_SAFE_INTEGER) {
|
|
42
|
+
check(registry);
|
|
43
|
+
const operations = new Map(registry.operations.map((op) => [op.name, op]));
|
|
44
|
+
const goals = new Map(registry.goals.map((goal) => [goal.name, goal]));
|
|
45
|
+
return {
|
|
46
|
+
limit,
|
|
47
|
+
workLimit,
|
|
48
|
+
read(input) {
|
|
49
|
+
const errors = [];
|
|
50
|
+
const o = object(input, "", errors, ["line", "operations"], ["goal"]);
|
|
51
|
+
if (o === undefined) {
|
|
52
|
+
return { ok: false, errors };
|
|
53
|
+
}
|
|
54
|
+
const line = readLine(o.line, "/line", errors);
|
|
55
|
+
// The counters the scene has: the line's own, and those of each operation it names that the
|
|
56
|
+
// kind has, a count undefined where the line or the parameters do not read. Where the
|
|
57
|
+
// operations are no map, which ones the scene meant is unknown: the goal reads every
|
|
58
|
+
// operation's counters, and the map's own error stands alone.
|
|
59
|
+
const counters = new Map(Object.entries(lineCounters).map(([named, counter]) => [
|
|
60
|
+
named,
|
|
61
|
+
line === undefined ? undefined : (state) => counter.value(line, undefined, state),
|
|
62
|
+
]));
|
|
63
|
+
const chosen = new Map();
|
|
64
|
+
const allowed = map(o.operations, "/operations", errors);
|
|
65
|
+
if (allowed === undefined) {
|
|
66
|
+
for (const op of registry.operations) {
|
|
67
|
+
for (const named of Object.keys(op.counters ?? {}))
|
|
68
|
+
counters.set(named, undefined);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const [named, value] of Object.entries(allowed ?? {})) {
|
|
72
|
+
const at = pointer("/operations", named);
|
|
73
|
+
const op = operations.get(named);
|
|
74
|
+
if (op === undefined) {
|
|
75
|
+
errors.push({ path: at, message: "no operation of the kind has this name" });
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
for (const counter of Object.keys(op.counters ?? {}))
|
|
79
|
+
counters.set(counter, undefined);
|
|
80
|
+
const read = line === undefined
|
|
81
|
+
? undefined
|
|
82
|
+
: readPart((v, p, e) => op.read(v, line, p, e), value, at, errors);
|
|
83
|
+
if (line !== undefined && read !== undefined) {
|
|
84
|
+
chosen.set(named, read.options);
|
|
85
|
+
for (const [counter, count] of bound(op, line, read.options)) {
|
|
86
|
+
counters.set(counter, count);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const goal = readGoal(o.goal, goals, errors, line === undefined
|
|
91
|
+
? undefined
|
|
92
|
+
: (found, v, p, e) => found.read(v, line, p, e, counters));
|
|
93
|
+
if (errors.length > 0 || line === undefined) {
|
|
94
|
+
return { ok: false, errors };
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
scene: {
|
|
99
|
+
line,
|
|
100
|
+
operations: inRegistryOrder(registry.operations, chosen),
|
|
101
|
+
goal,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
start(scene) {
|
|
106
|
+
const state = { posts: [...scene.line.posts] };
|
|
107
|
+
for (const { op, options } of scene.operations) {
|
|
108
|
+
if (op.schema.state !== undefined && op.start !== undefined) {
|
|
109
|
+
state[op.schema.state.member] = op.start(scene.line, options);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return state;
|
|
113
|
+
},
|
|
114
|
+
// The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
|
|
115
|
+
play(scene, state, move) {
|
|
116
|
+
return dispatch(scene.operations, operations, move, (op, options, held) => op.play(scene.line, options, state, held));
|
|
117
|
+
},
|
|
118
|
+
moves(scene, state) {
|
|
119
|
+
return scene.operations.flatMap(({ op, options }) => op.moves(scene.line, options, state).map((move) => ({ [op.name]: move })));
|
|
120
|
+
},
|
|
121
|
+
hasGoal: (scene) => scene.goal !== null,
|
|
122
|
+
meets: (scene, state) => scene.goal?.goal.meets(scene.line, scene.goal.options, state) ?? false,
|
|
123
|
+
asks: (scene) => scene.goal?.goal.asks?.(scene.line, scene.goal.options) ?? { key: "fewest" },
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Json, SceneError } from "../../cores/kind.js";
|
|
2
|
+
/** The most places a line holds: the numbers from 0 to 100 among them. */
|
|
3
|
+
export declare const maxPlaces = 101;
|
|
4
|
+
/** The greatest number of a place, the last place's too. */
|
|
5
|
+
export declare const maxNumber = 10000;
|
|
6
|
+
/** The greatest spacing between two neighbouring places. */
|
|
7
|
+
export declare const maxSpacing = 10000;
|
|
8
|
+
export interface Line {
|
|
9
|
+
/** The count of places. */
|
|
10
|
+
readonly places: number;
|
|
11
|
+
/** The number of the first place; each next place has the next number. */
|
|
12
|
+
readonly first: number;
|
|
13
|
+
/** The length from a place to the next, in the statement's unit. */
|
|
14
|
+
readonly spacing: number;
|
|
15
|
+
/** Whether the line is drawn upright, its first place at the bottom: floors, stairs, a well. */
|
|
16
|
+
readonly vertical: boolean;
|
|
17
|
+
/** Whether the line is closed into a ring, its last place next to its first. */
|
|
18
|
+
readonly closed: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The ring's sides, each by its count of gaps, where it is drawn as a figure with corners — a
|
|
21
|
+
* square flowerbed, a garden's fence —, the first place a corner and each next corner a side on;
|
|
22
|
+
* null for a round ring and for an open line. It changes no rule.
|
|
23
|
+
*/
|
|
24
|
+
readonly sides: readonly number[] | null;
|
|
25
|
+
/** The places a post stands on at the scene's start, by their numbers, in ascending order. */
|
|
26
|
+
readonly posts: readonly number[];
|
|
27
|
+
}
|
|
28
|
+
/** A state of the kind: the posts under "posts", and a member for each operation that keeps one. */
|
|
29
|
+
export type State = {
|
|
30
|
+
readonly [member: string]: Json;
|
|
31
|
+
};
|
|
32
|
+
/** The number of the line's last place. */
|
|
33
|
+
export declare function lastPlace(line: Line): number;
|
|
34
|
+
/**
|
|
35
|
+
* The number of the place a count of places on from a place, back where the count is negative: on
|
|
36
|
+
* a ring counted round it, by the core of exact arithmetic; on an open line the plain sum, which
|
|
37
|
+
* may fall off the line.
|
|
38
|
+
*/
|
|
39
|
+
export declare function placeOn(line: Line, place: number, by: number): number;
|
|
40
|
+
/** Whether a value is the number of a place of the line. */
|
|
41
|
+
export declare function isPlace(line: Line, value: unknown): value is number;
|
|
42
|
+
/** A place of the line, by its number, or undefined with its error added. */
|
|
43
|
+
export declare function readPlace(line: Line, value: unknown, path: string, errors: SceneError[]): number | undefined;
|
|
44
|
+
/** A boolean, or undefined with its error added. */
|
|
45
|
+
export declare function readBoolean(value: unknown, path: string, errors: SceneError[]): boolean | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Places of the line by their numbers, none twice, in ascending order, or undefined with their
|
|
48
|
+
* errors added.
|
|
49
|
+
*/
|
|
50
|
+
export declare function readPlaces(line: Line, value: unknown, path: string, errors: SceneError[]): number[] | undefined;
|
|
51
|
+
/** The line of a scene, or undefined with its errors added. */
|
|
52
|
+
export declare function readLine(value: unknown, path: string, errors: SceneError[]): Line | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The numbers of a state's member, a list of places, each once, in ascending order. A list that is
|
|
55
|
+
* no array, an item that is no place of the line and a place named again count as none, and the
|
|
56
|
+
* order the list gives is not read: a state reported from outside the kind may hold any of them,
|
|
57
|
+
* and no rule counts it.
|
|
58
|
+
*/
|
|
59
|
+
export declare function placesOf(line: Line, state: State, member: string): readonly number[];
|
|
60
|
+
/** The places with one more, in ascending order: a new array, the old one left as it was. */
|
|
61
|
+
export declare function withPlace(places: readonly number[], place: number): number[];
|
|
62
|
+
/** The move's member "at", a place of the line, or undefined where the move holds no such place. */
|
|
63
|
+
export declare function moveAt(line: Line, move: Json): number | undefined;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// The line of the kind line: places in a row, numbered from the scene's first number on, each a
|
|
2
|
+
// spacing from the next, and the posts that stand on places; open, or closed into a ring, whose
|
|
3
|
+
// last place is next to its first, as bushes round a flowerbed or a fence round a garden. A place
|
|
4
|
+
// is named by its number, as a statement names a floor, a page or a post, so a scene, a move and a
|
|
5
|
+
// task's predicate use the numbers the statement uses; the length between two places is their
|
|
6
|
+
// count of gaps times the spacing, in the statement's own unit — metres, steps, minutes.
|
|
7
|
+
import { divideWithRemainder } from "../../cores/arith.js";
|
|
8
|
+
import { array, integer, isObject, object, pointer } from "../../cores/json.js";
|
|
9
|
+
/** The most places a line holds: the numbers from 0 to 100 among them. */
|
|
10
|
+
export const maxPlaces = 101;
|
|
11
|
+
/** The greatest number of a place, the last place's too. */
|
|
12
|
+
export const maxNumber = 10_000;
|
|
13
|
+
/** The greatest spacing between two neighbouring places. */
|
|
14
|
+
export const maxSpacing = 10_000;
|
|
15
|
+
/** The number of the line's last place. */
|
|
16
|
+
export function lastPlace(line) {
|
|
17
|
+
return line.first + line.places - 1;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The number of the place a count of places on from a place, back where the count is negative: on
|
|
21
|
+
* a ring counted round it, by the core of exact arithmetic; on an open line the plain sum, which
|
|
22
|
+
* may fall off the line.
|
|
23
|
+
*/
|
|
24
|
+
export function placeOn(line, place, by) {
|
|
25
|
+
if (!line.closed) {
|
|
26
|
+
return place + by;
|
|
27
|
+
}
|
|
28
|
+
return line.first + divideWithRemainder(place - line.first + by, line.places).remainder;
|
|
29
|
+
}
|
|
30
|
+
/** Whether a value is the number of a place of the line. */
|
|
31
|
+
export function isPlace(line, value) {
|
|
32
|
+
return (Number.isInteger(value) &&
|
|
33
|
+
value >= line.first &&
|
|
34
|
+
value <= lastPlace(line));
|
|
35
|
+
}
|
|
36
|
+
/** A place of the line, by its number, or undefined with its error added. */
|
|
37
|
+
export function readPlace(line, value, path, errors) {
|
|
38
|
+
if (!isPlace(line, value)) {
|
|
39
|
+
errors.push({
|
|
40
|
+
path,
|
|
41
|
+
message: `a place of the line, a whole number from ${line.first} to ${lastPlace(line)}`,
|
|
42
|
+
});
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
/** A boolean, or undefined with its error added. */
|
|
48
|
+
export function readBoolean(value, path, errors) {
|
|
49
|
+
if (typeof value !== "boolean") {
|
|
50
|
+
errors.push({ path, message: "true or false" });
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Places of the line by their numbers, none twice, in ascending order, or undefined with their
|
|
57
|
+
* errors added.
|
|
58
|
+
*/
|
|
59
|
+
export function readPlaces(line, value, path, errors) {
|
|
60
|
+
const before = errors.length;
|
|
61
|
+
const places = [];
|
|
62
|
+
array(value, path, errors)?.forEach((item, i) => {
|
|
63
|
+
const at = pointer(path, i);
|
|
64
|
+
const place = readPlace(line, item, at, errors);
|
|
65
|
+
if (place !== undefined && places.includes(place)) {
|
|
66
|
+
errors.push({ path: at, message: "a place named twice" });
|
|
67
|
+
}
|
|
68
|
+
else if (place !== undefined) {
|
|
69
|
+
places.push(place);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return errors.length > before || !Array.isArray(value) ? undefined : places.sort((a, b) => a - b);
|
|
73
|
+
}
|
|
74
|
+
/** The sides of a ring by their counts of gaps, or undefined with their errors added. */
|
|
75
|
+
function readSides(value, places, path, errors) {
|
|
76
|
+
const before = errors.length;
|
|
77
|
+
const sides = [];
|
|
78
|
+
array(value, path, errors, 3, maxPlaces)?.forEach((item, i) => {
|
|
79
|
+
const side = integer(item, pointer(path, i), errors, 1, maxPlaces - 1);
|
|
80
|
+
if (side !== undefined)
|
|
81
|
+
sides.push(side);
|
|
82
|
+
});
|
|
83
|
+
if (errors.length > before) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (sides.reduce((sum, side) => sum + side, 0) !== places) {
|
|
87
|
+
errors.push({ path, message: `the sides' gaps together are the ring's places, ${places}` });
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
return sides;
|
|
91
|
+
}
|
|
92
|
+
/** The line of a scene, or undefined with its errors added. */
|
|
93
|
+
export function readLine(value, path, errors) {
|
|
94
|
+
const before = errors.length;
|
|
95
|
+
const o = object(value, path, errors, ["places"], ["first", "spacing", "vertical", "closed", "sides", "posts"]);
|
|
96
|
+
if (o === undefined) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
const places = integer(o.places, pointer(path, "places"), errors, 2, maxPlaces);
|
|
100
|
+
const first = o.first === undefined ? 0 : integer(o.first, pointer(path, "first"), errors, 0, maxNumber);
|
|
101
|
+
const spacing = o.spacing === undefined
|
|
102
|
+
? 1
|
|
103
|
+
: integer(o.spacing, pointer(path, "spacing"), errors, 1, maxSpacing);
|
|
104
|
+
const vertical = o.vertical === undefined ? false : readBoolean(o.vertical, pointer(path, "vertical"), errors);
|
|
105
|
+
const closed = o.closed === undefined ? false : readBoolean(o.closed, pointer(path, "closed"), errors);
|
|
106
|
+
if (closed === true && vertical === true) {
|
|
107
|
+
errors.push({ path: pointer(path, "vertical"), message: "a ring is never drawn upright" });
|
|
108
|
+
}
|
|
109
|
+
let sides = null;
|
|
110
|
+
if (o.sides !== undefined && closed === false) {
|
|
111
|
+
errors.push({ path: pointer(path, "sides"), message: "sides only of a ring, a closed line" });
|
|
112
|
+
}
|
|
113
|
+
else if (o.sides !== undefined && closed === true && places !== undefined) {
|
|
114
|
+
sides = readSides(o.sides, places, pointer(path, "sides"), errors);
|
|
115
|
+
}
|
|
116
|
+
if (places === undefined ||
|
|
117
|
+
first === undefined ||
|
|
118
|
+
spacing === undefined ||
|
|
119
|
+
vertical === undefined ||
|
|
120
|
+
closed === undefined ||
|
|
121
|
+
sides === undefined) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
if (first + places - 1 > maxNumber) {
|
|
125
|
+
errors.push({ path, message: `the last place's number is at most ${maxNumber}` });
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const line = { places, first, spacing, vertical, closed, sides, posts: [] };
|
|
129
|
+
const posts = o.posts === undefined ? [] : readPlaces(line, o.posts, pointer(path, "posts"), errors);
|
|
130
|
+
return posts === undefined || errors.length > before ? undefined : { ...line, posts };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The numbers of a state's member, a list of places, each once, in ascending order. A list that is
|
|
134
|
+
* no array, an item that is no place of the line and a place named again count as none, and the
|
|
135
|
+
* order the list gives is not read: a state reported from outside the kind may hold any of them,
|
|
136
|
+
* and no rule counts it.
|
|
137
|
+
*/
|
|
138
|
+
export function placesOf(line, state, member) {
|
|
139
|
+
const value = state[member];
|
|
140
|
+
if (!Array.isArray(value)) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
const places = new Set(value.filter((item) => isPlace(line, item)));
|
|
144
|
+
return [...places].sort((a, b) => a - b);
|
|
145
|
+
}
|
|
146
|
+
/** The places with one more, in ascending order: a new array, the old one left as it was. */
|
|
147
|
+
export function withPlace(places, place) {
|
|
148
|
+
return [...places, place].sort((a, b) => a - b);
|
|
149
|
+
}
|
|
150
|
+
/** The move's member "at", a place of the line, or undefined where the move holds no such place. */
|
|
151
|
+
export function moveAt(line, move) {
|
|
152
|
+
if (!isObject(move)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const keys = Object.keys(move);
|
|
156
|
+
return keys.length === 1 && keys[0] === "at" && isPlace(line, move.at) ? move.at : undefined;
|
|
157
|
+
}
|