@altnovatrail/widgets 0.2.0 → 0.3.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.
Files changed (97) hide show
  1. package/README.md +1 -1
  2. package/cores/vector.schema.json +22 -1
  3. package/kinds/cards/cards.handles.json +109 -0
  4. package/kinds/cards/cards.schema.json +483 -0
  5. package/kinds/cards/vectors/any.json +255 -0
  6. package/kinds/cards/vectors/box.json +325 -0
  7. package/kinds/cards/vectors/fork-taps.json +305 -0
  8. package/kinds/cards/vectors/fork.json +254 -0
  9. package/kinds/cards/vectors/invalid-goal.json +70 -0
  10. package/kinds/cards/vectors/invalid-ids.json +113 -0
  11. package/kinds/cards/vectors/invalid-schema.json +88 -0
  12. package/kinds/cards/vectors/invalid-taps.json +214 -0
  13. package/kinds/cards/vectors/key.json +264 -0
  14. package/kinds/cards/vectors/limit.json +274 -0
  15. package/kinds/cards/vectors/no-goal.json +205 -0
  16. package/kinds/cards/vectors/none.json +239 -0
  17. package/kinds/cards/vectors/recog.json +113 -0
  18. package/kinds/cards/vectors/riddle.json +397 -0
  19. package/kinds/cards/vectors/solo.json +218 -0
  20. package/kinds/cells/cells.schema.json +17 -2
  21. package/kinds/line/line.handles.json +195 -0
  22. package/kinds/line/line.schema.json +336 -0
  23. package/kinds/line/vectors/cases.json +213 -0
  24. package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
  25. package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
  26. package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
  27. package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
  28. package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
  29. package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
  30. package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
  31. package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
  32. package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
  33. package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
  34. package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
  35. package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
  36. package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
  37. package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
  38. package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
  39. package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
  40. package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
  41. package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
  42. package/kinds/line/vectors/invalid-line.json +34 -0
  43. package/kinds/line/vectors/invalid-ring.json +34 -0
  44. package/kinds/line/vectors/invalid.json +43 -0
  45. package/kinds/line/vectors/jump-unreachable.json +104 -0
  46. package/kinds/line/vectors/limit.json +40 -0
  47. package/kinds/line/vectors/op-not-in-scene.json +47 -0
  48. package/kinds/line/vectors/ring-cut.json +104 -0
  49. package/kinds/line/vectors/ring-every.json +107 -0
  50. package/kinds/line/vectors/ring-jump.json +135 -0
  51. package/lib/cores/arith.d.ts +62 -0
  52. package/lib/cores/arith.js +197 -0
  53. package/lib/cores/kind.d.ts +31 -3
  54. package/lib/cores/registry.d.ts +43 -0
  55. package/lib/cores/registry.js +87 -0
  56. package/lib/cores/search.d.ts +22 -6
  57. package/lib/cores/search.js +70 -14
  58. package/lib/kinds/cards/goal.d.ts +25 -0
  59. package/lib/kinds/cards/goal.js +72 -0
  60. package/lib/kinds/cards/index.d.ts +14 -0
  61. package/lib/kinds/cards/index.js +17 -0
  62. package/lib/kinds/cards/kind.d.ts +23 -0
  63. package/lib/kinds/cards/kind.js +101 -0
  64. package/lib/kinds/cards/parts.d.ts +36 -0
  65. package/lib/kinds/cards/parts.js +34 -0
  66. package/lib/kinds/cards/scene.d.ts +102 -0
  67. package/lib/kinds/cards/scene.js +356 -0
  68. package/lib/kinds/cards/schema.d.ts +10 -0
  69. package/lib/kinds/cards/schema.js +285 -0
  70. package/lib/kinds/cells/kind.js +11 -64
  71. package/lib/kinds/cells/operation.d.ts +6 -0
  72. package/lib/kinds/cells/polyomino.d.ts +14 -0
  73. package/lib/kinds/cells/polyomino.js +100 -24
  74. package/lib/kinds/cells/registry.js +1 -1
  75. package/lib/kinds/cells/schema.js +3 -1
  76. package/lib/kinds/line/cut.d.ts +2 -0
  77. package/lib/kinds/line/cut.js +63 -0
  78. package/lib/kinds/line/goals.d.ts +22 -0
  79. package/lib/kinds/line/goals.js +137 -0
  80. package/lib/kinds/line/index.d.ts +13 -0
  81. package/lib/kinds/line/index.js +17 -0
  82. package/lib/kinds/line/jump.d.ts +10 -0
  83. package/lib/kinds/line/jump.js +95 -0
  84. package/lib/kinds/line/kind.d.ts +25 -0
  85. package/lib/kinds/line/kind.js +125 -0
  86. package/lib/kinds/line/line.d.ts +63 -0
  87. package/lib/kinds/line/line.js +157 -0
  88. package/lib/kinds/line/operation.d.ts +69 -0
  89. package/lib/kinds/line/operation.js +1 -0
  90. package/lib/kinds/line/posts.d.ts +11 -0
  91. package/lib/kinds/line/posts.js +85 -0
  92. package/lib/kinds/line/registry.d.ts +2 -0
  93. package/lib/kinds/line/registry.js +11 -0
  94. package/lib/kinds/line/schema.d.ts +4 -0
  95. package/lib/kinds/line/schema.js +159 -0
  96. package/package.json +16 -4
  97. package/cores/handles.schema.json +0 -63
