@altnovatrail/picture-kit 0.1.1 → 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.
Files changed (130) hide show
  1. package/README.md +1 -1
  2. package/lib/index.d.ts +45 -1
  3. package/lib/index.js +34 -3
  4. package/lib/kinds/balance.js +2 -3
  5. package/lib/kinds/calendar.d.ts +5 -1
  6. package/lib/kinds/calendar.js +33 -11
  7. package/lib/kinds/chart.js +2 -4
  8. package/lib/kinds/clock.d.ts +5 -0
  9. package/lib/kinds/clock.js +45 -4
  10. package/lib/kinds/coins.js +3 -2
  11. package/lib/kinds/column.js +2 -3
  12. package/lib/kinds/commands.d.ts +8 -6
  13. package/lib/kinds/commands.js +57 -15
  14. package/lib/kinds/containers.js +2 -1
  15. package/lib/kinds/countfig.d.ts +2 -0
  16. package/lib/kinds/countfig.js +6 -1
  17. package/lib/kinds/cubes.d.ts +29 -5
  18. package/lib/kinds/cubes.js +82 -34
  19. package/lib/kinds/die.d.ts +8 -0
  20. package/lib/kinds/die.js +75 -14
  21. package/lib/kinds/domino.js +3 -3
  22. package/lib/kinds/dotgrid.d.ts +3 -0
  23. package/lib/kinds/dotgrid.js +6 -4
  24. package/lib/kinds/figure.js +5 -4
  25. package/lib/kinds/fold-parts.d.ts +0 -2
  26. package/lib/kinds/fold-parts.js +0 -4
  27. package/lib/kinds/fold.d.ts +20 -1
  28. package/lib/kinds/fold.js +15 -6
  29. package/lib/kinds/fraction.js +3 -2
  30. package/lib/kinds/gauges.js +2 -1
  31. package/lib/kinds/gears.d.ts +21 -2
  32. package/lib/kinds/gears.js +113 -62
  33. package/lib/kinds/grid.d.ts +5 -0
  34. package/lib/kinds/grid.js +30 -2
  35. package/lib/kinds/groups.d.ts +3 -1
  36. package/lib/kinds/groups.js +142 -28
  37. package/lib/kinds/lanes.js +3 -2
  38. package/lib/kinds/layers.d.ts +6 -0
  39. package/lib/kinds/layers.js +23 -4
  40. package/lib/kinds/line.d.ts +4 -0
  41. package/lib/kinds/line.js +117 -32
  42. package/lib/kinds/matchsticks.d.ts +4 -0
  43. package/lib/kinds/matchsticks.js +5 -3
  44. package/lib/kinds/net.js +4 -3
  45. package/lib/kinds/overlay.d.ts +23 -1
  46. package/lib/kinds/overlay.js +7 -3
  47. package/lib/kinds/pieces.js +3 -2
  48. package/lib/kinds/plane-parts.d.ts +0 -2
  49. package/lib/kinds/plane-parts.js +0 -6
  50. package/lib/kinds/plane.js +6 -6
  51. package/lib/kinds/ropes.js +1 -3
  52. package/lib/kinds/shapes.js +5 -7
  53. package/lib/kinds/solids.js +3 -2
  54. package/lib/kinds/tangram.js +2 -4
  55. package/lib/kinds/target.js +4 -6
  56. package/lib/kinds/tiles.js +5 -4
  57. package/lib/kinds/venn.js +3 -5
  58. package/lib/noto.js +1 -0
  59. package/lib/objects.js +2 -1
  60. package/lib/read.d.ts +2 -2
  61. package/lib/read.js +3 -3
  62. package/lib/solve/balance.d.ts +11 -0
  63. package/lib/solve/balance.js +116 -1
  64. package/lib/solve/calendar.d.ts +46 -0
  65. package/lib/solve/calendar.js +206 -0
  66. package/lib/solve/choice-scenes.d.ts +20 -0
  67. package/lib/solve/choice-scenes.js +51 -0
  68. package/lib/solve/clock.d.ts +54 -0
  69. package/lib/solve/clock.js +240 -0
  70. package/lib/solve/coins.d.ts +71 -0
  71. package/lib/solve/coins.js +222 -0
  72. package/lib/solve/commands.d.ts +36 -0
  73. package/lib/solve/commands.js +210 -0
  74. package/lib/solve/countfig.d.ts +41 -0
  75. package/lib/solve/countfig.js +199 -0
  76. package/lib/solve/cubes.d.ts +49 -0
  77. package/lib/solve/cubes.js +255 -0
  78. package/lib/solve/die.d.ts +39 -0
  79. package/lib/solve/die.js +93 -0
  80. package/lib/solve/domino.d.ts +57 -0
  81. package/lib/solve/domino.js +263 -0
  82. package/lib/solve/enumerate.d.ts +75 -0
  83. package/lib/solve/enumerate.js +323 -0
  84. package/lib/solve/figure.d.ts +26 -0
  85. package/lib/solve/figure.js +88 -0
  86. package/lib/solve/fold.d.ts +22 -0
  87. package/lib/solve/fold.js +68 -0
  88. package/lib/solve/gaps.d.ts +65 -0
  89. package/lib/solve/gaps.js +292 -0
  90. package/lib/solve/gears.d.ts +33 -0
  91. package/lib/solve/gears.js +130 -0
  92. package/lib/solve/grid.d.ts +35 -0
  93. package/lib/solve/grid.js +194 -0
  94. package/lib/solve/groups.d.ts +93 -0
  95. package/lib/solve/groups.js +346 -0
  96. package/lib/solve/index.d.ts +60 -0
  97. package/lib/solve/index.js +194 -0
  98. package/lib/solve/layers.d.ts +30 -0
  99. package/lib/solve/layers.js +72 -0
  100. package/lib/solve/magic.d.ts +32 -0
  101. package/lib/solve/magic.js +216 -0
  102. package/lib/solve/matchsticks.d.ts +37 -0
  103. package/lib/solve/matchsticks.js +214 -0
  104. package/lib/solve/order.d.ts +94 -0
  105. package/lib/solve/order.js +419 -0
  106. package/lib/solve/overlay.d.ts +22 -0
  107. package/lib/solve/overlay.js +56 -0
  108. package/lib/solve/parity.d.ts +22 -0
  109. package/lib/solve/parity.js +27 -0
  110. package/lib/solve/pattern.d.ts +62 -0
  111. package/lib/solve/pattern.js +533 -0
  112. package/lib/solve/pieces.d.ts +57 -0
  113. package/lib/solve/pieces.js +309 -0
  114. package/lib/solve/pigeonhole.d.ts +38 -0
  115. package/lib/solve/pigeonhole.js +95 -0
  116. package/lib/solve/proof.d.ts +115 -0
  117. package/lib/solve/proof.js +105 -0
  118. package/lib/solve/pyramid.d.ts +28 -0
  119. package/lib/solve/pyramid.js +161 -0
  120. package/lib/solve/question.d.ts +25 -0
  121. package/lib/solve/question.js +75 -0
  122. package/lib/solve/shapes.d.ts +30 -0
  123. package/lib/solve/shapes.js +182 -0
  124. package/lib/solve/target.d.ts +40 -0
  125. package/lib/solve/target.js +190 -0
  126. package/lib/style.d.ts +1 -1
  127. package/lib/style.js +1 -1
  128. package/lib/words.d.ts +15 -0
  129. package/lib/words.js +39 -0
  130. package/package.json +1 -1
