@altnovatrail/picture-kit 0.1.0 → 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 +2 -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,93 @@
|
|
|
1
|
+
import type { GroupsScene } from "../kinds/groups.ts";
|
|
2
|
+
import { type Thing } from "../things.ts";
|
|
3
|
+
import { type Solver } from "./proof.ts";
|
|
4
|
+
/** A holder of a scene of groups, by its index in groups or by its label. */
|
|
5
|
+
export type HolderRef = number | string;
|
|
6
|
+
/** Things a question names: one thing, or several that its word takes together, as "balls". */
|
|
7
|
+
export type Things = Thing | Thing[];
|
|
8
|
+
/**
|
|
9
|
+
* A selection of the things of a scene: of some things, of, or of all but
|
|
10
|
+
* some, not; in one holder, or in all. Whatever it counts, it reads every
|
|
11
|
+
* item of its holders: a thing it passes over is one the learner tells apart
|
|
12
|
+
* from the things counted, so a picture may show it.
|
|
13
|
+
*/
|
|
14
|
+
export type Selection = {
|
|
15
|
+
of?: Things | undefined;
|
|
16
|
+
not?: Things | undefined;
|
|
17
|
+
in?: HolderRef | undefined;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A move the statement tells: count things go from one holder to another
|
|
21
|
+
* before the question is asked — of the thing named, or of the one thing the
|
|
22
|
+
* holder holds. The picture shows the holders before every move.
|
|
23
|
+
*/
|
|
24
|
+
export type Move = {
|
|
25
|
+
from: HolderRef;
|
|
26
|
+
to: HolderRef;
|
|
27
|
+
count: number;
|
|
28
|
+
thing?: Thing;
|
|
29
|
+
};
|
|
30
|
+
/** How many parts each thing has, as the statement says: legs, wheels, the two cherries of a pair. */
|
|
31
|
+
export type Parts = {
|
|
32
|
+
thing: Thing;
|
|
33
|
+
count: number;
|
|
34
|
+
}[];
|
|
35
|
+
/** The givens of a question about groups: the moves the statement tells, and the parts of things. */
|
|
36
|
+
export type GroupsGivens = {
|
|
37
|
+
moves?: Move[];
|
|
38
|
+
parts?: Parts;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The questions about a scene of groups, every one answered by counting
|
|
42
|
+
* what the picture shows, after the moves of the givens, if any.
|
|
43
|
+
*
|
|
44
|
+
* - `how-many`: how many things of the selection, a number.
|
|
45
|
+
* - `how-many-kinds`: how many different things the selection holds, a number.
|
|
46
|
+
* - `how-many-parts`: how many parts the things of the selection have in
|
|
47
|
+
* all, a number; the parts of the givens say how many each thing has, and
|
|
48
|
+
* name every thing of the selection.
|
|
49
|
+
* - `difference`: by how many one selection outnumbers the other, as "how
|
|
50
|
+
* many more pears than apples" or "on tree A than on tree B", a number.
|
|
51
|
+
* - `which-holder-most`, `which-holder-fewest`: the holder with the most or
|
|
52
|
+
* the fewest things of the selection, named by its label as a text; every
|
|
53
|
+
* group needs a label. A tie has no answer.
|
|
54
|
+
* - `which-holder-holds`: the one holder with exactly count things of the
|
|
55
|
+
* selection, by its label; none, or more than one, has no answer.
|
|
56
|
+
* - `which-thing-most`, `which-thing-fewest`: the thing there are most or
|
|
57
|
+
* fewest of, in a holder or in all, a thing. A tie has no answer.
|
|
58
|
+
* - `to-equal`: how many things of the selection to move from the fuller of
|
|
59
|
+
* two holders to the other for them to hold the same, a number; an odd
|
|
60
|
+
* difference has no answer.
|
|
61
|
+
* - `to-reach`: how many more things the selection needs to hold target, a
|
|
62
|
+
* number; a selection past target has no answer.
|
|
63
|
+
*/
|
|
64
|
+
export type GroupsQuestion = ({
|
|
65
|
+
ask: "how-many" | "how-many-kinds" | "how-many-parts";
|
|
66
|
+
} & Selection) | {
|
|
67
|
+
ask: "difference";
|
|
68
|
+
a: Selection;
|
|
69
|
+
b: Selection;
|
|
70
|
+
} | {
|
|
71
|
+
ask: "which-holder-most" | "which-holder-fewest";
|
|
72
|
+
of?: Things;
|
|
73
|
+
not?: Things;
|
|
74
|
+
} | {
|
|
75
|
+
ask: "which-holder-holds";
|
|
76
|
+
count: number;
|
|
77
|
+
of?: Things;
|
|
78
|
+
not?: Things;
|
|
79
|
+
} | {
|
|
80
|
+
ask: "which-thing-most" | "which-thing-fewest";
|
|
81
|
+
in?: HolderRef;
|
|
82
|
+
} | {
|
|
83
|
+
ask: "to-equal";
|
|
84
|
+
a: HolderRef;
|
|
85
|
+
b: HolderRef;
|
|
86
|
+
of?: Things;
|
|
87
|
+
not?: Things;
|
|
88
|
+
} | ({
|
|
89
|
+
ask: "to-reach";
|
|
90
|
+
target: number;
|
|
91
|
+
} & Selection);
|
|
92
|
+
/** groupsSolver counts the things of a scene of groups: the questions of GroupsQuestion. */
|
|
93
|
+
export declare const groupsSolver: Solver<GroupsScene, GroupsQuestion, GroupsGivens | undefined>;
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { int, isObj, list, only } from "../read.js";
|
|
2
|
+
import { checkThing, sameThing, wordOf } from "../things.js";
|
|
3
|
+
import { listOf } from "../words.js";
|
|
4
|
+
import { noScene, solved, unsolved } from "./proof.js";
|
|
5
|
+
import { askOf, namedGroups, refOf, sceneless } from "./question.js";
|
|
6
|
+
const members = {
|
|
7
|
+
"how-many": ["of", "not", "in"],
|
|
8
|
+
difference: ["a", "b"],
|
|
9
|
+
"which-holder-most": ["of", "not"],
|
|
10
|
+
"which-holder-fewest": ["of", "not"],
|
|
11
|
+
"which-thing-most": ["in"],
|
|
12
|
+
"which-thing-fewest": ["in"],
|
|
13
|
+
"to-equal": ["a", "b", "of", "not"],
|
|
14
|
+
"to-reach": ["target", "of", "not", "in"],
|
|
15
|
+
"how-many-kinds": ["of", "not", "in"],
|
|
16
|
+
"how-many-parts": ["of", "not", "in"],
|
|
17
|
+
"which-holder-holds": ["count", "of", "not"],
|
|
18
|
+
};
|
|
19
|
+
/** The most things a group holds, as the kind allows; the most moves; the most parts of a thing. */
|
|
20
|
+
const most = 24;
|
|
21
|
+
const mostMoves = 6;
|
|
22
|
+
const mostParts = 100;
|
|
23
|
+
const labelsOf = (scene) => scene.groups.map((g) => g.label);
|
|
24
|
+
const asList = (things) => (Array.isArray(things) ? things : [things]);
|
|
25
|
+
const both = (...lists) => [...new Set(lists.flat())];
|
|
26
|
+
function indexOf(scene, ref) {
|
|
27
|
+
return typeof ref === "number" ? ref : labelsOf(scene).indexOf(ref);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* stateOf makes the moves of the givens one after another, and records in
|
|
31
|
+
* problems a move the holders cannot make at its turn.
|
|
32
|
+
*/
|
|
33
|
+
function stateOf(scene, moves, problems) {
|
|
34
|
+
const held = scene.groups.map((g, i) => g.items.map((item, j) => ({
|
|
35
|
+
thing: item.thing,
|
|
36
|
+
count: item.count,
|
|
37
|
+
paths: [`groups[${i}].items[${j}]`],
|
|
38
|
+
})));
|
|
39
|
+
const moved = new Set();
|
|
40
|
+
const labels = labelsOf(scene);
|
|
41
|
+
moves.forEach((m, k) => {
|
|
42
|
+
if (!isObj(m))
|
|
43
|
+
return;
|
|
44
|
+
const path = `givens.moves[${k}]`;
|
|
45
|
+
const before = problems.length;
|
|
46
|
+
const from = refOf(m.from, labels, "groups", `${path}.from`, problems);
|
|
47
|
+
const to = refOf(m.to, labels, "groups", `${path}.to`, problems);
|
|
48
|
+
if (from !== undefined && from === to)
|
|
49
|
+
problems.push(`${path}.to: is the same group as ${path}.from`);
|
|
50
|
+
const count = int(m.count, `${path}.count`, problems, 1, most);
|
|
51
|
+
if (m.thing !== undefined)
|
|
52
|
+
checkThing(m.thing, `${path}.thing`, problems);
|
|
53
|
+
if (problems.length > before || from === undefined || to === undefined)
|
|
54
|
+
return;
|
|
55
|
+
const source = held[from].filter((h) => h.count > 0);
|
|
56
|
+
const kinds = source.filter((h, i) => source.findIndex((o) => sameThing(o.thing, h.thing)) === i);
|
|
57
|
+
const thing = m.thing ?? (kinds.length === 1 ? kinds[0].thing : null);
|
|
58
|
+
if (thing === null) {
|
|
59
|
+
problems.push(`${path}.thing: groups[${from}] holds ${kinds.length === 0 ? "nothing" : "more than one thing"} at this move; name the thing that moves`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const mine = source.filter((h) => sameThing(h.thing, thing));
|
|
63
|
+
const there = mine.reduce((sum, h) => sum + h.count, 0);
|
|
64
|
+
if (there < count) {
|
|
65
|
+
problems.push(`${path}.count: groups[${from}] holds ${there} ${wordOf(thing, there)} at this move, fewer than ${count}`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
for (const h of [...held[from], ...held[to]])
|
|
69
|
+
for (const p of h.paths)
|
|
70
|
+
moved.add(p);
|
|
71
|
+
let left = count;
|
|
72
|
+
const paths = [];
|
|
73
|
+
for (const h of mine) {
|
|
74
|
+
const take = Math.min(left, h.count);
|
|
75
|
+
h.count -= take;
|
|
76
|
+
left -= take;
|
|
77
|
+
paths.push(...h.paths);
|
|
78
|
+
}
|
|
79
|
+
const into = held[to].find((h) => sameThing(h.thing, thing));
|
|
80
|
+
if (into) {
|
|
81
|
+
into.count += count;
|
|
82
|
+
into.paths = both(into.paths, paths);
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
held[to].push({ thing, count, paths });
|
|
86
|
+
});
|
|
87
|
+
return { held, moved: [...moved] };
|
|
88
|
+
}
|
|
89
|
+
/** counts tells whether a selection counts a thing. */
|
|
90
|
+
function counts(sel, thing) {
|
|
91
|
+
if (sel.of !== undefined && !asList(sel.of).some((t) => sameThing(t, thing)))
|
|
92
|
+
return false;
|
|
93
|
+
return !(sel.not !== undefined && asList(sel.not).some((t) => sameThing(t, thing)));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* select counts the things of a selection. It reads every item of the
|
|
97
|
+
* selection's holders, counted or passed over, and every item of a holder
|
|
98
|
+
* a move touched.
|
|
99
|
+
*/
|
|
100
|
+
function select(scene, state, sel) {
|
|
101
|
+
const at = sel.in === undefined ? undefined : indexOf(scene, sel.in);
|
|
102
|
+
const all = state.held.flatMap((hs, i) => (at !== undefined && i !== at ? [] : hs));
|
|
103
|
+
const things = all.filter((h) => h.count > 0 && counts(sel, h.thing));
|
|
104
|
+
return {
|
|
105
|
+
count: things.reduce((sum, h) => sum + h.count, 0),
|
|
106
|
+
things,
|
|
107
|
+
reads: both(all.flatMap((h) => h.paths), state.moved),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** thingsIn totals each different thing of a selection, in order of first place. */
|
|
111
|
+
function thingsIn(scene, state, sel) {
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const h of select(scene, state, sel).things) {
|
|
114
|
+
const one = out.find((o) => sameThing(o.thing, h.thing));
|
|
115
|
+
if (one)
|
|
116
|
+
one.count += h.count;
|
|
117
|
+
else
|
|
118
|
+
out.push({ ...h });
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
function checkThings(value, path, scene, problems) {
|
|
123
|
+
const many = Array.isArray(value);
|
|
124
|
+
const things = many ? list(value, path, problems, 1, 8) : [value];
|
|
125
|
+
things.forEach((t, i) => {
|
|
126
|
+
const at = many ? `${path}[${i}]` : path;
|
|
127
|
+
const before = problems.length;
|
|
128
|
+
checkThing(t, at, problems);
|
|
129
|
+
if (problems.length > before)
|
|
130
|
+
return;
|
|
131
|
+
const thing = t;
|
|
132
|
+
if (things.findIndex((o) => JSON.stringify(o) === JSON.stringify(thing)) < i)
|
|
133
|
+
problems.push(`${at}: is named twice`);
|
|
134
|
+
else if (scene && !scene.groups.some((g) => g.items.some((x) => sameThing(x.thing, thing))))
|
|
135
|
+
problems.push(`${at}: no group holds ${wordOf(thing, 2)}`);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function checkSelection(sel, path, scene, problems) {
|
|
139
|
+
if (sel.of !== undefined)
|
|
140
|
+
checkThings(sel.of, `${path}.of`, scene, problems);
|
|
141
|
+
if (sel.not !== undefined)
|
|
142
|
+
checkThings(sel.not, `${path}.not`, scene, problems);
|
|
143
|
+
if (sel.of !== undefined && sel.not !== undefined)
|
|
144
|
+
problems.push(`${path}.not: a selection names either the things it counts or those it leaves`);
|
|
145
|
+
if (sel.in !== undefined && scene)
|
|
146
|
+
refOf(sel.in, labelsOf(scene), "groups", `${path}.in`, problems);
|
|
147
|
+
}
|
|
148
|
+
/** checkGivens records what is wrong with the givens, and gives the moves they tell. */
|
|
149
|
+
function checkGivens(givens, ask, problems) {
|
|
150
|
+
const needs = "givens.parts: how-many-parts needs how many parts each thing has";
|
|
151
|
+
if (givens === undefined) {
|
|
152
|
+
if (ask === "how-many-parts")
|
|
153
|
+
problems.push(needs);
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
if (!isObj(givens)) {
|
|
157
|
+
problems.push("givens: must be an object");
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
only(givens, ["moves", "parts"], "givens", problems, "the givens of groups");
|
|
161
|
+
const moves = givens.moves === undefined ? [] : list(givens.moves, "givens.moves", problems, 1, mostMoves);
|
|
162
|
+
moves.forEach((m, k) => {
|
|
163
|
+
const path = `givens.moves[${k}]`;
|
|
164
|
+
if (isObj(m))
|
|
165
|
+
only(m, ["from", "to", "count", "thing"], path, problems, "a move");
|
|
166
|
+
else
|
|
167
|
+
problems.push(`${path}: must be a move, as {"from": "A", "to": "B", "count": 2}`);
|
|
168
|
+
});
|
|
169
|
+
if (givens.parts === undefined) {
|
|
170
|
+
if (ask === "how-many-parts")
|
|
171
|
+
problems.push(needs);
|
|
172
|
+
}
|
|
173
|
+
else if (ask !== undefined && ask !== "how-many-parts")
|
|
174
|
+
problems.push(`givens.parts: ${ask} counts things, not their parts`);
|
|
175
|
+
else
|
|
176
|
+
list(givens.parts, "givens.parts", problems, 1, 8).forEach((p, k) => {
|
|
177
|
+
const path = `givens.parts[${k}]`;
|
|
178
|
+
if (!isObj(p)) {
|
|
179
|
+
problems.push(`${path}: must be a thing and its parts, as {"thing": "animal.duck", "count": 2}`);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
only(p, ["thing", "count"], path, problems, "the parts of a thing");
|
|
183
|
+
checkThing(p.thing, `${path}.thing`, problems);
|
|
184
|
+
int(p.count, `${path}.count`, problems, 1, mostParts);
|
|
185
|
+
});
|
|
186
|
+
return moves;
|
|
187
|
+
}
|
|
188
|
+
/** checkParts records a thing of the selection whose parts the givens leave out, and a part not needed. */
|
|
189
|
+
function checkParts(things, parts, problems) {
|
|
190
|
+
parts.forEach((p, k) => {
|
|
191
|
+
const path = `givens.parts[${k}].thing`;
|
|
192
|
+
if (parts.findIndex((o) => sameThing(o.thing, p.thing)) < k)
|
|
193
|
+
problems.push(`${path}: the parts of ${wordOf(p.thing, 2)} are given already`);
|
|
194
|
+
else if (!things.some((h) => sameThing(h.thing, p.thing)))
|
|
195
|
+
problems.push(`${path}: the selection holds no ${wordOf(p.thing, 2)}`);
|
|
196
|
+
});
|
|
197
|
+
const said = new Set();
|
|
198
|
+
for (const h of things) {
|
|
199
|
+
const key = JSON.stringify(h.thing);
|
|
200
|
+
if (said.has(key) || parts.some((p) => sameThing(p.thing, h.thing)))
|
|
201
|
+
continue;
|
|
202
|
+
said.add(key);
|
|
203
|
+
problems.push(`givens.parts: does not say how many parts ${wordOf(h.thing, 2)} have`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function check(question, givens, scene) {
|
|
207
|
+
const problems = [];
|
|
208
|
+
const read = askOf(question, members, problems);
|
|
209
|
+
const moves = checkGivens(givens, read?.ask, problems);
|
|
210
|
+
if (!read)
|
|
211
|
+
return problems;
|
|
212
|
+
const { q, ask } = read;
|
|
213
|
+
const labels = scene ? labelsOf(scene) : [];
|
|
214
|
+
switch (ask) {
|
|
215
|
+
case "difference":
|
|
216
|
+
for (const side of ["a", "b"]) {
|
|
217
|
+
const sel = q[side];
|
|
218
|
+
if (!isObj(sel)) {
|
|
219
|
+
problems.push(`question.${side}: must be a selection, as {"of": "fruit.apple", "in": "A"}`);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
only(sel, ["of", "not", "in"], `question.${side}`, problems, "a selection");
|
|
223
|
+
checkSelection(sel, `question.${side}`, scene, problems);
|
|
224
|
+
}
|
|
225
|
+
if (JSON.stringify(q.a) === JSON.stringify(q.b))
|
|
226
|
+
problems.push("question.b: is the same selection as question.a");
|
|
227
|
+
break;
|
|
228
|
+
case "to-equal": {
|
|
229
|
+
checkSelection(q, "question", scene, problems);
|
|
230
|
+
if (!scene)
|
|
231
|
+
break;
|
|
232
|
+
const a = refOf(q.a, labels, "groups", "question.a", problems);
|
|
233
|
+
const b = refOf(q.b, labels, "groups", "question.b", problems);
|
|
234
|
+
if (a !== undefined && a === b)
|
|
235
|
+
problems.push("question.b: is the same group as question.a");
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
default:
|
|
239
|
+
checkSelection(q, "question", scene, problems);
|
|
240
|
+
}
|
|
241
|
+
if (ask === "to-reach")
|
|
242
|
+
int(q.target, "question.target", problems, 1, 100);
|
|
243
|
+
if (ask === "which-holder-holds")
|
|
244
|
+
int(q.count, "question.count", problems, 0, most);
|
|
245
|
+
if (scene &&
|
|
246
|
+
(ask === "which-holder-most" || ask === "which-holder-fewest" || ask === "which-holder-holds"))
|
|
247
|
+
namedGroups(labels, ask, problems);
|
|
248
|
+
if (!scene) {
|
|
249
|
+
sceneless(ask, "a scene of groups", problems);
|
|
250
|
+
return problems;
|
|
251
|
+
}
|
|
252
|
+
const state = stateOf(scene, moves, problems);
|
|
253
|
+
if (problems.length > 0)
|
|
254
|
+
return problems;
|
|
255
|
+
if (ask === "which-thing-most" || ask === "which-thing-fewest")
|
|
256
|
+
if (thingsIn(scene, state, q).length < 2)
|
|
257
|
+
problems.push(`question.ask: ${ask} needs two different things or more in the selection`);
|
|
258
|
+
if (ask === "how-many-parts")
|
|
259
|
+
checkParts(select(scene, state, q).things, givens.parts, problems);
|
|
260
|
+
return problems;
|
|
261
|
+
}
|
|
262
|
+
function solve(scene, question, givens) {
|
|
263
|
+
if (!scene)
|
|
264
|
+
return noScene;
|
|
265
|
+
const state = stateOf(scene, givens?.moves ?? [], []);
|
|
266
|
+
switch (question.ask) {
|
|
267
|
+
case "how-many": {
|
|
268
|
+
const { count, reads } = select(scene, state, question);
|
|
269
|
+
return solved({ number: count }, reads);
|
|
270
|
+
}
|
|
271
|
+
case "how-many-kinds": {
|
|
272
|
+
const { reads } = select(scene, state, question);
|
|
273
|
+
return solved({ number: thingsIn(scene, state, question).length }, reads);
|
|
274
|
+
}
|
|
275
|
+
case "how-many-parts": {
|
|
276
|
+
const { things, reads } = select(scene, state, question);
|
|
277
|
+
const parts = givens.parts;
|
|
278
|
+
const total = things.reduce((sum, h) => sum + h.count * parts.find((p) => sameThing(p.thing, h.thing)).count, 0);
|
|
279
|
+
return solved({ number: total }, reads);
|
|
280
|
+
}
|
|
281
|
+
case "difference": {
|
|
282
|
+
const a = select(scene, state, question.a);
|
|
283
|
+
const b = select(scene, state, question.b);
|
|
284
|
+
return solved({ number: Math.abs(a.count - b.count) }, both(a.reads, b.reads));
|
|
285
|
+
}
|
|
286
|
+
case "which-holder-most":
|
|
287
|
+
case "which-holder-fewest":
|
|
288
|
+
case "which-holder-holds": {
|
|
289
|
+
const sel = { of: question.of, not: question.not };
|
|
290
|
+
const all = scene.groups.map((_, i) => select(scene, state, { ...sel, in: i }));
|
|
291
|
+
const reads = both(...all.map((c, i) => [...c.reads, `groups[${i}].label`]));
|
|
292
|
+
const values = all.map((c) => c.count);
|
|
293
|
+
const best = question.ask === "which-holder-holds"
|
|
294
|
+
? question.count
|
|
295
|
+
: question.ask === "which-holder-most"
|
|
296
|
+
? Math.max(...values)
|
|
297
|
+
: Math.min(...values);
|
|
298
|
+
const at = scene.groups.filter((_, i) => values[i] === best).map((g) => g.label);
|
|
299
|
+
if (at.length === 1)
|
|
300
|
+
return solved({ text: at[0] }, reads);
|
|
301
|
+
if (at.length === 0)
|
|
302
|
+
return unsolved(`no group holds exactly ${best}`, reads);
|
|
303
|
+
return unsolved(`${listOf(at)} hold ${best} each, a tie`, reads);
|
|
304
|
+
}
|
|
305
|
+
case "which-thing-most":
|
|
306
|
+
case "which-thing-fewest": {
|
|
307
|
+
const totals = thingsIn(scene, state, question);
|
|
308
|
+
const reads = select(scene, state, { in: question.in }).reads;
|
|
309
|
+
const values = totals.map((t) => t.count);
|
|
310
|
+
const best = question.ask === "which-thing-most" ? Math.max(...values) : Math.min(...values);
|
|
311
|
+
const at = totals.filter((t) => t.count === best);
|
|
312
|
+
if (at.length === 1)
|
|
313
|
+
return solved({ thing: at[0].thing }, reads);
|
|
314
|
+
return unsolved(`${listOf(at.map((t) => wordOf(t.thing, 2)))} are ${best} each, a tie`, reads);
|
|
315
|
+
}
|
|
316
|
+
case "to-equal": {
|
|
317
|
+
const sel = { of: question.of, not: question.not };
|
|
318
|
+
const a = select(scene, state, { ...sel, in: question.a });
|
|
319
|
+
const b = select(scene, state, { ...sel, in: question.b });
|
|
320
|
+
const reads = both(a.reads, b.reads);
|
|
321
|
+
const d = Math.abs(a.count - b.count);
|
|
322
|
+
if (d % 2 === 1)
|
|
323
|
+
return unsolved(`the two hold ${a.count} and ${b.count}, an odd difference no move evens`, reads);
|
|
324
|
+
return solved({ number: d / 2 }, reads);
|
|
325
|
+
}
|
|
326
|
+
case "to-reach": {
|
|
327
|
+
const { count, reads } = select(scene, state, question);
|
|
328
|
+
if (count > question.target)
|
|
329
|
+
return unsolved(`the selection holds ${count}, more than ${question.target} already`, reads);
|
|
330
|
+
return solved({ number: question.target - count }, reads);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** The facts of a scene of groups are its items, each a thing and its count. */
|
|
335
|
+
function facts(scene) {
|
|
336
|
+
return scene.groups.flatMap((g, i) => g.items.map((_, j) => `groups[${i}].items[${j}]`));
|
|
337
|
+
}
|
|
338
|
+
/** groupsSolver counts the things of a scene of groups: the questions of GroupsQuestion. */
|
|
339
|
+
export const groupsSolver = {
|
|
340
|
+
name: "groups",
|
|
341
|
+
kinds: ["groups"],
|
|
342
|
+
questions: Object.keys(members),
|
|
343
|
+
check,
|
|
344
|
+
facts,
|
|
345
|
+
solve,
|
|
346
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Proof, type Solver } from "./proof.ts";
|
|
2
|
+
/** The closed list of the topics' solvers, each by its name. */
|
|
3
|
+
export declare const solvers: readonly Solver[];
|
|
4
|
+
/**
|
|
5
|
+
* prove answers a question about a scene by the solver of that name: the
|
|
6
|
+
* scene, if the task has one, is checked as render checks it and must be of
|
|
7
|
+
* a kind the solver reads, then what the task offers, and only then the
|
|
8
|
+
* question and the givens, by the solver, which refuses a task with no scene
|
|
9
|
+
* unless its question needs none. offered is what the task offers, its
|
|
10
|
+
* options and the scenes of its picture options, which a question that
|
|
11
|
+
* answers among them reads; left out, the task offers nothing. A task that
|
|
12
|
+
* fails a check is refused: prove throws SceneError with every problem, each
|
|
13
|
+
* with its path — `scales[1].tilt: …`, `question.of: …`, `choices[0]: …`.
|
|
14
|
+
* The solver's check runs only on a scene, options and choices that passed
|
|
15
|
+
* their own checks, so a task with a broken scene is told the scene's
|
|
16
|
+
* problems first.
|
|
17
|
+
*/
|
|
18
|
+
export declare function prove(name: string, scene: unknown, question: unknown, givens?: unknown, offered?: {
|
|
19
|
+
options?: readonly unknown[];
|
|
20
|
+
choices?: readonly unknown[];
|
|
21
|
+
}): Proof;
|
|
22
|
+
/**
|
|
23
|
+
* Task is what the gate reads of a task: its solver, scene, question, givens, options, picture
|
|
24
|
+
* options and key.
|
|
25
|
+
*/
|
|
26
|
+
export type Task = {
|
|
27
|
+
solver: string;
|
|
28
|
+
scene?: unknown;
|
|
29
|
+
question: unknown;
|
|
30
|
+
givens?: unknown;
|
|
31
|
+
options: readonly unknown[];
|
|
32
|
+
/**
|
|
33
|
+
* The scenes of the picture options, in the order of the letter options: the first letter
|
|
34
|
+
* option is A and shows choices[0], the next B, and so on. A task shown with its options in
|
|
35
|
+
* another order moves each scene with its option and letters them anew by their places, as the
|
|
36
|
+
* writer draws them, so the key stays the option of the same scene and its letter changes.
|
|
37
|
+
*/
|
|
38
|
+
choices?: readonly unknown[];
|
|
39
|
+
/** The index in options of the option that answers. */
|
|
40
|
+
key: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* turned is a task with its options turned by a number of places, as a learner may be shown them:
|
|
44
|
+
* the option at place i goes to place i - by, the key with its option, and each picture option's
|
|
45
|
+
* scene with it, lettered anew by its place among the letter options.
|
|
46
|
+
*/
|
|
47
|
+
export declare function turned(task: Task, by: number): Task;
|
|
48
|
+
/**
|
|
49
|
+
* checkTask lists what keeps a task's key from being proved; none means the
|
|
50
|
+
* key is proved. The scene, the question, the givens and what the task
|
|
51
|
+
* offers pass their checks, as prove has them; there are two options or
|
|
52
|
+
* more, each an Option; every fact of the scene is one the solver read, for a
|
|
53
|
+
* fact the task does not need distracts or lies; and exactly one option
|
|
54
|
+
* matches the proof, the key — for an option "it cannot be told", the
|
|
55
|
+
* solver proves no answer and names two candidates or more. The key holds
|
|
56
|
+
* whatever the order of the options: the gate proves the task again with its
|
|
57
|
+
* options turned by every number of places, its picture options lettered
|
|
58
|
+
* anew, and refuses a turn in which the key's option no longer matches alone.
|
|
59
|
+
*/
|
|
60
|
+
export declare function checkTask(task: Task): string[];
|