@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,222 @@
|
|
|
1
|
+
import { int } from "../read.js";
|
|
2
|
+
import { listOf } from "../words.js";
|
|
3
|
+
import { noScene, solved, unsolved } from "./proof.js";
|
|
4
|
+
import { askOf, flagOf, namedGroups, noGivens, refOf, sceneless } from "./question.js";
|
|
5
|
+
const members = {
|
|
6
|
+
total: ["in"],
|
|
7
|
+
"can-pay": ["price", "good", "in", "exact"],
|
|
8
|
+
"fewest-coins": ["price", "good", "in", "any"],
|
|
9
|
+
change: ["price", "good", "paid", "in"],
|
|
10
|
+
"which-holder-most": [],
|
|
11
|
+
"which-holder-fewest": [],
|
|
12
|
+
difference: ["a", "b"],
|
|
13
|
+
"missing-coin": ["total", "good", "in"],
|
|
14
|
+
};
|
|
15
|
+
/** The largest price or sum a question names: the searches below run up to it. */
|
|
16
|
+
const top = 100000;
|
|
17
|
+
const whole = /^[0-9]+$/;
|
|
18
|
+
const groupsOf = (scene) => scene.groups ?? [];
|
|
19
|
+
const goodsOf = (scene) => scene.goods ?? [];
|
|
20
|
+
/** piecesOf lists every coin and note of the money, one by one, with the path of its item. */
|
|
21
|
+
function piecesOf(scene, holder) {
|
|
22
|
+
const groups = groupsOf(scene);
|
|
23
|
+
const at = holder === undefined
|
|
24
|
+
? undefined
|
|
25
|
+
: typeof holder === "number"
|
|
26
|
+
? holder
|
|
27
|
+
: groups.findIndex((g) => g.label === holder);
|
|
28
|
+
return groups.flatMap((g, i) => at !== undefined && i !== at
|
|
29
|
+
? []
|
|
30
|
+
: g.items.flatMap((item, j) => {
|
|
31
|
+
const shown = item.coin ?? item.note;
|
|
32
|
+
const value = shown === "?" ? Number.NaN : shown;
|
|
33
|
+
return Array.from({ length: item.count ?? 1 }, () => ({
|
|
34
|
+
value,
|
|
35
|
+
path: `groups[${i}].items[${j}]`,
|
|
36
|
+
}));
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
const readsOf = (pieces) => [...new Set(pieces.map((p) => p.path))];
|
|
40
|
+
const sum = (pieces) => pieces.reduce((s, p) => s + p.value, 0);
|
|
41
|
+
/** priceOf is the price or total a question names, and the path of the good it read, if any. */
|
|
42
|
+
function priceOf(scene, q) {
|
|
43
|
+
const given = q.price ?? q.total;
|
|
44
|
+
if (given !== undefined)
|
|
45
|
+
return { price: given, reads: [] };
|
|
46
|
+
const goods = goodsOf(scene);
|
|
47
|
+
const at = typeof q.good === "number" ? q.good : goods.findIndex((g) => g.label === q.good);
|
|
48
|
+
return { price: Number(goods[at].price), reads: [`goods[${at}]`] };
|
|
49
|
+
}
|
|
50
|
+
function check(question, givens, scene) {
|
|
51
|
+
const problems = [];
|
|
52
|
+
noGivens(givens, problems);
|
|
53
|
+
const read = askOf(question, members, problems);
|
|
54
|
+
if (!read)
|
|
55
|
+
return problems;
|
|
56
|
+
if (!scene) {
|
|
57
|
+
sceneless(read.ask, "a scene of coins", problems);
|
|
58
|
+
return problems;
|
|
59
|
+
}
|
|
60
|
+
const { q, ask } = read;
|
|
61
|
+
const groups = groupsOf(scene);
|
|
62
|
+
const labels = groups.map((g) => g.label);
|
|
63
|
+
if (groups.length === 0 && !(ask === "change" && q.paid !== undefined))
|
|
64
|
+
problems.push(`question.ask: ${ask} counts money, and the scene shows none`);
|
|
65
|
+
if (q.in !== undefined)
|
|
66
|
+
refOf(q.in, labels, "groups", "question.in", problems);
|
|
67
|
+
const amount = ask === "missing-coin" ? "total" : "price";
|
|
68
|
+
if (ask === "can-pay" || ask === "fewest-coins" || ask === "change" || ask === "missing-coin") {
|
|
69
|
+
if ((q[amount] === undefined) === (q.good === undefined))
|
|
70
|
+
problems.push(`question: must give either ${amount} or good, ${ask === "missing-coin" ? "what the money is worth" : "the price to pay"}`);
|
|
71
|
+
if (q[amount] !== undefined)
|
|
72
|
+
int(q[amount], `question.${amount}`, problems, 1, top);
|
|
73
|
+
if (q.good !== undefined) {
|
|
74
|
+
const goods = goodsOf(scene);
|
|
75
|
+
const at = refOf(q.good, goods.map((g) => g.label), "goods", "question.good", problems);
|
|
76
|
+
const tag = at === undefined ? "" : goods[at].price;
|
|
77
|
+
if (at !== undefined && !(whole.test(tag) && Number(tag) >= 1 && Number(tag) <= top))
|
|
78
|
+
problems.push(`question.good: goods[${at}] has the price ${tag}, not a whole number from 1 to ${top}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (ask === "change" && q.paid !== undefined)
|
|
82
|
+
int(q.paid, "question.paid", problems, 1, top);
|
|
83
|
+
flagOf(q.exact, "question.exact", problems);
|
|
84
|
+
flagOf(q.any, "question.any", problems);
|
|
85
|
+
if (ask === "which-holder-most" || ask === "which-holder-fewest")
|
|
86
|
+
namedGroups(labels, ask, problems);
|
|
87
|
+
if (ask === "difference") {
|
|
88
|
+
const a = refOf(q.a, labels, "groups", "question.a", problems);
|
|
89
|
+
const b = refOf(q.b, labels, "groups", "question.b", problems);
|
|
90
|
+
if (a !== undefined && a === b)
|
|
91
|
+
problems.push("question.b: is the same group as question.a");
|
|
92
|
+
}
|
|
93
|
+
if (problems.length > 0 || (ask === "change" && q.paid !== undefined))
|
|
94
|
+
return problems;
|
|
95
|
+
const read_ = ask === "difference"
|
|
96
|
+
? [q.a, q.b]
|
|
97
|
+
: ask === "which-holder-most" || ask === "which-holder-fewest" || q.in === undefined
|
|
98
|
+
? undefined
|
|
99
|
+
: [q.in];
|
|
100
|
+
const asked = [];
|
|
101
|
+
groups.forEach((g, i) => {
|
|
102
|
+
if (read_ && !read_.some((h) => h === i || h === g.label))
|
|
103
|
+
return;
|
|
104
|
+
g.items.forEach((item, j) => {
|
|
105
|
+
if ((item.coin ?? item.note) === "?")
|
|
106
|
+
asked.push(`groups[${i}].items[${j}]`);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
if (ask !== "missing-coin")
|
|
110
|
+
for (const at of asked)
|
|
111
|
+
problems.push(`${at}: shows ? for its value, which ${ask} has to read`);
|
|
112
|
+
else if (asked.length !== 1)
|
|
113
|
+
problems.push(asked.length === 0
|
|
114
|
+
? "question.ask: missing-coin finds the value shown with ?, and the money shows none"
|
|
115
|
+
: `question.ask: missing-coin finds one value shown with ?, and ${listOf(asked)} show ? each`);
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* fewestOf is the fewest of values that make amount exactly: each value once, or with reuse as
|
|
120
|
+
* many times as needed; undefined when none do. It tries every sum up to amount.
|
|
121
|
+
*/
|
|
122
|
+
function fewestOf(values, amount, reuse) {
|
|
123
|
+
const none = Number.POSITIVE_INFINITY;
|
|
124
|
+
const best = new Array(amount + 1).fill(none);
|
|
125
|
+
best[0] = 0;
|
|
126
|
+
for (const v of values) {
|
|
127
|
+
if (reuse)
|
|
128
|
+
for (let s = v; s <= amount; s++)
|
|
129
|
+
best[s] = Math.min(best[s], best[s - v] + 1);
|
|
130
|
+
else
|
|
131
|
+
for (let s = amount; s >= v; s--)
|
|
132
|
+
best[s] = Math.min(best[s], best[s - v] + 1);
|
|
133
|
+
}
|
|
134
|
+
return best[amount] === none ? undefined : best[amount];
|
|
135
|
+
}
|
|
136
|
+
function solve(scene, question) {
|
|
137
|
+
if (!scene)
|
|
138
|
+
return noScene;
|
|
139
|
+
switch (question.ask) {
|
|
140
|
+
case "total": {
|
|
141
|
+
const pieces = piecesOf(scene, question.in);
|
|
142
|
+
return solved({ number: sum(pieces) }, readsOf(pieces));
|
|
143
|
+
}
|
|
144
|
+
case "can-pay": {
|
|
145
|
+
const pieces = piecesOf(scene, question.in);
|
|
146
|
+
const { price, reads } = priceOf(scene, question);
|
|
147
|
+
const all = [...readsOf(pieces), ...reads];
|
|
148
|
+
const pays = question.exact
|
|
149
|
+
? fewestOf(pieces.map((p) => p.value), price, false) !== undefined
|
|
150
|
+
: sum(pieces) >= price;
|
|
151
|
+
return solved({ truth: pays }, all);
|
|
152
|
+
}
|
|
153
|
+
case "fewest-coins": {
|
|
154
|
+
const pieces = piecesOf(scene, question.in);
|
|
155
|
+
const { price, reads } = priceOf(scene, question);
|
|
156
|
+
const all = [...readsOf(pieces), ...reads];
|
|
157
|
+
const values = question.any
|
|
158
|
+
? [...new Set(pieces.map((p) => p.value))]
|
|
159
|
+
: pieces.map((p) => p.value);
|
|
160
|
+
const k = fewestOf(values, price, question.any === true);
|
|
161
|
+
if (k === undefined)
|
|
162
|
+
return unsolved(`no coins and notes of the money make ${price}`, all);
|
|
163
|
+
return solved({ number: k }, all);
|
|
164
|
+
}
|
|
165
|
+
case "change": {
|
|
166
|
+
const pieces = question.paid === undefined ? piecesOf(scene, question.in) : [];
|
|
167
|
+
const paid = question.paid ?? sum(pieces);
|
|
168
|
+
const { price, reads } = priceOf(scene, question);
|
|
169
|
+
const all = [...readsOf(pieces), ...reads];
|
|
170
|
+
if (paid < price)
|
|
171
|
+
return unsolved(`${paid} is less than the price, ${price}`, all);
|
|
172
|
+
return solved({ number: paid - price }, all);
|
|
173
|
+
}
|
|
174
|
+
case "which-holder-most":
|
|
175
|
+
case "which-holder-fewest": {
|
|
176
|
+
const groups = groupsOf(scene);
|
|
177
|
+
const worth = groups.map((_, i) => piecesOf(scene, i));
|
|
178
|
+
const reads = worth.flatMap((p, i) => [...readsOf(p), `groups[${i}].label`]);
|
|
179
|
+
const values = worth.map(sum);
|
|
180
|
+
const best = question.ask === "which-holder-most" ? Math.max(...values) : Math.min(...values);
|
|
181
|
+
const at = groups.filter((_, i) => values[i] === best).map((g) => g.label);
|
|
182
|
+
if (at.length === 1)
|
|
183
|
+
return solved({ text: at[0] }, reads);
|
|
184
|
+
return unsolved(`${listOf(at)} hold ${best} each, a tie`, reads);
|
|
185
|
+
}
|
|
186
|
+
case "difference": {
|
|
187
|
+
const a = piecesOf(scene, question.a);
|
|
188
|
+
const b = piecesOf(scene, question.b);
|
|
189
|
+
return solved({ number: Math.abs(sum(a) - sum(b)) }, [...readsOf(a), ...readsOf(b)]);
|
|
190
|
+
}
|
|
191
|
+
case "missing-coin": {
|
|
192
|
+
const pieces = piecesOf(scene, question.in);
|
|
193
|
+
const { price: total, reads } = priceOf(scene, question);
|
|
194
|
+
const all = [...readsOf(pieces), ...reads];
|
|
195
|
+
const asked = pieces.filter((p) => Number.isNaN(p.value));
|
|
196
|
+
const left = total - sum(pieces.filter((p) => !Number.isNaN(p.value)));
|
|
197
|
+
const each = left / asked.length;
|
|
198
|
+
if (!Number.isInteger(each) || each < 1)
|
|
199
|
+
return unsolved(`the money shown is worth ${total - left}, and the rest of ${total}, ${left}, is no whole worth from 1 for ${asked.length === 1 ? "the piece" : `each of ${asked.length} pieces`} shown with ?`, all);
|
|
200
|
+
return solved({ number: each }, all);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The facts of a scene of coins are its coins and notes whose value shows,
|
|
206
|
+
* each item with its count, and its goods whose price is a whole number.
|
|
207
|
+
*/
|
|
208
|
+
function facts(scene) {
|
|
209
|
+
return [
|
|
210
|
+
...groupsOf(scene).flatMap((g, i) => g.items.flatMap((item, j) => (item.coin ?? item.note) === "?" ? [] : [`groups[${i}].items[${j}]`])),
|
|
211
|
+
...goodsOf(scene).flatMap((g, k) => (whole.test(g.price) ? [`goods[${k}]`] : [])),
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
/** coinsSolver counts the money of a scene of coins: the questions of CoinsQuestion. */
|
|
215
|
+
export const coinsSolver = {
|
|
216
|
+
name: "coins",
|
|
217
|
+
kinds: ["coins"],
|
|
218
|
+
questions: Object.keys(members),
|
|
219
|
+
check,
|
|
220
|
+
facts,
|
|
221
|
+
solve,
|
|
222
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CommandsScene } from "../kinds/commands.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The questions about a program of commands run on a board. The walker
|
|
5
|
+
* starts at its place and runs the program from the left: an arrow moves it
|
|
6
|
+
* one cell that way; step moves it one cell the way it faces, jump two, over
|
|
7
|
+
* the cell ahead and whatever stands on it, and turn-left and turn-right
|
|
8
|
+
* turn it; a repeat runs its commands as many times as its count. No move
|
|
9
|
+
* leaves the board, goes through a wall, or ends on an obstacle; a jump goes
|
|
10
|
+
* through no wall either.
|
|
11
|
+
*
|
|
12
|
+
* - `ends-at`: what stands on the cell where the program stops — the
|
|
13
|
+
* letter of a mark, as a text, or the target or a thing, as a thing; a
|
|
14
|
+
* cell with nothing on it has no answer. It takes no givens, and refuses a
|
|
15
|
+
* program that breaks a rule of moving, has a "?" or holds an if.
|
|
16
|
+
* - `which-program`: which of the task's picture options, each a commands
|
|
17
|
+
* scene of a program with no board, brings the walker from its start to
|
|
18
|
+
* the target with no move through a wall or onto an obstacle, a letter. An
|
|
19
|
+
* option that leaves the board is refused, and none or two options that
|
|
20
|
+
* bring it there have no answer. The board's scene holds no program of its
|
|
21
|
+
* own. It takes no givens.
|
|
22
|
+
*
|
|
23
|
+
* ends-at reads the start, the program and everything on the board but a
|
|
24
|
+
* drawn path; which-program the start, the target, the obstacles and the
|
|
25
|
+
* walls, so that a mark, a thing or a path on its board is a fact it does
|
|
26
|
+
* not need.
|
|
27
|
+
*/
|
|
28
|
+
export type CommandsQuestion = {
|
|
29
|
+
ask: "ends-at";
|
|
30
|
+
} | {
|
|
31
|
+
ask: "which-program";
|
|
32
|
+
};
|
|
33
|
+
/** The givens of commands: none. */
|
|
34
|
+
export type CommandsGivens = Record<string, never>;
|
|
35
|
+
/** commandsSolver runs programs of commands on a board: the questions of CommandsQuestion. */
|
|
36
|
+
export declare const commandsSolver: Solver<CommandsScene, CommandsQuestion, CommandsGivens>;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { wordOf } from "../things.js";
|
|
2
|
+
import { choicesOf } from "./choice-scenes.js";
|
|
3
|
+
import { letters, noScene, solved, unsolved, } from "./proof.js";
|
|
4
|
+
import { askOf, noGivens, sceneless } from "./question.js";
|
|
5
|
+
const members = { "ends-at": [], "which-program": [] };
|
|
6
|
+
const delta = { up: [-1, 0], right: [0, 1], down: [1, 0], left: [0, -1] };
|
|
7
|
+
const clockwise = ["up", "right", "down", "left"];
|
|
8
|
+
const key = (at) => `${at[0]},${at[1]}`;
|
|
9
|
+
const between = (a, b) => [key(a), key(b)].sort((a, b) => a.localeCompare(b)).join("|");
|
|
10
|
+
const name = (at) => `row ${at[0]}, column ${at[1]}`;
|
|
11
|
+
/**
|
|
12
|
+
* run follows a program from the start on the scene's board, move by move, the path of the
|
|
13
|
+
* program given as at. A move through a wall, or one that ends on an obstacle, is noted as hit
|
|
14
|
+
* and the run goes on, so that a program that leaves the board later is still found; leaving the
|
|
15
|
+
* board, a "?", an if, or a step or a turn when the walker faces no way stop it.
|
|
16
|
+
*/
|
|
17
|
+
function run(scene, program, at) {
|
|
18
|
+
const board = scene.board;
|
|
19
|
+
const walls = new Set((board.walls ?? []).map(([a, b]) => between(a, b)));
|
|
20
|
+
const obstacles = new Set((board.obstacles ?? []).map((o) => key(o.at)));
|
|
21
|
+
let here = scene.start.at;
|
|
22
|
+
let facing = scene.start?.facing;
|
|
23
|
+
let hit;
|
|
24
|
+
const block = (why, path) => {
|
|
25
|
+
hit ??= { why, path };
|
|
26
|
+
};
|
|
27
|
+
const move = (d, cells, path) => {
|
|
28
|
+
for (let k = 0; k < cells; k++) {
|
|
29
|
+
const next = [here[0] + d[0], here[1] + d[1]];
|
|
30
|
+
if (next[0] < 1 || next[0] > board.rows || next[1] < 1 || next[1] > board.cols)
|
|
31
|
+
return `takes the walker off the board from ${name(here)}`;
|
|
32
|
+
if (walls.has(between(here, next)))
|
|
33
|
+
block(`goes through the wall between ${name(here)} and ${name(next)}`, path);
|
|
34
|
+
here = next;
|
|
35
|
+
}
|
|
36
|
+
if (obstacles.has(key(here)))
|
|
37
|
+
block(`ends a move on the obstacle at ${name(here)}`, path);
|
|
38
|
+
return undefined;
|
|
39
|
+
};
|
|
40
|
+
const go = (cmds, path) => {
|
|
41
|
+
for (let i = 0; i < cmds.length; i++) {
|
|
42
|
+
const c = cmds[i];
|
|
43
|
+
const p = `${path}[${i}]`;
|
|
44
|
+
if (typeof c === "string") {
|
|
45
|
+
if (c === "up" || c === "down" || c === "left" || c === "right") {
|
|
46
|
+
const off = move(delta[c], 1, p);
|
|
47
|
+
if (off)
|
|
48
|
+
return { why: off, path: p };
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (!facing)
|
|
52
|
+
return { why: `is ${c}, and the walker faces no way: give start.facing`, path: p };
|
|
53
|
+
if (c === "step" || c === "jump") {
|
|
54
|
+
const off = move(delta[facing], c === "jump" ? 2 : 1, p);
|
|
55
|
+
if (off)
|
|
56
|
+
return { why: off, path: p };
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
facing = clockwise[(clockwise.indexOf(facing) + (c === "turn-right" ? 1 : 3)) % 4];
|
|
60
|
+
}
|
|
61
|
+
else if ("repeat" in c) {
|
|
62
|
+
if (typeof c.repeat !== "number")
|
|
63
|
+
return {
|
|
64
|
+
why: "is a ? for the count, which the program does not show",
|
|
65
|
+
path: `${p}.repeat`,
|
|
66
|
+
};
|
|
67
|
+
for (let k = 0; k < c.repeat; k++) {
|
|
68
|
+
const fault = go(c.do, `${p}.do`);
|
|
69
|
+
if (fault)
|
|
70
|
+
return fault;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if ("if" in c)
|
|
74
|
+
return { why: "is an if, which this solver does not run", path: p };
|
|
75
|
+
else
|
|
76
|
+
return { why: "is a ?, a command the program does not show", path: p };
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
const fault = go(program, at);
|
|
81
|
+
if (fault)
|
|
82
|
+
return { ok: false, ...fault };
|
|
83
|
+
return hit ? { ok: true, at: here, hit } : { ok: true, at: here };
|
|
84
|
+
}
|
|
85
|
+
/** whatIsAt names what stands on a cell of the board: a mark's letter, the target or a thing. */
|
|
86
|
+
function whatIsAt(scene, at) {
|
|
87
|
+
const b = scene.board;
|
|
88
|
+
const mark = (b.marks ?? []).find((m) => key(m.at) === key(at));
|
|
89
|
+
if (mark)
|
|
90
|
+
return { text: mark.label };
|
|
91
|
+
if (b.target && key(b.target.at) === key(at))
|
|
92
|
+
return { thing: b.target.thing };
|
|
93
|
+
const thing = (b.things ?? []).find((t) => key(t.at) === key(at));
|
|
94
|
+
return thing ? { thing: thing.thing } : undefined;
|
|
95
|
+
}
|
|
96
|
+
/** program refuses a picture option that is not a program with no board. */
|
|
97
|
+
function program(o, path, problems) {
|
|
98
|
+
if (o.board !== undefined)
|
|
99
|
+
problems.push(`${path}.board: an option is a program with no board`);
|
|
100
|
+
if (o.start !== undefined)
|
|
101
|
+
problems.push(`${path}.start: the walker starts where the board's scene puts it`);
|
|
102
|
+
if (o.commands === undefined)
|
|
103
|
+
problems.push(`${path}: must hold a program, commands`);
|
|
104
|
+
}
|
|
105
|
+
function check(question, givens, scene, offered) {
|
|
106
|
+
const problems = [];
|
|
107
|
+
const read = askOf(question, members, problems);
|
|
108
|
+
if (!read)
|
|
109
|
+
return problems;
|
|
110
|
+
noGivens(givens, problems);
|
|
111
|
+
const options = read.ask === "which-program" ? choicesOf(offered, read.ask, "commands", program, problems) : [];
|
|
112
|
+
if (!scene) {
|
|
113
|
+
sceneless(read.ask, "a scene of commands, the board", problems);
|
|
114
|
+
return problems;
|
|
115
|
+
}
|
|
116
|
+
if (!scene.board) {
|
|
117
|
+
problems.push(`board: ${read.ask} runs a program on a board, and the scene has none`);
|
|
118
|
+
return problems;
|
|
119
|
+
}
|
|
120
|
+
if (read.ask === "ends-at") {
|
|
121
|
+
if (!scene.commands)
|
|
122
|
+
problems.push("commands: ends-at runs the scene's program, and it has none");
|
|
123
|
+
else {
|
|
124
|
+
const r = run(scene, scene.commands, "commands");
|
|
125
|
+
const fault = r.ok ? r.hit : r;
|
|
126
|
+
if (fault)
|
|
127
|
+
problems.push(`${fault.path}: ${fault.why}`);
|
|
128
|
+
}
|
|
129
|
+
return problems;
|
|
130
|
+
}
|
|
131
|
+
if (!scene.board.target)
|
|
132
|
+
problems.push("board.target: which-program brings the walker to the target, and there is none");
|
|
133
|
+
if (scene.commands)
|
|
134
|
+
problems.push("commands: which-program runs the options' programs; the board's scene holds none");
|
|
135
|
+
if (problems.length > 0)
|
|
136
|
+
return problems;
|
|
137
|
+
options.forEach((o, i) => {
|
|
138
|
+
const r = run(scene, o.commands, `choices[${i}].commands`);
|
|
139
|
+
if (!r.ok)
|
|
140
|
+
problems.push(`${r.path}: ${r.why}`);
|
|
141
|
+
});
|
|
142
|
+
return problems;
|
|
143
|
+
}
|
|
144
|
+
/** safeEnd is where a program stops when no wall and no obstacle blocks it on the way. */
|
|
145
|
+
function safeEnd(scene, program) {
|
|
146
|
+
const r = run(scene, program, "commands");
|
|
147
|
+
return r.ok && !r.hit ? r.at : undefined;
|
|
148
|
+
}
|
|
149
|
+
function solve(scene, question, _givens, offered) {
|
|
150
|
+
if (!scene)
|
|
151
|
+
return noScene;
|
|
152
|
+
const b = scene.board;
|
|
153
|
+
const listed = (member) => (b[member] ?? []).map((_, i) => `board.${member}[${i}]`);
|
|
154
|
+
if (question.ask === "ends-at") {
|
|
155
|
+
const reads = [
|
|
156
|
+
"start",
|
|
157
|
+
"commands",
|
|
158
|
+
...listed("obstacles"),
|
|
159
|
+
...listed("walls"),
|
|
160
|
+
...listed("marks"),
|
|
161
|
+
...(b.target ? ["board.target"] : []),
|
|
162
|
+
...listed("things"),
|
|
163
|
+
];
|
|
164
|
+
const end = safeEnd(scene, scene.commands);
|
|
165
|
+
const there = whatIsAt(scene, end);
|
|
166
|
+
if (!there)
|
|
167
|
+
return unsolved(`the program stops at ${name(end)}, where nothing stands`, reads);
|
|
168
|
+
return solved(there, reads);
|
|
169
|
+
}
|
|
170
|
+
const reads = ["start", "board.target", ...listed("obstacles"), ...listed("walls")];
|
|
171
|
+
const target = b.target;
|
|
172
|
+
const good = (offered?.choices ?? []).flatMap((o, i) => {
|
|
173
|
+
const end = safeEnd(scene, o.commands);
|
|
174
|
+
return end && key(end) === key(target.at) ? [letters[i]] : [];
|
|
175
|
+
});
|
|
176
|
+
if (good.length === 1)
|
|
177
|
+
return solved({ letter: good[0] }, reads);
|
|
178
|
+
const goal = wordOf(target.thing);
|
|
179
|
+
if (good.length === 0)
|
|
180
|
+
return unsolved(`no option brings the walker to the ${goal}`, reads);
|
|
181
|
+
return unsolved(`options ${good.join(" and ")} all bring the walker to the ${goal}`, reads);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The facts of a scene of commands are the walker's start, each command of
|
|
185
|
+
* its program, and what stands on its board: the target, each obstacle,
|
|
186
|
+
* thing, mark and wall, and a drawn path.
|
|
187
|
+
*/
|
|
188
|
+
function facts(scene) {
|
|
189
|
+
const b = scene.board;
|
|
190
|
+
const listed = (member) => (b?.[member] ?? []).map((_, i) => `board.${member}[${i}]`);
|
|
191
|
+
return [
|
|
192
|
+
...(scene.start ? ["start"] : []),
|
|
193
|
+
...(scene.commands ?? []).map((_, i) => `commands[${i}]`),
|
|
194
|
+
...(b?.target ? ["board.target"] : []),
|
|
195
|
+
...listed("obstacles"),
|
|
196
|
+
...listed("things"),
|
|
197
|
+
...listed("marks"),
|
|
198
|
+
...listed("walls"),
|
|
199
|
+
...(b?.path ? ["board.path"] : []),
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
/** commandsSolver runs programs of commands on a board: the questions of CommandsQuestion. */
|
|
203
|
+
export const commandsSolver = {
|
|
204
|
+
name: "commands",
|
|
205
|
+
kinds: ["commands"],
|
|
206
|
+
questions: Object.keys(members),
|
|
207
|
+
check,
|
|
208
|
+
facts,
|
|
209
|
+
solve,
|
|
210
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type CountfigScene } from "../kinds/countfig.ts";
|
|
2
|
+
import { type DotgridScene } from "../kinds/dotgrid.ts";
|
|
3
|
+
import type { GridScene } from "../kinds/grid.ts";
|
|
4
|
+
import { type Solver } from "./proof.ts";
|
|
5
|
+
/**
|
|
6
|
+
* The questions about a figure, each how many shapes of one sort it holds,
|
|
7
|
+
* a number. It takes no givens.
|
|
8
|
+
*
|
|
9
|
+
* - `count-triangles`, `count-squares`, `count-rectangles`, `count-segments`.
|
|
10
|
+
*
|
|
11
|
+
* A figure is a scene of one of three kinds:
|
|
12
|
+
*
|
|
13
|
+
* - countfig, points and segments: the count is the one the kind checks a
|
|
14
|
+
* scene's facts by — the triangles, squares and rectangles whose sides lie
|
|
15
|
+
* along the lines of the figure, with corners at its points, a square a
|
|
16
|
+
* rectangle too, and the segments between two points along its lines. It
|
|
17
|
+
* reads every segment and every point on a segment; a point on none is no
|
|
18
|
+
* corner of any shape, so a picture that shows one shows a fact the count
|
|
19
|
+
* does not need. A shaded shape is read when it is of the sort counted —
|
|
20
|
+
* three corners for triangles, four for squares and rectangles —, an
|
|
21
|
+
* example of what to count.
|
|
22
|
+
* - grid, a figure of cells, "" or a colour for a cell of the figure and
|
|
23
|
+
* null for a place left out of it: the squares are the blocks of n by n
|
|
24
|
+
* cells all on the figure, the rectangles every block of cells all on it,
|
|
25
|
+
* a square too. It counts no triangles and no segments, and reads every
|
|
26
|
+
* cell of the figure. A grid with walls, doors, a path, labels, or more in
|
|
27
|
+
* a cell than a colour, is no figure to count, nor one with a place left
|
|
28
|
+
* out inside it, whose outline the lines draw as a square of its own.
|
|
29
|
+
* - dotgrid, shapes on dot paper or rubber bands on a geoboard: each shape
|
|
30
|
+
* counts by its corners — three make a triangle, four with right angles a
|
|
31
|
+
* rectangle, and with equal sides too a square, turned or not. It counts
|
|
32
|
+
* no segments, and reads every shape. Shapes that touch or cross make
|
|
33
|
+
* figures of their lines, so they are refused.
|
|
34
|
+
*/
|
|
35
|
+
export type CountfigQuestion = {
|
|
36
|
+
ask: "count-triangles" | "count-squares" | "count-rectangles" | "count-segments";
|
|
37
|
+
};
|
|
38
|
+
/** A figure is a scene the solver counts the shapes of. */
|
|
39
|
+
export type Figure = CountfigScene | GridScene | DotgridScene;
|
|
40
|
+
/** countfigSolver counts the shapes of a figure: the questions of CountfigQuestion. */
|
|
41
|
+
export declare const countfigSolver: Solver<Figure, CountfigQuestion, Record<string, never>>;
|