@@ -0,0 +1,28 @@
1
+ import type { PyramidScene } from "../kinds/pyramid.ts";
2
+ import { type Solver } from "./proof.ts";
3
+ /**
4
+ * The question about a wall of bricks, a number pyramid, where every brick
5
+ * above the bottom row is the sum — or, with the rule product, the product —
6
+ * of the two bricks it rests on.
7
+ *
8
+ * - `brick`: the number of the asked brick, the one {"ask": true}, a number.
9
+ *
10
+ * A brick that shows a number gives it; an empty brick, a text or a thing
11
+ * is a number to find, and bricks with the same text or the same thing hold
12
+ * the same number. The whole wall comes from its bottom row, so the solver
13
+ * tries every whole number for each bottom brick that shows none — from 0,
14
+ * or from 1 for a product — up to the largest number the wall shows, and
15
+ * builds the wall up from it: no brick is less than one it rests under, so
16
+ * a bottom brick under a number is never larger than it, and one under none
17
+ * is one the wall does not decide. Each wall it finds passes the kind's own
18
+ * check of facts; the search stops after 1000000 tries. The answer is proved
19
+ * when every wall that agrees with the picture gives the asked brick one
20
+ * number; when they give it more, those are the candidates. A product wall
21
+ * that shows 0 is refused: any number may stand under a 0. It reads every
22
+ * row, and takes no givens.
23
+ */
24
+ export type PyramidQuestion = {
25
+ ask: "brick";
26
+ };
27
+ /** pyramidSolver finds a brick of a wall: the question of PyramidQuestion. */
28
+ export declare const pyramidSolver: Solver<PyramidScene, PyramidQuestion, Record<string, never>>;
@@ -0,0 +1,161 @@
1
+ import { contentOf, numberOf } from "../kinds/magic-parts.js";
2
+ import { check as checkScene } from "../render.js";
3
+ import { noScene, solved, unsolved } from "./proof.js";
4
+ import { askOf, noGivens, sceneless } from "./question.js";
5
+ const members = { brick: [] };
6
+ /** The search stops after so many tries. */
7
+ const tries = 1000000;
8
+ /** asksOf lists the place [row, brick] of every brick the question asks. */
9
+ function asksOf(scene) {
10
+ return scene.rows.flatMap((row, i) => row.flatMap((e, j) => (contentOf(e).ask ? [[i, j]] : [])));
11
+ }
12
+ /** tieOf is what makes two bricks hold one number: the same text, or the same thing. */
13
+ function tieOf(e) {
14
+ if (numberOf(e) !== undefined)
15
+ return undefined;
16
+ const c = contentOf(e);
17
+ if (c.text !== undefined)
18
+ return `text ${c.text}`;
19
+ if (c.thing !== undefined)
20
+ return `thing ${JSON.stringify(c.thing)}`;
21
+ return undefined;
22
+ }
23
+ function check(question, givens, scene) {
24
+ const problems = [];
25
+ noGivens(givens, problems);
26
+ const read = askOf(question, members, problems);
27
+ if (!read)
28
+ return problems;
29
+ if (!scene) {
30
+ sceneless(read.ask, "a scene of pyramid, the wall", problems);
31
+ return problems;
32
+ }
33
+ if (scene.rule === undefined)
34
+ problems.push("rule: brick needs the rule the bricks follow, sum or product");
35
+ const asks = asksOf(scene).length;
36
+ if (asks !== 1)
37
+ problems.push(`rows: brick needs exactly one {"ask": true}, the brick it asks; the wall has ${asks}`);
38
+ const shown = scene.rows.flat().flatMap((e) => {
39
+ const v = numberOf(e);
40
+ return v === undefined ? [] : [v];
41
+ });
42
+ if (shown.length === 0)
43
+ problems.push("rows: the wall shows no number to start from");
44
+ if (scene.rule === "product" && shown.includes(0))
45
+ problems.push("rows: a product wall that shows 0 may hold any number under the 0");
46
+ return problems;
47
+ }
48
+ /**
49
+ * walls tries the bottom row brick by brick from the left: once a bottom
50
+ * brick has its number, every brick whose last support it is gets its own,
51
+ * and the wall is pruned where a brick differs from its shown number or from
52
+ * a brick tied to it. It gives each number of the asked brick with a wall.
53
+ */
54
+ function walls(scene, asked) {
55
+ const rows = scene.rows;
56
+ const k = rows.length;
57
+ const n = rows[k - 1].length;
58
+ const product = scene.rule === "product";
59
+ const shown = rows.map((row) => row.map(numberOf));
60
+ const ties = rows.map((row) => row.map(tieOf));
61
+ const top = Math.max(...shown.flat().map((v) => v ?? 0));
62
+ const value = rows.map((row) => row.map(() => 0));
63
+ const tied = new Map();
64
+ const out = { answers: new Map(), stopped: false };
65
+ let left = tries;
66
+ // The brick j of row i rests, at last, on bottom brick j + (k - 1 - i).
67
+ const settle = (p) => {
68
+ const set = [];
69
+ for (let i = k - 1; i >= 0; i--) {
70
+ const j = p - (k - 1 - i);
71
+ if (j < 0 || j >= rows[i].length)
72
+ continue;
73
+ if (i < k - 1) {
74
+ const a = value[i + 1][j];
75
+ const b = value[i + 1][j + 1];
76
+ value[i][j] = product ? a * b : a + b;
77
+ }
78
+ const v = value[i][j];
79
+ const s = shown[i][j];
80
+ if (s !== undefined && s !== v)
81
+ return unset(set);
82
+ const tie = ties[i][j];
83
+ if (tie === undefined)
84
+ continue;
85
+ const held = tied.get(tie);
86
+ if (held && held[0] !== v)
87
+ return unset(set);
88
+ if (held)
89
+ held[1]++;
90
+ else
91
+ tied.set(tie, [v, 1]);
92
+ set.push(tie);
93
+ }
94
+ return set;
95
+ };
96
+ const unset = (set) => {
97
+ for (const tie of set) {
98
+ const held = tied.get(tie);
99
+ if (--held[1] === 0)
100
+ tied.delete(tie);
101
+ }
102
+ return undefined;
103
+ };
104
+ const go = (p) => {
105
+ if (p === n) {
106
+ const answer = value[asked[0]][asked[1]];
107
+ if (!out.answers.has(answer))
108
+ out.answers.set(answer, value.map((row) => [...row]));
109
+ return;
110
+ }
111
+ const s = shown[k - 1][p];
112
+ const from = s ?? (product ? 1 : 0);
113
+ const to = s ?? top;
114
+ for (let v = from; v <= to && !out.stopped; v++) {
115
+ if (--left < 0) {
116
+ out.stopped = true;
117
+ return;
118
+ }
119
+ value[k - 1][p] = v;
120
+ const set = settle(p);
121
+ if (!set)
122
+ continue;
123
+ go(p + 1);
124
+ unset(set);
125
+ }
126
+ };
127
+ go(0);
128
+ return out;
129
+ }
130
+ function solve(scene, _question) {
131
+ if (!scene)
132
+ return noScene;
133
+ const reads = scene.rows.map((_, i) => `rows[${i}]`);
134
+ const found = walls(scene, asksOf(scene)[0]);
135
+ if (found.stopped)
136
+ return unsolved(`the search stopped after ${tries} tries`, reads);
137
+ // Every wall found holds by the kind's own check of facts, as a check of the solver itself.
138
+ const told = [...found.answers]
139
+ .filter(([, values]) => checkScene({ ...scene, facts: { values } }).length === 0)
140
+ .map(([answer]) => answer)
141
+ .sort((a, b) => a - b);
142
+ const rule = scene.rule === "product" ? "product" : "sum";
143
+ if (told.length === 0)
144
+ return unsolved(`no whole numbers make every brick the ${rule} of the two under it`, reads);
145
+ if (told.length > 1)
146
+ return unsolved(`the wall allows each of ${told.join(", ")} for the brick asked`, reads, told.map((number) => ({ number })));
147
+ return solved({ number: told[0] }, reads);
148
+ }
149
+ /** The facts of a wall are its bricks that show a number. */
150
+ function facts(scene) {
151
+ return scene.rows.flatMap((row, i) => row.flatMap((e, j) => (numberOf(e) === undefined ? [] : [`rows[${i}][${j}]`])));
152
+ }
153
+ /** pyramidSolver finds a brick of a wall: the question of PyramidQuestion. */
154
+ export const pyramidSolver = {
155
+ name: "pyramid",
156
+ kinds: ["pyramid"],
157
+ questions: Object.keys(members),
158
+ check,
159
+ facts,
160
+ solve,
161
+ };
@@ -0,0 +1,25 @@
1
+ import { type Obj, type Problems } from "../read.ts";
2
+ /** askOf reads a question's ask, and refuses a member that its ask does not take. */
3
+ export declare function askOf<T extends string>(question: unknown, members: Record<T, readonly string[]>, problems: Problems): {
4
+ q: Obj;
5
+ ask: T;
6
+ } | undefined;
7
+ /** noGivens refuses givens for a solver that takes none: what it needs is in the scene. */
8
+ export declare function noGivens(givens: unknown, problems: Problems): void;
9
+ /**
10
+ * refOf reads a reference to one of a scene's list — a group, a good — as
11
+ * its index in the list or its label, and gives the index.
12
+ */
13
+ export declare function refOf(ref: unknown, labels: readonly (string | undefined)[], list: string, path: string, problems: Problems): number | undefined;
14
+ /**
15
+ * namedGroups refuses a question that answers with a group's label unless
16
+ * there are two groups or more, each with a label of its own.
17
+ */
18
+ export declare function namedGroups(labels: readonly (string | undefined)[], ask: string, problems: Problems): void;
19
+ /** flagOf reads a member that is true or false, if it is there. */
20
+ export declare function flagOf(value: unknown, path: string, problems: Problems): void;
21
+ /**
22
+ * sceneless refuses a question that reads a scene, asked of a task with none: what names the scene
23
+ * it reads, as "a scene of groups".
24
+ */
25
+ export declare function sceneless(ask: string, what: string, problems: Problems): void;
@@ -0,0 +1,75 @@
1
+ // Reading a question and its givens that came as JSON, the way src/read.ts reads a scene: each
2
+ // problem with its path, so that a refused task says exactly what to fix.
3
+ import { isObj, oneOf, only } from "../read.js";
4
+ /** askOf reads a question's ask, and refuses a member that its ask does not take. */
5
+ export function askOf(question, members, problems) {
6
+ const asks = Object.keys(members);
7
+ if (!isObj(question)) {
8
+ problems.push(`question: must be an object with ask, as {"ask": "${asks[0]}"}`);
9
+ return undefined;
10
+ }
11
+ const before = problems.length;
12
+ const ask = oneOf(question.ask, asks, "question.ask", problems);
13
+ if (problems.length > before)
14
+ return undefined;
15
+ only(question, ["ask", ...members[ask]], "question", problems, `a question ${ask}`);
16
+ return { q: question, ask };
17
+ }
18
+ /** noGivens refuses givens for a solver that takes none: what it needs is in the scene. */
19
+ export function noGivens(givens, problems) {
20
+ if (givens === undefined)
21
+ return;
22
+ if (!isObj(givens))
23
+ problems.push("givens: must be an object");
24
+ else
25
+ only(givens, [], "givens", problems, "the givens of this solver, which takes none");
26
+ }
27
+ /**
28
+ * refOf reads a reference to one of a scene's list — a group, a good — as
29
+ * its index in the list or its label, and gives the index.
30
+ */
31
+ export function refOf(ref, labels, list, path, problems) {
32
+ if (typeof ref === "number" && Number.isInteger(ref)) {
33
+ if (ref >= 0 && ref < labels.length)
34
+ return ref;
35
+ problems.push(`${path}: there is no ${list}[${ref}]`);
36
+ return undefined;
37
+ }
38
+ if (typeof ref === "string") {
39
+ const at = labels.indexOf(ref);
40
+ if (at >= 0 && labels.lastIndexOf(ref) === at)
41
+ return at;
42
+ if (at >= 0)
43
+ problems.push(`${path}: more than one of ${list} is labelled ${ref}`);
44
+ else
45
+ problems.push(`${path}: none of ${list} is labelled ${ref}`);
46
+ return undefined;
47
+ }
48
+ problems.push(`${path}: must be the index of one of ${list}, or its label`);
49
+ return undefined;
50
+ }
51
+ /**
52
+ * namedGroups refuses a question that answers with a group's label unless
53
+ * there are two groups or more, each with a label of its own.
54
+ */
55
+ export function namedGroups(labels, ask, problems) {
56
+ if (labels.length < 2)
57
+ problems.push(`question.ask: ${ask} needs two groups or more`);
58
+ const bare = labels.indexOf(undefined);
59
+ if (bare >= 0)
60
+ problems.push(`question.ask: ${ask} names a group by its label, and groups[${bare}] has none`);
61
+ else if (new Set(labels).size < labels.length)
62
+ problems.push(`question.ask: ${ask} names a group by its label, and two groups share one`);
63
+ }
64
+ /** flagOf reads a member that is true or false, if it is there. */
65
+ export function flagOf(value, path, problems) {
66
+ if (value !== undefined && typeof value !== "boolean")
67
+ problems.push(`${path}: must be true or false`);
68
+ }
69
+ /**
70
+ * sceneless refuses a question that reads a scene, asked of a task with none: what names the scene
71
+ * it reads, as "a scene of groups".
72
+ */
73
+ export function sceneless(ask, what, problems) {
74
+ problems.push(`scene: ${ask} reads ${what}, and the task has none`);
75
+ }
@@ -0,0 +1,30 @@
1
+ import type { ShapesScene } from "../kinds/shapes.ts";
2
+ import { type Solver } from "./proof.ts";
3
+ /**
4
+ * The questions about rows of shapes that stand for numbers, "▲ + ▲ = 8":
5
+ * the same shape in the same colour always stands for the same number.
6
+ * One row asks, its place {"ask": true} alone on one side of its "=".
7
+ *
8
+ * - `value`: the number of the shape of the asked row, "▲ = ?", a number.
9
+ * - `row-value`: the total of the asked row, "● + ▲ = ?" — shapes and whole
10
+ * numbers joined by +, two or more, a shape among them —, a number.
11
+ *
12
+ * Every other row is an equation or a comparison of shapes and whole
13
+ * numbers, joined by + − ×, with one =, < or >; it holds as the kind's check
14
+ * of facts works it out, × before + and −. The solver tries every whole
15
+ * number from 1 to 20 — or to the largest number the rows show, when that is
16
+ * larger, at most 1000 — for each kind of shape, and keeps the numbers
17
+ * under which every row holds, each row tried by the kind's own check as
18
+ * soon as its shapes have their numbers; the search stops after 200000
19
+ * tries, and a proof holds within that range. It proves the answer when the
20
+ * rows leave every shape one number; when they leave the asked row two
21
+ * numbers or more, those are the candidates, so "it cannot be told"; when
22
+ * they leave it one, but another shape two, the proof has no candidates: a
23
+ * row says what the task does not need. It reads every row, never facts, and
24
+ * takes no givens.
25
+ */
26
+ export type ShapesQuestion = {
27
+ ask: "value" | "row-value";
28
+ };
29
+ /** shapesSolver finds the numbers shapes stand for: the questions of ShapesQuestion. */
30
+ export declare const shapesSolver: Solver<ShapesScene, ShapesQuestion, Record<string, never>>;
@@ -0,0 +1,182 @@
1
+ import { check as checkScene } from "../render.js";
2
+ import { colors } from "../style.js";
3
+ import { noScene, solved, unsolved } from "./proof.js";
4
+ import { askOf, noGivens, sceneless } from "./question.js";
5
+ const members = { value: [], "row-value": [] };
6
+ /** A shape stands for a number from 1 to the largest number shown, at least least, at most most. */
7
+ const least = 20;
8
+ const most = 1000;
9
+ /** The search stops after so many tries. */
10
+ const tries = 200000;
11
+ const relations = ["=", "<", ">"];
12
+ const joins = ["+", "-", "×"];
13
+ const isPiece = (item) => "shape" in item;
14
+ const keyOf = (p) => `${p.color} ${p.shape}`;
15
+ const nameOf = (p) => `${colors[p.color].word} ${p.shape}`;
16
+ /** sideOk tells whether a side alternates terms — shapes and numbers — and ops of joins. */
17
+ function sideOk(items, ops) {
18
+ if (items.length % 2 === 0)
19
+ return false;
20
+ return items.every((item, i) => i % 2 === 0 ? isPiece(item) || "number" in item : "op" in item && ops.includes(item.op));
21
+ }
22
+ /** asksOf gives the place of every {"ask": true} of the rows, as [row, item]. */
23
+ function asksOf(scene) {
24
+ return (scene.rows ?? []).flatMap((row, i) => row.items.flatMap((item, j) => ("ask" in item ? [[i, j]] : [])));
25
+ }
26
+ /** askedSide is the side of the asked row that is not the ask, if the row is "… = ?" or "? = …". */
27
+ function askedSide(items) {
28
+ const at = items.findIndex((item) => "op" in item && relations.includes(item.op));
29
+ if (at < 0 || items.some((item, k) => k !== at && "op" in item && relations.includes(item.op)))
30
+ return undefined;
31
+ const mark = items[at];
32
+ if (mark.op !== "=")
33
+ return undefined;
34
+ const left = items.slice(0, at);
35
+ const right = items.slice(at + 1);
36
+ const alone = (side) => side.length === 1 && "ask" in side[0];
37
+ if (alone(right))
38
+ return left;
39
+ if (alone(left))
40
+ return right;
41
+ return undefined;
42
+ }
43
+ function check(question, givens, scene) {
44
+ const problems = [];
45
+ noGivens(givens, problems);
46
+ const read = askOf(question, members, problems);
47
+ if (!read)
48
+ return problems;
49
+ if (!scene) {
50
+ sceneless(read.ask, "a scene of shapes", problems);
51
+ return problems;
52
+ }
53
+ const { ask } = read;
54
+ if (!scene.rows) {
55
+ problems.push(`question.ask: ${ask} reads rows of shapes, and the scene is a table`);
56
+ return problems;
57
+ }
58
+ const asks = asksOf(scene);
59
+ if (asks.length !== 1)
60
+ problems.push(`rows: ${ask} needs exactly one {"ask": true}, the place it asks; the rows hold ${asks.length}`);
61
+ scene.rows.forEach((row, i) => {
62
+ const before = problems.length;
63
+ row.items.forEach((item, j) => {
64
+ const at = `rows[${i}].items[${j}]`;
65
+ if ("tower" in item || "pile" in item)
66
+ problems.push(`${at}: ${ask} reads shapes, numbers and + − × = < >, not a tower or a pile`);
67
+ else if (isPiece(item) && item.label !== undefined)
68
+ problems.push(`${at}: a shape marked ${item.label} shows a number; the rows tell it`);
69
+ else if ("number" in item && !/^\d+$/.test(item.number))
70
+ problems.push(`${at}: ${item.number} is not a whole number to read`);
71
+ });
72
+ if (problems.length > before || asks.length !== 1)
73
+ return;
74
+ if (i === asks[0][0]) {
75
+ const side = askedSide(row.items);
76
+ if (ask === "value" && !(side?.length === 1 && isPiece(side[0])))
77
+ problems.push(`rows[${i}]: value asks the number of one shape, as "triangle = ?"`);
78
+ if (ask === "row-value" &&
79
+ !(side && side.length >= 3 && sideOk(side, ["+"]) && side.some(isPiece)))
80
+ problems.push(`rows[${i}]: row-value asks the total of shapes joined by +, as "circle + triangle = ?"`);
81
+ return;
82
+ }
83
+ const marks = row.items.flatMap((item, k) => "op" in item && relations.includes(item.op) ? [k] : []);
84
+ const at = marks[0];
85
+ if (marks.length !== 1 ||
86
+ !sideOk(row.items.slice(0, at), joins) ||
87
+ !sideOk(row.items.slice(at + 1), joins))
88
+ problems.push(`rows[${i}]: must be an equation or a comparison: shapes and whole numbers joined by + − ×, and one =, < or >`);
89
+ });
90
+ return problems;
91
+ }
92
+ /**
93
+ * search tries the numbers of the kinds of shape, one kind after another,
94
+ * and gives each answer the rows allow, and every number each kind takes.
95
+ */
96
+ function search(scene, kinds, asked, answerOf) {
97
+ const rows = scene.rows;
98
+ const numbers = rows.flatMap((r) => r.items.flatMap((item) => ("number" in item ? [Number(item.number)] : [])));
99
+ const top = Math.min(most, Math.max(least, ...numbers));
100
+ const index = new Map(kinds.map((p, i) => [keyOf(p), i]));
101
+ // Each row is tried once the last of its kinds has a number: at that depth.
102
+ const due = kinds.map(() => []);
103
+ rows.forEach((row, i) => {
104
+ if (i === asked)
105
+ return;
106
+ const last = Math.max(0, ...row.items.filter(isPiece).map((p) => index.get(keyOf(p))));
107
+ due[last].push(i);
108
+ });
109
+ const out = { answers: new Set(), seen: kinds.map(() => new Set()), stopped: false };
110
+ const values = [];
111
+ let left = tries;
112
+ const holds = (d) => {
113
+ if (due[d].length === 0)
114
+ return true;
115
+ const facts = {
116
+ values: kinds
117
+ .slice(0, d + 1)
118
+ .map((p, i) => ({ shape: p.shape, color: p.color, value: values[i] })),
119
+ };
120
+ return checkScene({ kind: "shapes", rows: due[d].map((i) => rows[i]), facts }).length === 0;
121
+ };
122
+ const go = (d) => {
123
+ if (d === kinds.length) {
124
+ out.answers.add(answerOf(values));
125
+ for (const [i, v] of values.entries())
126
+ out.seen[i].add(v);
127
+ return;
128
+ }
129
+ for (let v = 1; v <= top && !out.stopped; v++) {
130
+ if (--left < 0) {
131
+ out.stopped = true;
132
+ return;
133
+ }
134
+ values[d] = v;
135
+ if (holds(d))
136
+ go(d + 1);
137
+ }
138
+ values.length = d;
139
+ };
140
+ go(0);
141
+ return out;
142
+ }
143
+ function solve(scene, question) {
144
+ if (!scene)
145
+ return noScene;
146
+ const rows = scene.rows;
147
+ const reads = rows.map((_, i) => `rows[${i}]`);
148
+ const asked = asksOf(scene)[0][0];
149
+ const kinds = [];
150
+ for (const row of rows)
151
+ for (const item of row.items)
152
+ if (isPiece(item) && !kinds.some((k) => keyOf(k) === keyOf(item)))
153
+ kinds.push(item);
154
+ const at = (p) => kinds.findIndex((k) => keyOf(k) === keyOf(p));
155
+ const side = askedSide(rows[asked].items);
156
+ const answerOf = (values) => side.reduce((sum, item) => sum + (isPiece(item) ? values[at(item)] : "number" in item ? Number(item.number) : 0), 0);
157
+ const all = search(scene, kinds, asked, answerOf);
158
+ if (all.stopped)
159
+ return unsolved(`the search stopped after ${tries} tries`, reads);
160
+ const told = [...all.answers].sort((a, b) => a - b);
161
+ if (told.length === 0)
162
+ return unsolved("no whole numbers for the shapes make every row hold", reads);
163
+ if (told.length > 1)
164
+ return unsolved(`the rows allow each of ${told.join(", ")} for ${question.ask === "value" ? "the shape" : "the row"} asked`, reads, told.map((number) => ({ number })));
165
+ const loose = all.seen.findIndex((s) => s.size > 1);
166
+ if (loose >= 0)
167
+ return unsolved(`the rows leave the ${nameOf(kinds[loose])} more than one number`, reads);
168
+ return solved({ number: told[0] }, reads);
169
+ }
170
+ /** The facts of a scene of shapes are its rows, or its table. */
171
+ function facts(scene) {
172
+ return scene.rows ? scene.rows.map((_, i) => `rows[${i}]`) : ["table"];
173
+ }
174
+ /** shapesSolver finds the numbers shapes stand for: the questions of ShapesQuestion. */
175
+ export const shapesSolver = {
176
+ name: "shapes",
177
+ kinds: ["shapes"],
178
+ questions: Object.keys(members),
179
+ check,
180
+ facts,
181
+ solve,
182
+ };
@@ -0,0 +1,40 @@
1
+ import { type TargetScene } from "../kinds/target.ts";
2
+ import { type Solver } from "./proof.ts";
3
+ /** A board of a scene of targets, by its index in targets or by its label. */
4
+ export type BoardRef = number | string;
5
+ /**
6
+ * The questions about boards that darts hit: rings from the centre out,
7
+ * zones with a bull's-eye or not, or pegs that hoops land on. Each dart
8
+ * scores the score of the ring or the zone it hits, each hoop the score of
9
+ * its peg, as the kind adds them for a board's total. A board is the one
10
+ * board, or the one board names, by its index or its label.
11
+ *
12
+ * - `score`: what the darts or hoops of a board score, a number. A board
13
+ * that shows its total already, has nothing on it, or has a dart in a ring
14
+ * whose score is asked, is refused.
15
+ * - `impossible-total`: which of the totals the task's options offer, each
16
+ * a number, the darts the statement names, givens.darts, cannot score all
17
+ * together on a board of rings or zones, every dart hitting it: a number.
18
+ * Every way the darts can land is tried, at most 4 darts. A board with a
19
+ * dart drawn shows what the task does not need.
20
+ * - `which-holder-most`, `which-holder-fewest`: the board whose darts or
21
+ * hoops score the most or the fewest, named by its label as a text; there
22
+ * are two boards or more, each with a label of its own. A tie has no answer.
23
+ *
24
+ * Only impossible-total takes givens.
25
+ */
26
+ export type TargetQuestion = {
27
+ ask: "score";
28
+ board?: BoardRef;
29
+ } | {
30
+ ask: "impossible-total";
31
+ board?: BoardRef;
32
+ } | {
33
+ ask: "which-holder-most" | "which-holder-fewest";
34
+ };
35
+ export type TargetGivens = {
36
+ /** How many darts the statement says hit the board, for impossible-total. */
37
+ darts?: number;
38
+ };
39
+ /** targetSolver scores the darts of a scene of targets: the questions of TargetQuestion. */
40
+ export declare const targetSolver: Solver<TargetScene, TargetQuestion, TargetGivens>;