@@ -0,0 +1,72 @@
1
+ import { shared, } from "./scene.js";
2
+ /** The cards a goal ranges over, in the scene's order. */
3
+ export function range(scene, among) {
4
+ return scene.groups.filter((group) => among.includes(group.id)).flatMap((group) => group.cards);
5
+ }
6
+ /** Whether a card meets a literal: has the value it names, or any other. */
7
+ export function fits(card, literal) {
8
+ const value = card.signs.get(literal.sign);
9
+ return "is" in literal ? value === literal.is : value !== literal.not;
10
+ }
11
+ function holds(card, condition) {
12
+ if ("all" in condition)
13
+ return condition.all.every((c) => holds(card, c));
14
+ if ("any" in condition)
15
+ return condition.any.some((c) => holds(card, c));
16
+ return fits(card, condition);
17
+ }
18
+ /** A condition's literals, depth first, in the order the witness gives a card's fits. */
19
+ export function literals(condition) {
20
+ if ("all" in condition)
21
+ return condition.all.flatMap(literals);
22
+ if ("any" in condition)
23
+ return condition.any.flatMap(literals);
24
+ return [condition];
25
+ }
26
+ /** Whether two cards give the same value to every sign named. */
27
+ const same = (a, b, signs) => signs.every((sign) => a.signs.get(sign) === b.signs.get(sign));
28
+ const byCodeUnits = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
29
+ /**
30
+ * The cards of the goal's range that meet it, by their IDs sorted by UTF-16 code units, and the
31
+ * witness: the rule — the condition the goal names, the values the group shares as an all of them,
32
+ * or the signs a card must differ by — and a row for each card of the range in the scene's order,
33
+ * with whether it fits each literal of the rule, depth first, or the first other card it does not
34
+ * differ from, none for a card that differs from every other.
35
+ */
36
+ export function answer(scene) {
37
+ const goal = scene.goal;
38
+ if (goal === null)
39
+ return null;
40
+ const cards = range(scene, goal.among);
41
+ if ("differs" in goal) {
42
+ const rows = cards.map((card) => {
43
+ const twin = cards.find((other) => other !== card && same(card, other, goal.differs));
44
+ return twin === undefined ? { card: card.id } : { card: card.id, same: twin.id };
45
+ });
46
+ const parts = cards
47
+ .filter((card) => !cards.some((other) => other !== card && same(card, other, goal.differs)))
48
+ .map((card) => card.id);
49
+ return {
50
+ parts: parts.sort(byCodeUnits),
51
+ witness: { rule: { differs: [...goal.differs] }, rows },
52
+ };
53
+ }
54
+ const condition = "signs" in goal
55
+ ? goal.signs
56
+ : // The reading takes only a group of the scene for the one a goal learns from.
57
+ { all: shared(scene.groups.find((group) => group.id === goal.like), scene.signs) };
58
+ const each = literals(condition);
59
+ const rows = cards.map((card) => ({ card: card.id, fits: each.map((literal) => fits(card, literal)) }));
60
+ const parts = cards.filter((card) => holds(card, condition)).map((card) => card.id);
61
+ return { parts: parts.sort(byCodeUnits), witness: { rule: conditionJson(condition), rows } };
62
+ }
63
+ /** A condition as the scene's JSON gives it. */
64
+ export function conditionJson(condition) {
65
+ if ("all" in condition)
66
+ return { all: condition.all.map(conditionJson) };
67
+ if ("any" in condition)
68
+ return { any: condition.any.map(conditionJson) };
69
+ return "is" in condition
70
+ ? { sign: condition.sign, is: condition.is }
71
+ : { sign: condition.sign, not: condition.not };
72
+ }
@@ -0,0 +1,14 @@
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 kinds cells' and line's. A scene whose taps choose one card has a state for each
4
+ * card and option and one with none; one whose taps choose many has every set of them, so eleven
5
+ * cards and options to choose take 2,048 states, and twelve pass the limit.
6
+ */
7
+ export declare const limit = 3000;
8
+ /**
9
+ * The most work a scene's whole state graph may take, as the search core counts it: the same as
10
+ * the kinds cells' and line's. A state of cards is a short list of IDs, so a graph within the
11
+ * limit of states takes a small part of it.
12
+ */
13
+ export declare const workLimit = 6000000;
14
+ export declare const cards: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./kind.js").State, import("./kind.js").Move>;
@@ -0,0 +1,17 @@
1
+ // The kind cards as a module's build and the widget take it, within the kind's one limit of states
2
+ // and one of work.
3
+ import { cardsKind } from "./kind.js";
4
+ /**
5
+ * The most states a scene's whole state graph may hold, at a build and on a device alike: the
6
+ * same as the kinds cells' and line's. A scene whose taps choose one card has a state for each
7
+ * card and option and one with none; one whose taps choose many has every set of them, so eleven
8
+ * cards and options to choose take 2,048 states, and twelve pass the limit.
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: the same as
13
+ * the kinds cells' and line's. A state of cards is a short list of IDs, so a graph within the
14
+ * limit of states takes a small part of it.
15
+ */
16
+ export const workLimit = 6_000_000;
17
+ export const cards = cardsKind(limit, workLimit);
@@ -0,0 +1,23 @@
1
+ import type { Kind } from "../../cores/kind.js";
2
+ import { type Answer } from "./goal.js";
3
+ import { type Scene as Given } from "./scene.js";
4
+ /** A scene as the kind plays it: as read, with what a tap may choose and the goal's answer. */
5
+ export interface Scene extends Given {
6
+ /** The IDs a tap chooses, in the scene's order: the cards of the groups chosen from, then the options. */
7
+ readonly choosable: readonly string[];
8
+ readonly answer: Answer | null;
9
+ }
10
+ /** What is chosen, by the IDs of the cards and options, sorted by UTF-16 code units. */
11
+ export type State = {
12
+ readonly chosen: readonly string[];
13
+ };
14
+ /** A tap on a card or an option, by its ID. */
15
+ export type Move = {
16
+ readonly choose: string;
17
+ };
18
+ /**
19
+ * The kind cards with its limits of states and of work. A move refuses what is no tap, not-a-move;
20
+ * an ID of no card and no option of the scene, no-part; and one the scene does not let a tap choose,
21
+ * not-choosable.
22
+ */
23
+ export declare function cardsKind(limit: number, workLimit: number): Kind<Scene, State, Move>;
@@ -0,0 +1,101 @@
1
+ // The kind cards: a scene of cards in groups, options and a table of signs, a tap that chooses a
2
+ // card or an option, a state of what is chosen, and a goal whose answer is the cards that meet it,
3
+ // read off the scene by the solver with no search. Its state graph is the sets a learner's taps
4
+ // can choose, which the search core still lays and holds within the kind's limits, so that a
5
+ // lesson's walkthrough leads to the answer by taps.
6
+ import { isObject } from "../../cores/json.js";
7
+ import { answer } from "./goal.js";
8
+ import { readScene } from "./scene.js";
9
+ const byCodeUnits = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
10
+ /**
11
+ * What a state holds of what may be chosen, each once and sorted: a state reported from outside the
12
+ * kind may hold an ID twice, out of order, or one no tap chooses, and the rules count no such item.
13
+ */
14
+ function chosen(scene, state) {
15
+ const held = Array.isArray(state.chosen) ? state.chosen : [];
16
+ return scene.choosable.filter((id) => held.includes(id)).sort(byCodeUnits);
17
+ }
18
+ /**
19
+ * The kind cards with its limits of states and of work. A move refuses what is no tap, not-a-move;
20
+ * an ID of no card and no option of the scene, no-part; and one the scene does not let a tap choose,
21
+ * not-choosable.
22
+ */
23
+ export function cardsKind(limit, workLimit) {
24
+ return {
25
+ limit,
26
+ workLimit,
27
+ read(input) {
28
+ const read = readScene(input);
29
+ if (!read.ok)
30
+ return read;
31
+ const scene = read.scene;
32
+ const from = scene.groups.filter((group) => scene.choose?.from.includes(group.id));
33
+ const choosable = scene.choose === null
34
+ ? []
35
+ : [
36
+ ...from.flatMap((group) => group.cards.map((card) => card.id)),
37
+ ...scene.options.map((option) => option.id),
38
+ ];
39
+ const found = answer(scene);
40
+ // A scene of taps is met by taps alone: its answer is cards a tap chooses, one where a tap
41
+ // chooses one, and never none, which the scene's own state would meet before any tap.
42
+ if (found !== null &&
43
+ scene.choose !== null &&
44
+ (found.parts.length === 0 ||
45
+ (!scene.choose.many && found.parts.length > 1) ||
46
+ found.parts.some((id) => !choosable.includes(id)))) {
47
+ return {
48
+ ok: false,
49
+ errors: [
50
+ {
51
+ path: "/goal",
52
+ message: scene.choose.many
53
+ ? "a goal whose answer is cards a tap chooses, at least one"
54
+ : "a goal whose answer is one card a tap chooses",
55
+ },
56
+ ],
57
+ };
58
+ }
59
+ return { ok: true, scene: { ...scene, choosable, answer: found } };
60
+ },
61
+ start: () => ({ chosen: [] }),
62
+ // The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
63
+ play(scene, state, move) {
64
+ const m = move;
65
+ if (!isObject(m) || Object.keys(m).length !== 1 || typeof m.choose !== "string") {
66
+ return { ok: false, reason: "not-a-move" };
67
+ }
68
+ const id = m.choose;
69
+ const known = scene.options.some((option) => option.id === id) ||
70
+ scene.groups.some((group) => group.cards.some((card) => card.id === id));
71
+ if (!known)
72
+ return { ok: false, reason: "no-part" };
73
+ if (!scene.choosable.includes(id))
74
+ return { ok: false, reason: "not-choosable" };
75
+ const now = chosen(scene, state);
76
+ // One choice leaves whatever else a state from outside the kind held.
77
+ const many = scene.choose?.many === true;
78
+ const next = now.includes(id)
79
+ ? many
80
+ ? now.filter((held) => held !== id)
81
+ : []
82
+ : many
83
+ ? [...now, id].sort(byCodeUnits)
84
+ : [id];
85
+ return { ok: true, state: { chosen: next } };
86
+ },
87
+ moves: (scene) => scene.choosable.map((id) => ({ choose: id })),
88
+ hasGoal: (scene) => scene.goal !== null,
89
+ meets(scene, state) {
90
+ // A scene only for the pen has nothing a state could hold chosen.
91
+ if (scene.answer === null || scene.choosable.length === 0)
92
+ return false;
93
+ const now = chosen(scene, state);
94
+ return (now.length === scene.answer.parts.length &&
95
+ now.every((id, i) => id === scene.answer?.parts[i]));
96
+ },
97
+ asks: (scene) => scene.answer === null
98
+ ? { key: "fewest" }
99
+ : { key: "parts", parts: scene.answer.parts, witness: scene.answer.witness },
100
+ };
101
+ }
@@ -0,0 +1,36 @@
1
+ import type { Scene } from "./scene.js";
2
+ export type Part = {
3
+ readonly id: string;
4
+ readonly of: "group";
5
+ readonly group: string;
6
+ } | {
7
+ readonly id: string;
8
+ readonly of: "card";
9
+ readonly card: string;
10
+ readonly group: string;
11
+ } | {
12
+ readonly id: string;
13
+ readonly of: "option";
14
+ } | {
15
+ readonly id: string;
16
+ readonly of: "row";
17
+ readonly card: string;
18
+ } | {
19
+ readonly id: string;
20
+ readonly of: "column";
21
+ readonly column: string;
22
+ } | {
23
+ readonly id: string;
24
+ readonly of: "cell";
25
+ readonly card: string;
26
+ readonly column: string;
27
+ };
28
+ export declare const rowId: (card: string) => string;
29
+ export declare const columnId: (column: string) => string;
30
+ export declare const cellId: (card: string, column: string) => string;
31
+ /**
32
+ * Every part of the scene, in its order: each group and its cards, the options, then the table's
33
+ * rows, columns and cells, row by row. The rows go in the order the table names their groups,
34
+ * as the panel lays them.
35
+ */
36
+ export declare function parts(scene: Scene): Part[];
@@ -0,0 +1,34 @@
1
+ export const rowId = (card) => `row:${card}`;
2
+ export const columnId = (column) => `col:${column}`;
3
+ export const cellId = (card, column) => `cell:${card}:${column}`;
4
+ /**
5
+ * Every part of the scene, in its order: each group and its cards, the options, then the table's
6
+ * rows, columns and cells, row by row. The rows go in the order the table names their groups,
7
+ * as the panel lays them.
8
+ */
9
+ export function parts(scene) {
10
+ const all = [];
11
+ for (const group of scene.groups) {
12
+ all.push({ id: group.id, of: "group", group: group.id });
13
+ for (const card of group.cards) {
14
+ all.push({ id: card.id, of: "card", card: card.id, group: group.id });
15
+ }
16
+ }
17
+ for (const option of scene.options)
18
+ all.push({ id: option.id, of: "option" });
19
+ if (scene.table !== null) {
20
+ const { rows, columns } = scene.table;
21
+ const cards = rows.flatMap((id) => scene.groups.find((g) => g.id === id)?.cards ?? []);
22
+ for (const card of cards)
23
+ all.push({ id: rowId(card.id), of: "row", card: card.id });
24
+ for (const column of columns) {
25
+ all.push({ id: columnId(column.id), of: "column", column: column.id });
26
+ }
27
+ for (const card of cards) {
28
+ for (const column of columns) {
29
+ all.push({ id: cellId(card.id, column.id), of: "cell", card: card.id, column: column.id });
30
+ }
31
+ }
32
+ }
33
+ return all;
34
+ }
@@ -0,0 +1,102 @@
1
+ import type { Read } from "../../cores/kind.js";
2
+ /** The ID of a card, a group or an option: a part's ID, as the widget gives it, by itself. */
3
+ export declare const partId: RegExp;
4
+ /** The ID of a sign, a value or a column: kebab-case, with no colon, so a part's ID holds it. */
5
+ export declare const nameId: RegExp;
6
+ export declare const maxSigns = 10;
7
+ export declare const maxValues = 12;
8
+ export declare const maxGroups = 6;
9
+ export declare const maxCards = 12;
10
+ export declare const maxOptions = 6;
11
+ export declare const maxColumns = 8;
12
+ export declare const maxLabel = 80;
13
+ export declare const maxText = 300;
14
+ /** The most conditions an all or an any joins, and the deepest they nest, a literal at depth 1. */
15
+ export declare const maxJoined = 8;
16
+ export declare const maxDepth = 4;
17
+ export interface Value {
18
+ readonly id: string;
19
+ readonly label: string;
20
+ }
21
+ export interface Sign {
22
+ readonly id: string;
23
+ readonly label: string;
24
+ readonly values: readonly Value[];
25
+ }
26
+ export interface Card {
27
+ readonly id: string;
28
+ /** A card of a task: the text of its statement, drawn in place of a picture; null for a creature. */
29
+ readonly text: string | null;
30
+ /** The value the card gives each sign of the scene, by the sign's ID. */
31
+ readonly signs: ReadonlyMap<string, string>;
32
+ }
33
+ export interface Group {
34
+ readonly id: string;
35
+ readonly label: string | null;
36
+ readonly cards: readonly Card[];
37
+ }
38
+ export interface Option {
39
+ readonly id: string;
40
+ readonly label: string;
41
+ }
42
+ /** A sign that has a value, or that has any other. */
43
+ export type Literal = {
44
+ readonly sign: string;
45
+ readonly is: string;
46
+ } | {
47
+ readonly sign: string;
48
+ readonly not: string;
49
+ };
50
+ export type Condition = Literal | {
51
+ readonly all: readonly Condition[];
52
+ } | {
53
+ readonly any: readonly Condition[];
54
+ };
55
+ export interface Column {
56
+ readonly id: string;
57
+ readonly label: string;
58
+ readonly literal: Literal;
59
+ }
60
+ export interface Table {
61
+ /** The groups whose cards are the table's rows, in the order the scene names them. */
62
+ readonly rows: readonly string[];
63
+ readonly columns: readonly Column[];
64
+ }
65
+ export interface Choose {
66
+ readonly many: boolean;
67
+ /** The groups whose cards a tap chooses, as the scene names them; options are always chosen so. */
68
+ readonly from: readonly string[];
69
+ }
70
+ /**
71
+ * What a state is to hold: the cards of the groups it ranges over that meet a condition of signs;
72
+ * that have every value the cards of a group share; or that differ from every other card of the
73
+ * range by at least one of the signs named.
74
+ */
75
+ export type Goal = {
76
+ readonly signs: Condition;
77
+ readonly among: readonly string[];
78
+ } | {
79
+ readonly like: string;
80
+ readonly among: readonly string[];
81
+ } | {
82
+ readonly differs: readonly string[];
83
+ readonly among: readonly string[];
84
+ };
85
+ export interface Scene {
86
+ readonly signs: readonly Sign[];
87
+ readonly groups: readonly Group[];
88
+ readonly options: readonly Option[];
89
+ readonly choose: Choose | null;
90
+ readonly table: Table | null;
91
+ readonly goal: Goal | null;
92
+ }
93
+ /**
94
+ * The values every card of a group gives, as literals in the order of the scene's signs: the rule
95
+ * the group teaches.
96
+ */
97
+ export declare function shared(group: Group, signs: readonly Sign[]): Literal[];
98
+ /**
99
+ * A scene read by the kind's schema, or every error it names: the taps, the table and the goal are
100
+ * read once the signs, the groups and the options are, against what they name.
101
+ */
102
+ export declare function readScene(input: unknown): Read<Scene>;