@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.
- package/README.md +1 -1
- package/lib/index.d.ts +45 -1
- package/lib/index.js +34 -3
- package/lib/kinds/balance.js +2 -3
- package/lib/kinds/calendar.d.ts +5 -1
- package/lib/kinds/calendar.js +33 -11
- package/lib/kinds/chart.js +2 -4
- package/lib/kinds/clock.d.ts +5 -0
- package/lib/kinds/clock.js +45 -4
- package/lib/kinds/coins.js +3 -2
- package/lib/kinds/column.js +2 -3
- package/lib/kinds/commands.d.ts +8 -6
- package/lib/kinds/commands.js +57 -15
- package/lib/kinds/containers.js +2 -1
- package/lib/kinds/countfig.d.ts +2 -0
- package/lib/kinds/countfig.js +6 -1
- package/lib/kinds/cubes.d.ts +29 -5
- package/lib/kinds/cubes.js +82 -34
- package/lib/kinds/die.d.ts +8 -0
- package/lib/kinds/die.js +75 -14
- package/lib/kinds/domino.js +3 -3
- package/lib/kinds/dotgrid.d.ts +3 -0
- package/lib/kinds/dotgrid.js +6 -4
- package/lib/kinds/figure.js +5 -4
- package/lib/kinds/fold-parts.d.ts +0 -2
- package/lib/kinds/fold-parts.js +0 -4
- package/lib/kinds/fold.d.ts +20 -1
- package/lib/kinds/fold.js +15 -6
- package/lib/kinds/fraction.js +3 -2
- package/lib/kinds/gauges.js +2 -1
- package/lib/kinds/gears.d.ts +21 -2
- package/lib/kinds/gears.js +113 -62
- package/lib/kinds/grid.d.ts +5 -0
- package/lib/kinds/grid.js +30 -2
- package/lib/kinds/groups.d.ts +3 -1
- package/lib/kinds/groups.js +142 -28
- package/lib/kinds/lanes.js +3 -2
- package/lib/kinds/layers.d.ts +6 -0
- package/lib/kinds/layers.js +23 -4
- package/lib/kinds/line.d.ts +4 -0
- package/lib/kinds/line.js +117 -32
- package/lib/kinds/matchsticks.d.ts +4 -0
- package/lib/kinds/matchsticks.js +5 -3
- package/lib/kinds/net.js +4 -3
- package/lib/kinds/overlay.d.ts +23 -1
- package/lib/kinds/overlay.js +7 -3
- package/lib/kinds/pieces.js +3 -2
- package/lib/kinds/plane-parts.d.ts +0 -2
- package/lib/kinds/plane-parts.js +0 -6
- package/lib/kinds/plane.js +6 -6
- package/lib/kinds/ropes.js +1 -3
- package/lib/kinds/shapes.js +5 -7
- package/lib/kinds/solids.js +3 -2
- package/lib/kinds/tangram.js +2 -4
- package/lib/kinds/target.js +4 -6
- package/lib/kinds/tiles.js +5 -4
- package/lib/kinds/venn.js +3 -5
- package/lib/noto.js +1 -0
- package/lib/objects.js +2 -1
- package/lib/read.d.ts +2 -2
- package/lib/read.js +3 -3
- package/lib/solve/balance.d.ts +11 -0
- package/lib/solve/balance.js +116 -1
- package/lib/solve/calendar.d.ts +46 -0
- package/lib/solve/calendar.js +206 -0
- package/lib/solve/choice-scenes.d.ts +20 -0
- package/lib/solve/choice-scenes.js +51 -0
- package/lib/solve/clock.d.ts +54 -0
- package/lib/solve/clock.js +240 -0
- package/lib/solve/coins.d.ts +71 -0
- package/lib/solve/coins.js +222 -0
- package/lib/solve/commands.d.ts +36 -0
- package/lib/solve/commands.js +210 -0
- package/lib/solve/countfig.d.ts +41 -0
- package/lib/solve/countfig.js +199 -0
- package/lib/solve/cubes.d.ts +49 -0
- package/lib/solve/cubes.js +255 -0
- package/lib/solve/die.d.ts +39 -0
- package/lib/solve/die.js +93 -0
- package/lib/solve/domino.d.ts +57 -0
- package/lib/solve/domino.js +263 -0
- package/lib/solve/enumerate.d.ts +75 -0
- package/lib/solve/enumerate.js +323 -0
- package/lib/solve/figure.d.ts +26 -0
- package/lib/solve/figure.js +88 -0
- package/lib/solve/fold.d.ts +22 -0
- package/lib/solve/fold.js +68 -0
- package/lib/solve/gaps.d.ts +65 -0
- package/lib/solve/gaps.js +292 -0
- package/lib/solve/gears.d.ts +33 -0
- package/lib/solve/gears.js +130 -0
- package/lib/solve/grid.d.ts +35 -0
- package/lib/solve/grid.js +194 -0
- package/lib/solve/groups.d.ts +93 -0
- package/lib/solve/groups.js +346 -0
- package/lib/solve/index.d.ts +60 -0
- package/lib/solve/index.js +194 -0
- package/lib/solve/layers.d.ts +30 -0
- package/lib/solve/layers.js +72 -0
- package/lib/solve/magic.d.ts +32 -0
- package/lib/solve/magic.js +216 -0
- package/lib/solve/matchsticks.d.ts +37 -0
- package/lib/solve/matchsticks.js +214 -0
- package/lib/solve/order.d.ts +94 -0
- package/lib/solve/order.js +419 -0
- package/lib/solve/overlay.d.ts +22 -0
- package/lib/solve/overlay.js +56 -0
- package/lib/solve/parity.d.ts +22 -0
- package/lib/solve/parity.js +27 -0
- package/lib/solve/pattern.d.ts +62 -0
- package/lib/solve/pattern.js +533 -0
- package/lib/solve/pieces.d.ts +57 -0
- package/lib/solve/pieces.js +309 -0
- package/lib/solve/pigeonhole.d.ts +38 -0
- package/lib/solve/pigeonhole.js +95 -0
- package/lib/solve/proof.d.ts +115 -0
- package/lib/solve/proof.js +105 -0
- package/lib/solve/pyramid.d.ts +28 -0
- package/lib/solve/pyramid.js +161 -0
- package/lib/solve/question.d.ts +25 -0
- package/lib/solve/question.js +75 -0
- package/lib/solve/shapes.d.ts +30 -0
- package/lib/solve/shapes.js +182 -0
- package/lib/solve/target.d.ts +40 -0
- package/lib/solve/target.js +190 -0
- package/lib/style.d.ts +1 -1
- package/lib/style.js +1 -1
- package/lib/words.d.ts +15 -0
- package/lib/words.js +39 -0
- package/package.json +1 -1
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { int, isObj, list } from "../read.js";
|
|
2
|
+
import { check as checkScene, SceneError } from "../render.js";
|
|
3
|
+
import { balanceSolver } from "./balance.js";
|
|
4
|
+
import { calendarSolver } from "./calendar.js";
|
|
5
|
+
import { clockSolver } from "./clock.js";
|
|
6
|
+
import { coinsSolver } from "./coins.js";
|
|
7
|
+
import { commandsSolver } from "./commands.js";
|
|
8
|
+
import { countfigSolver } from "./countfig.js";
|
|
9
|
+
import { cubesSolver } from "./cubes.js";
|
|
10
|
+
import { dieSolver } from "./die.js";
|
|
11
|
+
import { dominoSolver } from "./domino.js";
|
|
12
|
+
import { enumerateSolver } from "./enumerate.js";
|
|
13
|
+
import { figureSolver } from "./figure.js";
|
|
14
|
+
import { foldSolver } from "./fold.js";
|
|
15
|
+
import { gapsSolver } from "./gaps.js";
|
|
16
|
+
import { gearsSolver } from "./gears.js";
|
|
17
|
+
import { gridSolver } from "./grid.js";
|
|
18
|
+
import { groupsSolver } from "./groups.js";
|
|
19
|
+
import { layersSolver } from "./layers.js";
|
|
20
|
+
import { magicSolver } from "./magic.js";
|
|
21
|
+
import { matchsticksSolver } from "./matchsticks.js";
|
|
22
|
+
import { orderSolver } from "./order.js";
|
|
23
|
+
import { overlaySolver } from "./overlay.js";
|
|
24
|
+
import { paritySolver } from "./parity.js";
|
|
25
|
+
import { patternSolver } from "./pattern.js";
|
|
26
|
+
import { piecesSolver } from "./pieces.js";
|
|
27
|
+
import { pigeonholeSolver } from "./pigeonhole.js";
|
|
28
|
+
import { checkOption, letters, nothing, provesKey, unread, } from "./proof.js";
|
|
29
|
+
import { pyramidSolver } from "./pyramid.js";
|
|
30
|
+
import { shapesSolver } from "./shapes.js";
|
|
31
|
+
import { targetSolver } from "./target.js";
|
|
32
|
+
/** The closed list of the topics' solvers, each by its name. */
|
|
33
|
+
export const solvers = [
|
|
34
|
+
balanceSolver,
|
|
35
|
+
groupsSolver,
|
|
36
|
+
coinsSolver,
|
|
37
|
+
countfigSolver,
|
|
38
|
+
gridSolver,
|
|
39
|
+
commandsSolver,
|
|
40
|
+
piecesSolver,
|
|
41
|
+
orderSolver,
|
|
42
|
+
enumerateSolver,
|
|
43
|
+
pigeonholeSolver,
|
|
44
|
+
calendarSolver,
|
|
45
|
+
clockSolver,
|
|
46
|
+
shapesSolver,
|
|
47
|
+
pyramidSolver,
|
|
48
|
+
magicSolver,
|
|
49
|
+
targetSolver,
|
|
50
|
+
dominoSolver,
|
|
51
|
+
dieSolver,
|
|
52
|
+
patternSolver,
|
|
53
|
+
gapsSolver,
|
|
54
|
+
gearsSolver,
|
|
55
|
+
paritySolver,
|
|
56
|
+
cubesSolver,
|
|
57
|
+
figureSolver,
|
|
58
|
+
foldSolver,
|
|
59
|
+
overlaySolver,
|
|
60
|
+
layersSolver,
|
|
61
|
+
matchsticksSolver,
|
|
62
|
+
];
|
|
63
|
+
/** within puts a problem of a scene at the path where the scene stands. */
|
|
64
|
+
function within(path, problem) {
|
|
65
|
+
return problem.startsWith("scene") ? `${path}${problem.slice(5)}` : `${path}.${problem}`;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* offeredOf reads what a task offers: every option an Option; the choices, if any, two or more
|
|
69
|
+
* scenes that each pass their check, none a scene of choices, as the writer draws them together in
|
|
70
|
+
* one; and the letter options lettered by their places, one for each choice.
|
|
71
|
+
*/
|
|
72
|
+
function offeredOf(options, choices, problems) {
|
|
73
|
+
options.forEach((o, i) => {
|
|
74
|
+
checkOption(o, `options[${i}]`, problems);
|
|
75
|
+
});
|
|
76
|
+
const scenes = choices === undefined ? [] : list(choices, "choices", problems, 2, letters.length);
|
|
77
|
+
scenes.forEach((c, i) => {
|
|
78
|
+
if (isObj(c) && c.kind === "choices")
|
|
79
|
+
problems.push(`choices[${i}]: a picture option is a scene of another kind than choices`);
|
|
80
|
+
else
|
|
81
|
+
for (const p of checkScene(c))
|
|
82
|
+
problems.push(within(`choices[${i}]`, p));
|
|
83
|
+
});
|
|
84
|
+
if (choices !== undefined) {
|
|
85
|
+
const lettered = options.flatMap((o, i) => (isObj(o) && o.letter !== undefined ? [i] : []));
|
|
86
|
+
lettered.forEach((at, k) => {
|
|
87
|
+
const want = letters[k];
|
|
88
|
+
if (want !== undefined && options[at].letter !== want)
|
|
89
|
+
problems.push(`options[${at}].letter: must be ${want}; a picture option is lettered by its place among the letter options, A first`);
|
|
90
|
+
});
|
|
91
|
+
if (lettered.length !== scenes.length)
|
|
92
|
+
problems.push(`choices: ${scenes.length} scenes for ${lettered.length} letter options; each picture option is a letter option with its scene`);
|
|
93
|
+
}
|
|
94
|
+
return { options: options, choices: scenes };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* problemsOf runs every check of a task but its key: the solver, the scene as render checks it and
|
|
98
|
+
* of a kind the solver reads, what the task offers, and then, once all of those pass, the
|
|
99
|
+
* solver's own check of the question and the givens, which refuses a task with no scene unless it
|
|
100
|
+
* answers one.
|
|
101
|
+
*/
|
|
102
|
+
function problemsOf(name, scene, question, givens, options, choices) {
|
|
103
|
+
const solver = solvers.find((s) => s.name === name);
|
|
104
|
+
const problems = [];
|
|
105
|
+
if (!solver) {
|
|
106
|
+
problems.push(`solver: must be one of ${solvers.map((s) => s.name).join(", ")}`);
|
|
107
|
+
return { offered: nothing, problems };
|
|
108
|
+
}
|
|
109
|
+
if (scene !== undefined) {
|
|
110
|
+
if (isObj(scene) && !solver.kinds.includes(scene.kind))
|
|
111
|
+
problems.push(`scene.kind: the solver ${name} reads ${solver.kinds.join(", ") || "no scene"}`);
|
|
112
|
+
else
|
|
113
|
+
problems.push(...checkScene(scene));
|
|
114
|
+
}
|
|
115
|
+
const offered = offeredOf(options, choices, problems);
|
|
116
|
+
if (problems.length === 0)
|
|
117
|
+
problems.push(...solver.check(question, givens, scene, offered));
|
|
118
|
+
return { solver, offered, problems };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* prove answers a question about a scene by the solver of that name: the
|
|
122
|
+
* scene, if the task has one, is checked as render checks it and must be of
|
|
123
|
+
* a kind the solver reads, then what the task offers, and only then the
|
|
124
|
+
* question and the givens, by the solver, which refuses a task with no scene
|
|
125
|
+
* unless its question needs none. offered is what the task offers, its
|
|
126
|
+
* options and the scenes of its picture options, which a question that
|
|
127
|
+
* answers among them reads; left out, the task offers nothing. A task that
|
|
128
|
+
* fails a check is refused: prove throws SceneError with every problem, each
|
|
129
|
+
* with its path — `scales[1].tilt: …`, `question.of: …`, `choices[0]: …`.
|
|
130
|
+
* The solver's check runs only on a scene, options and choices that passed
|
|
131
|
+
* their own checks, so a task with a broken scene is told the scene's
|
|
132
|
+
* problems first.
|
|
133
|
+
*/
|
|
134
|
+
export function prove(name, scene, question, givens, offered) {
|
|
135
|
+
const checked = problemsOf(name, scene, question, givens, offered?.options ?? [], offered?.choices);
|
|
136
|
+
if (checked.problems.length > 0)
|
|
137
|
+
throw new SceneError(checked.problems);
|
|
138
|
+
return checked.solver.solve(scene, question, givens, checked.offered);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* turned is a task with its options turned by a number of places, as a learner may be shown them:
|
|
142
|
+
* the option at place i goes to place i - by, the key with its option, and each picture option's
|
|
143
|
+
* scene with it, lettered anew by its place among the letter options.
|
|
144
|
+
*/
|
|
145
|
+
export function turned(task, by) {
|
|
146
|
+
const n = task.options.length;
|
|
147
|
+
const options = task.options.map((_, i) => task.options[(i + by) % n]);
|
|
148
|
+
const out = { ...task, options, key: (((task.key - by) % n) + n) % n };
|
|
149
|
+
if (task.choices === undefined)
|
|
150
|
+
return out;
|
|
151
|
+
const letterOf = (o) => (isObj(o) && typeof o.letter === "string" ? o.letter : "");
|
|
152
|
+
const scenes = options.flatMap((o) => letterOf(o) === "" ? [] : [task.choices[letters.indexOf(letterOf(o))]]);
|
|
153
|
+
let k = 0;
|
|
154
|
+
const relettered = options.map((o) => (letterOf(o) === "" ? o : { letter: letters[k++] }));
|
|
155
|
+
return { ...out, options: relettered, choices: scenes };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* checkTask lists what keeps a task's key from being proved; none means the
|
|
159
|
+
* key is proved. The scene, the question, the givens and what the task
|
|
160
|
+
* offers pass their checks, as prove has them; there are two options or
|
|
161
|
+
* more, each an Option; every fact of the scene is one the solver read, for a
|
|
162
|
+
* fact the task does not need distracts or lies; and exactly one option
|
|
163
|
+
* matches the proof, the key — for an option "it cannot be told", the
|
|
164
|
+
* solver proves no answer and names two candidates or more. The key holds
|
|
165
|
+
* whatever the order of the options: the gate proves the task again with its
|
|
166
|
+
* options turned by every number of places, its picture options lettered
|
|
167
|
+
* anew, and refuses a turn in which the key's option no longer matches alone.
|
|
168
|
+
*/
|
|
169
|
+
export function checkTask(task) {
|
|
170
|
+
const listed = [];
|
|
171
|
+
const options = list(task.options, "options", listed, 2);
|
|
172
|
+
const checked = problemsOf(task.solver, task.scene, task.question, task.givens, options, task.choices);
|
|
173
|
+
const problems = [...checked.problems, ...listed];
|
|
174
|
+
int(task.key, "key", problems, 0, Math.max(0, options.length - 1));
|
|
175
|
+
if (problems.length > 0)
|
|
176
|
+
return problems;
|
|
177
|
+
const solver = checked.solver;
|
|
178
|
+
const proof = solver.solve(task.scene, task.question, task.givens, checked.offered);
|
|
179
|
+
const facts = task.scene === undefined ? [] : solver.facts(task.scene);
|
|
180
|
+
for (const fact of unread(facts, proof.reads))
|
|
181
|
+
problems.push(`scene: ${fact} is a fact the solver did not read; the task does not need it`);
|
|
182
|
+
problems.push(...provesKey(proof, checked.offered.options, task.key));
|
|
183
|
+
if (problems.length > 0)
|
|
184
|
+
return problems;
|
|
185
|
+
for (let by = 1; by < options.length; by++) {
|
|
186
|
+
const t = turned(task, by);
|
|
187
|
+
const offered = { options: t.options, choices: t.choices ?? [] };
|
|
188
|
+
const again = solver.solve(task.scene, task.question, task.givens, offered);
|
|
189
|
+
const wrong = provesKey(again, offered.options, t.key);
|
|
190
|
+
if (wrong.length > 0)
|
|
191
|
+
problems.push(`with the options turned by ${by}: ${wrong.join("; ")}`);
|
|
192
|
+
}
|
|
193
|
+
return problems;
|
|
194
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type LayersScene } from "../kinds/layers.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The question about a pile of sheets put down one after another, each
|
|
5
|
+
* covering what lies under it.
|
|
6
|
+
*
|
|
7
|
+
* - `layer-order`: the sheet put down at a place of the pile, the givens'
|
|
8
|
+
* place: "first", at the bottom; "last", on top; or a number counted from
|
|
9
|
+
* the first, `{"place": 2}`. The answer is the sheet's label as a text, or,
|
|
10
|
+
* where the sheets have none, the name of its colour, `{"text": "red"}`;
|
|
11
|
+
* so unlabelled sheets each need a colour of their own.
|
|
12
|
+
*
|
|
13
|
+
* The picture shows, for every two sheets that overlap, which lies on the
|
|
14
|
+
* other, and nothing of two that do not: the order is any that keeps every
|
|
15
|
+
* sheet above each sheet it is seen lying on. A sheet with a sheets that
|
|
16
|
+
* must lie under it and b that must lie on it can be put down anywhere from
|
|
17
|
+
* place a + 1 to place n − b, so the answer is proved when one sheet alone
|
|
18
|
+
* can be at the place, and otherwise the candidates are every sheet that can.
|
|
19
|
+
* Two sheets overlap where the kind sees them both, at the samples it looks
|
|
20
|
+
* at the picture by. Every sheet is read.
|
|
21
|
+
*/
|
|
22
|
+
export type LayersQuestion = {
|
|
23
|
+
ask: "layer-order";
|
|
24
|
+
};
|
|
25
|
+
/** The givens of layer-order: the place in the pile asked about. */
|
|
26
|
+
export type LayersGivens = {
|
|
27
|
+
place: "first" | "last" | number;
|
|
28
|
+
};
|
|
29
|
+
/** layersSolver reads the order of a pile of sheets: the question of LayersQuestion. */
|
|
30
|
+
export declare const layersSolver: Solver<LayersScene, LayersQuestion, LayersGivens>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { overlaps } from "../kinds/layers.js";
|
|
2
|
+
import { int, isObj, only } from "../read.js";
|
|
3
|
+
import { colors } from "../style.js";
|
|
4
|
+
import { noScene, solved, unsolved } from "./proof.js";
|
|
5
|
+
import { askOf, sceneless } from "./question.js";
|
|
6
|
+
const members = { "layer-order": [] };
|
|
7
|
+
/** spans gives each sheet the first and the last place in the pile the picture allows it. */
|
|
8
|
+
function spans(sheets) {
|
|
9
|
+
const touch = overlaps(sheets);
|
|
10
|
+
const n = sheets.length;
|
|
11
|
+
// below[i][j]: sheet j lies under sheet i, seen so or through sheets between.
|
|
12
|
+
const below = sheets.map((_, i) => sheets.map((_, j) => j < i && touch[i][j]));
|
|
13
|
+
for (let k = 0; k < n; k++)
|
|
14
|
+
for (let i = 0; i < n; i++)
|
|
15
|
+
for (let j = 0; j < n; j++)
|
|
16
|
+
if (below[i][k] && below[k][j])
|
|
17
|
+
below[i][j] = true;
|
|
18
|
+
return sheets.map((_, i) => {
|
|
19
|
+
const under = below[i].filter(Boolean).length;
|
|
20
|
+
const over = below.filter((row) => row[i]).length;
|
|
21
|
+
return [under + 1, n - over];
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const nameOf = (sheet) => ({ text: sheet.label ?? colors[sheet.color].word });
|
|
25
|
+
function check(question, givens, scene) {
|
|
26
|
+
const problems = [];
|
|
27
|
+
const read = askOf(question, members, problems);
|
|
28
|
+
if (read && !scene)
|
|
29
|
+
sceneless(read.ask, "a scene of layers, the pile", problems);
|
|
30
|
+
if (!isObj(givens)) {
|
|
31
|
+
problems.push('givens: must give the place asked, as {"place": "first"}');
|
|
32
|
+
return problems;
|
|
33
|
+
}
|
|
34
|
+
only(givens, ["place"], "givens", problems, "the givens of layer-order");
|
|
35
|
+
const n = scene?.sheets.length ?? 8;
|
|
36
|
+
if (givens.place !== "first" && givens.place !== "last") {
|
|
37
|
+
if (typeof givens.place === "string" || givens.place === undefined)
|
|
38
|
+
problems.push('givens.place: must be "first", "last" or a number from 1, the first');
|
|
39
|
+
else
|
|
40
|
+
int(givens.place, "givens.place", problems, 1, n);
|
|
41
|
+
}
|
|
42
|
+
if (scene?.sheets.every((s) => s.label === undefined)) {
|
|
43
|
+
const seen = new Map();
|
|
44
|
+
scene.sheets.forEach((s, i) => {
|
|
45
|
+
const other = seen.get(s.color);
|
|
46
|
+
if (other !== undefined)
|
|
47
|
+
problems.push(`sheets[${i}]: is ${colors[s.color].word} as sheets[${other}] is; the answer names a sheet by its colour, so give each its own, or label them`);
|
|
48
|
+
seen.set(s.color, i);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return problems;
|
|
52
|
+
}
|
|
53
|
+
function solve(scene, _question, givens) {
|
|
54
|
+
if (!scene)
|
|
55
|
+
return noScene;
|
|
56
|
+
const n = scene.sheets.length;
|
|
57
|
+
const place = givens.place === "first" ? 1 : givens.place === "last" ? n : givens.place;
|
|
58
|
+
const reads = scene.sheets.map((_, i) => `sheets[${i}]`);
|
|
59
|
+
const can = spans(scene.sheets).flatMap(([a, b], i) => (a <= place && place <= b ? [i] : []));
|
|
60
|
+
if (can.length === 1)
|
|
61
|
+
return solved(nameOf(scene.sheets[can[0]]), reads);
|
|
62
|
+
return unsolved(`the overlaps leave place ${place} to more than one sheet`, reads, can.map((i) => nameOf(scene.sheets[i])));
|
|
63
|
+
}
|
|
64
|
+
/** layersSolver reads the order of a pile of sheets: the question of LayersQuestion. */
|
|
65
|
+
export const layersSolver = {
|
|
66
|
+
name: "layers",
|
|
67
|
+
kinds: ["layers"],
|
|
68
|
+
questions: Object.keys(members),
|
|
69
|
+
check,
|
|
70
|
+
facts: (scene) => scene.sheets.map((_, i) => `sheets[${i}]`),
|
|
71
|
+
solve,
|
|
72
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type MagicScene } from "../kinds/magic.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The question about a magic figure, whose lines each add up to a sum.
|
|
5
|
+
*
|
|
6
|
+
* - `slot`: the number of the asked place, the one {"ask": true}, a number.
|
|
7
|
+
*
|
|
8
|
+
* A line's sum is its tag in sums; where sums holds null, or the figure has
|
|
9
|
+
* no sums, the statement gives it: givens.sum, the sum of every line with no
|
|
10
|
+
* tag. With neither, every line adds up to one sum, which the figure does
|
|
11
|
+
* not show. A place that shows a number gives it; an empty place, a text or
|
|
12
|
+
* a thing takes a number, and places with the same text or the same thing
|
|
13
|
+
* take the same one. With a pool, the open places take cards of the pool,
|
|
14
|
+
* each card once, as the kind's check of facts has them; without one, any
|
|
15
|
+
* whole number from 0, so every open place must lie on a line whose sum is
|
|
16
|
+
* known — given, or shown by a line of numbers alone — and is never larger
|
|
17
|
+
* than it. The solver tries every way to fill the open places, place by
|
|
18
|
+
* place, pruning a line that passes its sum, and keeps each filling under
|
|
19
|
+
* which every line adds up, which passes the kind's own check of facts too;
|
|
20
|
+
* the search stops after 1000000 tries. The answer is proved when every
|
|
21
|
+
* filling gives the asked place one number; when they give it more, those
|
|
22
|
+
* are the candidates. It reads the places, the sums and the pool.
|
|
23
|
+
*/
|
|
24
|
+
export type MagicQuestion = {
|
|
25
|
+
ask: "slot";
|
|
26
|
+
};
|
|
27
|
+
export type MagicGivens = {
|
|
28
|
+
/** The sum of every line with no tag, as the statement gives it. */
|
|
29
|
+
sum?: number;
|
|
30
|
+
};
|
|
31
|
+
/** magicSolver fills a magic figure: the question of MagicQuestion. */
|
|
32
|
+
export declare const magicSolver: Solver<MagicScene, MagicQuestion, MagicGivens>;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { magicLines } from "../kinds/magic.js";
|
|
2
|
+
import { contentOf, numberOf } from "../kinds/magic-parts.js";
|
|
3
|
+
import { int, isObj, only } from "../read.js";
|
|
4
|
+
import { check as checkScene } from "../render.js";
|
|
5
|
+
import { noScene, solved, unsolved } from "./proof.js";
|
|
6
|
+
import { askOf, sceneless } from "./question.js";
|
|
7
|
+
const members = { slot: [] };
|
|
8
|
+
/** The search stops after so many tries. */
|
|
9
|
+
const tries = 1000000;
|
|
10
|
+
const asksOf = (scene) => scene.slots.flatMap((e, i) => (contentOf(e).ask ? [i] : []));
|
|
11
|
+
function tieOf(e) {
|
|
12
|
+
if (numberOf(e) !== undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const c = contentOf(e);
|
|
15
|
+
if (c.text !== undefined)
|
|
16
|
+
return `text ${c.text}`;
|
|
17
|
+
if (c.thing !== undefined)
|
|
18
|
+
return `thing ${JSON.stringify(c.thing)}`;
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* sumsOf is the sum each line must add up to: its tag, or the sum the
|
|
23
|
+
* statement gives; undefined for the one sum every line shares, unknown.
|
|
24
|
+
*/
|
|
25
|
+
function sumsOf(scene, givens) {
|
|
26
|
+
return magicLines(scene).map((_, i) => {
|
|
27
|
+
const tag = scene.sums?.[i];
|
|
28
|
+
return typeof tag === "number" ? tag : givens.sum;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/** shared is the one sum of the lines, when no line has a sum of its own and a line is all numbers. */
|
|
32
|
+
function shared(scene, sums) {
|
|
33
|
+
if (sums.some((s) => s !== undefined))
|
|
34
|
+
return undefined;
|
|
35
|
+
for (const line of magicLines(scene)) {
|
|
36
|
+
const values = line.map((j) => numberOf(scene.slots[j]));
|
|
37
|
+
if (values.every((v) => v !== undefined))
|
|
38
|
+
return values.reduce((a, b) => a + b, 0);
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
function check(question, givens, scene) {
|
|
43
|
+
const problems = [];
|
|
44
|
+
if (givens !== undefined) {
|
|
45
|
+
if (!isObj(givens))
|
|
46
|
+
problems.push("givens: must be an object");
|
|
47
|
+
else {
|
|
48
|
+
only(givens, ["sum"], "givens", problems, "the givens of a magic figure");
|
|
49
|
+
if (givens.sum !== undefined)
|
|
50
|
+
int(givens.sum, "givens.sum", problems, 0, 9999);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const read = askOf(question, members, problems);
|
|
54
|
+
if (!read)
|
|
55
|
+
return problems;
|
|
56
|
+
if (!scene)
|
|
57
|
+
sceneless(read.ask, "a scene of magic, the figure", problems);
|
|
58
|
+
if (!scene || problems.length > 0)
|
|
59
|
+
return problems;
|
|
60
|
+
const asks = asksOf(scene).length;
|
|
61
|
+
if (asks !== 1)
|
|
62
|
+
problems.push(`slots: slot needs exactly one {"ask": true}, the place it asks; the figure has ${asks}`);
|
|
63
|
+
const lines = magicLines(scene);
|
|
64
|
+
const sum = givens?.sum;
|
|
65
|
+
scene.sums?.forEach((tag, i) => {
|
|
66
|
+
if (isObj(tag))
|
|
67
|
+
problems.push(`sums[${i}]: asks the sum of a line, and slot asks a place`);
|
|
68
|
+
else if (tag === null && sum === undefined)
|
|
69
|
+
problems.push(`sums[${i}]: shows no sum; givens.sum gives the sum the statement names`);
|
|
70
|
+
});
|
|
71
|
+
if (sum !== undefined && scene.sums?.every((tag) => typeof tag === "number"))
|
|
72
|
+
problems.push("givens.sum: every line shows its own sum");
|
|
73
|
+
scene.pool?.forEach((card, i) => {
|
|
74
|
+
if (typeof card !== "number")
|
|
75
|
+
problems.push(`pool[${i}]: ${card} is no number to place`);
|
|
76
|
+
});
|
|
77
|
+
if (problems.length > 0 || scene.pool)
|
|
78
|
+
return problems;
|
|
79
|
+
const sums = sumsOf(scene, { ...(sum !== undefined ? { sum } : {}) });
|
|
80
|
+
const one = shared(scene, sums);
|
|
81
|
+
scene.slots.forEach((e, i) => {
|
|
82
|
+
if (numberOf(e) !== undefined)
|
|
83
|
+
return;
|
|
84
|
+
const bounded = lines.some((line, l) => line.includes(i) && (sums[l] ?? one) !== undefined);
|
|
85
|
+
if (!bounded)
|
|
86
|
+
problems.push(`slots[${i}]: lies on no line whose sum is known, and there is no pool; nothing bounds it`);
|
|
87
|
+
});
|
|
88
|
+
return problems;
|
|
89
|
+
}
|
|
90
|
+
/** fillings tries every way to fill the open places and gives each number of the asked place with a filling. */
|
|
91
|
+
function fillings(scene, givens, asked) {
|
|
92
|
+
const lines = magicLines(scene);
|
|
93
|
+
const sums = sumsOf(scene, givens);
|
|
94
|
+
let one = shared(scene, sums);
|
|
95
|
+
const value = scene.slots.map((e) => numberOf(e) ?? 0);
|
|
96
|
+
const open = scene.slots.flatMap((e, i) => (numberOf(e) === undefined ? [i] : []));
|
|
97
|
+
const ties = scene.slots.map(tieOf);
|
|
98
|
+
const on = scene.slots.map((_, i) => lines.flatMap((line, l) => (line.includes(i) ? [l] : [])));
|
|
99
|
+
// What each line holds so far, and how many of its places are still open.
|
|
100
|
+
const total = lines.map((line) => line.reduce((s, j) => s + value[j], 0));
|
|
101
|
+
const empty = lines.map((line) => line.filter((j) => open.includes(j)).length);
|
|
102
|
+
const pool = scene.pool;
|
|
103
|
+
const used = pool?.map(() => false);
|
|
104
|
+
const bound = (i) => Math.min(...on[i].map((l) => sums[l] ?? one ?? Number.POSITIVE_INFINITY));
|
|
105
|
+
const tied = new Map();
|
|
106
|
+
const out = { answers: new Map(), stopped: false };
|
|
107
|
+
let left = tries;
|
|
108
|
+
const fits = (i) => on[i].every((l) => {
|
|
109
|
+
const want = sums[l] ?? one;
|
|
110
|
+
if (empty[l] === 0)
|
|
111
|
+
return want === undefined || total[l] === want;
|
|
112
|
+
return want === undefined || total[l] <= want;
|
|
113
|
+
});
|
|
114
|
+
const put = (i, v, sign) => {
|
|
115
|
+
value[i] = sign === 1 ? v : 0;
|
|
116
|
+
for (const l of on[i]) {
|
|
117
|
+
total[l] += sign * v;
|
|
118
|
+
empty[l] -= sign;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const go = (p) => {
|
|
122
|
+
if (p === open.length) {
|
|
123
|
+
const answer = value[asked];
|
|
124
|
+
if (!out.answers.has(answer))
|
|
125
|
+
out.answers.set(answer, [...value]);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const i = open[p];
|
|
129
|
+
const tie = ties[i];
|
|
130
|
+
const held = tie === undefined ? undefined : tied.get(tie);
|
|
131
|
+
const choices = pool
|
|
132
|
+
? pool.flatMap((card, c) => used[c] || pool.slice(0, c).some((d, b) => !used[b] && d === card) ? [] : [[card, c]])
|
|
133
|
+
: Array.from({ length: bound(i) + 1 }, (_, v) => [
|
|
134
|
+
v,
|
|
135
|
+
undefined,
|
|
136
|
+
]);
|
|
137
|
+
for (const [v, c] of choices) {
|
|
138
|
+
if (held && held[0] !== v)
|
|
139
|
+
continue;
|
|
140
|
+
if (--left < 0) {
|
|
141
|
+
out.stopped = true;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
put(i, v, 1);
|
|
145
|
+
if (c !== undefined)
|
|
146
|
+
used[c] = true;
|
|
147
|
+
if (tie !== undefined) {
|
|
148
|
+
if (held)
|
|
149
|
+
held[1]++;
|
|
150
|
+
else
|
|
151
|
+
tied.set(tie, [v, 1]);
|
|
152
|
+
}
|
|
153
|
+
// With one sum shared and unknown, the first line filled sets it.
|
|
154
|
+
const before = one;
|
|
155
|
+
if (one === undefined && sums.every((s) => s === undefined)) {
|
|
156
|
+
const full = on[i].find((l) => empty[l] === 0);
|
|
157
|
+
if (full !== undefined)
|
|
158
|
+
one = total[full];
|
|
159
|
+
}
|
|
160
|
+
if (fits(i))
|
|
161
|
+
go(p + 1);
|
|
162
|
+
one = before;
|
|
163
|
+
if (tie !== undefined) {
|
|
164
|
+
const h = tied.get(tie);
|
|
165
|
+
if (--h[1] === 0)
|
|
166
|
+
tied.delete(tie);
|
|
167
|
+
}
|
|
168
|
+
if (c !== undefined)
|
|
169
|
+
used[c] = false;
|
|
170
|
+
put(i, v, -1);
|
|
171
|
+
if (out.stopped)
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
go(0);
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
function solve(scene, _question, givens = {}) {
|
|
179
|
+
if (!scene)
|
|
180
|
+
return noScene;
|
|
181
|
+
const reads = [
|
|
182
|
+
...scene.slots.map((_, i) => `slots[${i}]`),
|
|
183
|
+
...(scene.sums ?? []).flatMap((tag, i) => (typeof tag === "number" ? [`sums[${i}]`] : [])),
|
|
184
|
+
...(scene.pool ? ["pool"] : []),
|
|
185
|
+
];
|
|
186
|
+
const found = fillings(scene, givens ?? {}, asksOf(scene)[0]);
|
|
187
|
+
if (found.stopped)
|
|
188
|
+
return unsolved(`the search stopped after ${tries} tries`, reads);
|
|
189
|
+
// Every filling found holds by the kind's own check of facts, as a check of the solver itself.
|
|
190
|
+
const told = [...found.answers]
|
|
191
|
+
.filter(([, values]) => checkScene({ ...scene, facts: { values } }).length === 0)
|
|
192
|
+
.map(([answer]) => answer)
|
|
193
|
+
.sort((a, b) => a - b);
|
|
194
|
+
if (told.length === 0)
|
|
195
|
+
return unsolved("no way to fill the open places makes every line add up", reads);
|
|
196
|
+
if (told.length > 1)
|
|
197
|
+
return unsolved(`the figure allows each of ${told.join(", ")} for the place asked`, reads, told.map((number) => ({ number })));
|
|
198
|
+
return solved({ number: told[0] }, reads);
|
|
199
|
+
}
|
|
200
|
+
/** The facts of a magic figure are its places that show a number, its sums shown, and its cards. */
|
|
201
|
+
function facts(scene) {
|
|
202
|
+
return [
|
|
203
|
+
...scene.slots.flatMap((e, i) => (numberOf(e) === undefined ? [] : [`slots[${i}]`])),
|
|
204
|
+
...(scene.sums ?? []).flatMap((tag, i) => (typeof tag === "number" ? [`sums[${i}]`] : [])),
|
|
205
|
+
...(scene.pool ?? []).map((_, i) => `pool[${i}]`),
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
/** magicSolver fills a magic figure: the question of MagicQuestion. */
|
|
209
|
+
export const magicSolver = {
|
|
210
|
+
name: "magic",
|
|
211
|
+
kinds: ["magic"],
|
|
212
|
+
questions: Object.keys(members),
|
|
213
|
+
check,
|
|
214
|
+
facts,
|
|
215
|
+
solve,
|
|
216
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type MatchsticksScene } from "../kinds/matchsticks.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The questions about matches laid on a table.
|
|
5
|
+
*
|
|
6
|
+
* - `how-many-matches`: how many matches the picture shows, a number: every
|
|
7
|
+
* match of every digit and sign of a text, or every stick of a grid but the
|
|
8
|
+
* empty places; or, with the givens' length, `{"length": 5}`, how many a
|
|
9
|
+
* row of that many squares side by side takes, when the picture is a
|
|
10
|
+
* shorter row of squares: 3 for each square and 1 more.
|
|
11
|
+
* - `count-squares`: how many squares of a square grid are whole, a number:
|
|
12
|
+
* counted as countfig counts the squares of a figure, small and big, along
|
|
13
|
+
* the matches that lie there; an empty place is no match. It takes no
|
|
14
|
+
* givens.
|
|
15
|
+
* - `move-one`: which of the task's options, each a number, `{"number": 9}`,
|
|
16
|
+
* or a text, `{"text": "5 + 2 = 7"}`, one match of a text moved to an
|
|
17
|
+
* empty place reaches: a digit or a number for a text of digits, a true
|
|
18
|
+
* sum, a text, for a text with "=". Every match of every symbol is
|
|
19
|
+
* tried in every empty place of every symbol, signs too, and a symbol that
|
|
20
|
+
* is then no digit or sign makes no text; a move adds no symbol. The
|
|
21
|
+
* answer is the one offered option reached, the offered value itself, and
|
|
22
|
+
* it is proved only when a match moved within one number reaches it: a
|
|
23
|
+
* move between two numbers, or between a number and a sign, proves none.
|
|
24
|
+
* An option is read without its spaces, "−" as "-".
|
|
25
|
+
*
|
|
26
|
+
* A coloured match or an empty place marked on a text is a fact no question
|
|
27
|
+
* reads: the picture never shows which match moves.
|
|
28
|
+
*/
|
|
29
|
+
export type MatchsticksQuestion = {
|
|
30
|
+
ask: "how-many-matches" | "count-squares" | "move-one";
|
|
31
|
+
};
|
|
32
|
+
/** The givens of matches: the length of the longer row. */
|
|
33
|
+
export type MatchsticksGivens = {
|
|
34
|
+
length?: number;
|
|
35
|
+
};
|
|
36
|
+
/** matchsticksSolver counts and moves matches: the questions of MatchsticksQuestion. */
|
|
37
|
+
export declare const matchsticksSolver: Solver<MatchsticksScene, MatchsticksQuestion, MatchsticksGivens>;
|