@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,199 @@
|
|
|
1
|
+
import { countShapes, lonePoints } from "../kinds/countfig.js";
|
|
2
|
+
import { cornersOf } from "../kinds/dotgrid.js";
|
|
3
|
+
import { distance, dot, edgesOf, meet, overlap, sub } from "../kinds/plane-parts.js";
|
|
4
|
+
import { isObj } from "../read.js";
|
|
5
|
+
import { noScene, solved } from "./proof.js";
|
|
6
|
+
import { askOf, noGivens, sceneless } from "./question.js";
|
|
7
|
+
const sorts = {
|
|
8
|
+
"count-triangles": "triangles",
|
|
9
|
+
"count-squares": "squares",
|
|
10
|
+
"count-rectangles": "rectangles",
|
|
11
|
+
"count-segments": "segments",
|
|
12
|
+
};
|
|
13
|
+
const members = {
|
|
14
|
+
"count-triangles": [],
|
|
15
|
+
"count-squares": [],
|
|
16
|
+
"count-rectangles": [],
|
|
17
|
+
"count-segments": [],
|
|
18
|
+
};
|
|
19
|
+
/** What a grid that is a figure of cells may not carry. */
|
|
20
|
+
const gridExtras = [
|
|
21
|
+
"walls",
|
|
22
|
+
"doors",
|
|
23
|
+
"path",
|
|
24
|
+
"rowLabels",
|
|
25
|
+
"colLabels",
|
|
26
|
+
"rightLabels",
|
|
27
|
+
"bottomLabels",
|
|
28
|
+
];
|
|
29
|
+
const steps = [
|
|
30
|
+
[1, 0],
|
|
31
|
+
[-1, 0],
|
|
32
|
+
[0, 1],
|
|
33
|
+
[0, -1],
|
|
34
|
+
];
|
|
35
|
+
/** holes are the places left out of a grid that no way through places left out joins to its edge. */
|
|
36
|
+
function holes(cells) {
|
|
37
|
+
const rows = cells.length;
|
|
38
|
+
const cols = cells[0].length;
|
|
39
|
+
const open = new Set();
|
|
40
|
+
const queue = [];
|
|
41
|
+
const reach = (r, c) => {
|
|
42
|
+
if (r < 0 || c < 0 || r >= rows || c >= cols || cells[r][c] !== null)
|
|
43
|
+
return;
|
|
44
|
+
if (open.has(`${r},${c}`))
|
|
45
|
+
return;
|
|
46
|
+
open.add(`${r},${c}`);
|
|
47
|
+
queue.push([r, c]);
|
|
48
|
+
};
|
|
49
|
+
for (let r = 0; r < rows; r++)
|
|
50
|
+
for (let c = 0; c < cols; c++)
|
|
51
|
+
if (r === 0 || c === 0 || r === rows - 1 || c === cols - 1)
|
|
52
|
+
reach(r, c);
|
|
53
|
+
while (queue.length > 0) {
|
|
54
|
+
const [r, c] = queue.pop();
|
|
55
|
+
for (const [dr, dc] of steps)
|
|
56
|
+
reach(r + dr, c + dc);
|
|
57
|
+
}
|
|
58
|
+
return cells.flatMap((row, r) => row.flatMap((cell, c) => cell === null && !open.has(`${r},${c}`) ? [[r, c]] : []));
|
|
59
|
+
}
|
|
60
|
+
function checkGrid(g, sort, problems) {
|
|
61
|
+
if (sort !== "squares" && sort !== "rectangles")
|
|
62
|
+
problems.push("question.ask: a figure of cells is counted by its squares or its rectangles");
|
|
63
|
+
for (const extra of gridExtras)
|
|
64
|
+
if (g[extra] !== undefined)
|
|
65
|
+
problems.push(`${extra}: a figure of cells to count has none; leave it out`);
|
|
66
|
+
g.cells.forEach((row, r) => {
|
|
67
|
+
row.forEach((cell, c) => {
|
|
68
|
+
const bare = cell === "" || (isObj(cell) && Object.keys(cell).every((k) => k === "fill"));
|
|
69
|
+
if (cell !== null && !bare)
|
|
70
|
+
problems.push(`cells[${r}][${c}]: a figure of cells to count shows nothing in its cells but a colour; write ""`);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
for (const [r, c] of holes(g.cells))
|
|
74
|
+
problems.push(`cells[${r}][${c}]: a place left out inside the figure, whose outline the lines draw as a square; make it a cell, "", or open it to the edge`);
|
|
75
|
+
}
|
|
76
|
+
/** touching names two shapes whose outlines meet, or one of which holds the other, if any do. */
|
|
77
|
+
function touching(places) {
|
|
78
|
+
for (let i = 0; i < places.length; i++)
|
|
79
|
+
for (let j = 0; j < i; j++) {
|
|
80
|
+
const a = places[i];
|
|
81
|
+
const b = places[j];
|
|
82
|
+
const meets = edgesOf(a).some(([p, q]) => edgesOf(b).some(([r, s]) => meet(p, q, r, s, 1e-9).how !== "apart"));
|
|
83
|
+
if (meets || overlap(a, b))
|
|
84
|
+
return [j, i];
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
function checkDotgrid(d, sort, problems) {
|
|
89
|
+
if (sort === "segments")
|
|
90
|
+
problems.push("question.ask: shapes on dot paper count no segments");
|
|
91
|
+
const shapes = d.shapes ?? [];
|
|
92
|
+
if (shapes.length === 0)
|
|
93
|
+
problems.push("shapes: the dot paper shows no shape to count");
|
|
94
|
+
const pair = touching(shapes.map((s) => cornersOf(d, s)));
|
|
95
|
+
if (pair)
|
|
96
|
+
problems.push(`shapes[${pair[1]}]: touches or crosses shapes[${pair[0]}], so their lines make figures of their own; keep the shapes apart`);
|
|
97
|
+
}
|
|
98
|
+
function check(question, givens, scene) {
|
|
99
|
+
const problems = [];
|
|
100
|
+
noGivens(givens, problems);
|
|
101
|
+
const read = askOf(question, members, problems);
|
|
102
|
+
if (!read)
|
|
103
|
+
return problems;
|
|
104
|
+
if (!scene) {
|
|
105
|
+
sceneless(read.ask, "a figure: a scene of countfig, grid or dotgrid", problems);
|
|
106
|
+
return problems;
|
|
107
|
+
}
|
|
108
|
+
const sort = sorts[read.ask];
|
|
109
|
+
if (scene.kind === "grid")
|
|
110
|
+
checkGrid(scene, sort, problems);
|
|
111
|
+
if (scene.kind === "dotgrid")
|
|
112
|
+
checkDotgrid(scene, sort, problems);
|
|
113
|
+
return problems;
|
|
114
|
+
}
|
|
115
|
+
/** gridCount counts the blocks of cells all on the figure: every one, or the square ones. */
|
|
116
|
+
function gridCount(g, squares) {
|
|
117
|
+
const rows = g.cells.length;
|
|
118
|
+
const cols = g.cells[0].length;
|
|
119
|
+
const whole = (r, c, h, w) => {
|
|
120
|
+
for (let i = r; i < r + h; i++)
|
|
121
|
+
for (let j = c; j < c + w; j++)
|
|
122
|
+
if (g.cells[i][j] === null)
|
|
123
|
+
return false;
|
|
124
|
+
return true;
|
|
125
|
+
};
|
|
126
|
+
let count = 0;
|
|
127
|
+
for (let h = 1; h <= rows; h++)
|
|
128
|
+
for (let w = 1; w <= cols; w++) {
|
|
129
|
+
if (squares && h !== w)
|
|
130
|
+
continue;
|
|
131
|
+
for (let r = 0; r + h <= rows; r++)
|
|
132
|
+
for (let c = 0; c + w <= cols; c++)
|
|
133
|
+
if (whole(r, c, h, w))
|
|
134
|
+
count++;
|
|
135
|
+
}
|
|
136
|
+
return count;
|
|
137
|
+
}
|
|
138
|
+
/** sortOf says what a shape of dot paper is by its corners: a square is not a rectangle here. */
|
|
139
|
+
function sortOf(corners) {
|
|
140
|
+
if (corners.length === 3)
|
|
141
|
+
return "triangles";
|
|
142
|
+
if (corners.length !== 4)
|
|
143
|
+
return undefined;
|
|
144
|
+
const sides = edgesOf(corners).map(([a, b]) => sub(b, a));
|
|
145
|
+
const right = sides.every((u, i) => {
|
|
146
|
+
const v = sides[(i + 1) % 4];
|
|
147
|
+
return Math.abs(dot(u, v)) <= 1e-9 * Math.hypot(u.x, u.y) * Math.hypot(v.x, v.y);
|
|
148
|
+
});
|
|
149
|
+
if (!right)
|
|
150
|
+
return undefined;
|
|
151
|
+
const [a, b, c] = corners;
|
|
152
|
+
return Math.abs(distance(a, b) - distance(b, c)) <= 1e-9 ? "squares" : "rectangles";
|
|
153
|
+
}
|
|
154
|
+
function solve(scene, question) {
|
|
155
|
+
if (!scene)
|
|
156
|
+
return noScene;
|
|
157
|
+
const sort = sorts[question.ask];
|
|
158
|
+
if (scene.kind === "grid")
|
|
159
|
+
return solved({ number: gridCount(scene, sort === "squares") }, facts(scene));
|
|
160
|
+
if (scene.kind === "dotgrid") {
|
|
161
|
+
const found = (scene.shapes ?? []).map((s) => sortOf(cornersOf(scene, s)));
|
|
162
|
+
const n = found.filter((f) => f === sort || (sort === "rectangles" && f === "squares")).length;
|
|
163
|
+
return solved({ number: n }, facts(scene));
|
|
164
|
+
}
|
|
165
|
+
const lone = lonePoints(scene);
|
|
166
|
+
const reads = [
|
|
167
|
+
...scene.points.flatMap((_, i) => (lone[i] ? [] : [`points[${i}]`])),
|
|
168
|
+
...scene.segments.map((_, j) => `segments[${j}]`),
|
|
169
|
+
];
|
|
170
|
+
const corners = scene.highlight?.corners.length;
|
|
171
|
+
if ((sort === "triangles" && corners === 3) ||
|
|
172
|
+
((sort === "squares" || sort === "rectangles") && corners === 4))
|
|
173
|
+
reads.push("highlight");
|
|
174
|
+
return solved({ number: countShapes(scene)[sort] }, reads);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The facts of a figure: of countfig, its points, its segments and its
|
|
178
|
+
* shaded shape; of a grid, the cells of the figure; of dot paper, its shapes.
|
|
179
|
+
*/
|
|
180
|
+
function facts(scene) {
|
|
181
|
+
if (scene.kind === "grid")
|
|
182
|
+
return scene.cells.flatMap((row, r) => row.flatMap((cell, c) => (cell === null ? [] : [`cells[${r}][${c}]`])));
|
|
183
|
+
if (scene.kind === "dotgrid")
|
|
184
|
+
return (scene.shapes ?? []).map((_, i) => `shapes[${i}]`);
|
|
185
|
+
return [
|
|
186
|
+
...scene.points.map((_, i) => `points[${i}]`),
|
|
187
|
+
...scene.segments.map((_, j) => `segments[${j}]`),
|
|
188
|
+
...(scene.highlight ? ["highlight"] : []),
|
|
189
|
+
];
|
|
190
|
+
}
|
|
191
|
+
/** countfigSolver counts the shapes of a figure: the questions of CountfigQuestion. */
|
|
192
|
+
export const countfigSolver = {
|
|
193
|
+
name: "countfig",
|
|
194
|
+
kinds: ["countfig", "grid", "dotgrid"],
|
|
195
|
+
questions: Object.keys(members),
|
|
196
|
+
check,
|
|
197
|
+
facts,
|
|
198
|
+
solve,
|
|
199
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type CubesScene } from "../kinds/cubes.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/** A structure of a scene of cubes, by its index in structures or by its label. */
|
|
4
|
+
export type StructureRef = number | string;
|
|
5
|
+
/**
|
|
6
|
+
* Where the statement says the cubes stand: "row", in the one row of the
|
|
7
|
+
* picture; or the squares of the table the building stands on, each [x, y]
|
|
8
|
+
* as a cube's at has them — x along a row from the left, y the row from the
|
|
9
|
+
* back —, each holding at least one cube and none standing elsewhere.
|
|
10
|
+
*/
|
|
11
|
+
export type Ground = "row" | [number, number][];
|
|
12
|
+
/**
|
|
13
|
+
* The questions about a building of cubes, a number each.
|
|
14
|
+
*
|
|
15
|
+
* - `how-many-cubes`: how many cubes the structure `of` holds, or every
|
|
16
|
+
* structure without it, the hidden cubes it must have included; the givens
|
|
17
|
+
* say where the cubes stand, `{"on": "row"}` or `{"on": [[0, 0], [1, 0]]}`.
|
|
18
|
+
* Every cube stands on the table or on a cube, as the statement says. The
|
|
19
|
+
* solver tries every building on that ground that the picture shows the
|
|
20
|
+
* same, face for face, and the question is refused unless all of them hold
|
|
21
|
+
* the same number of cubes: a place where a cube could hide unseen, or a
|
|
22
|
+
* hidden cube that holds nothing up, leaves the count open. A cube hidden
|
|
23
|
+
* under a cube in sight is proved, for that cube stands on it. The search
|
|
24
|
+
* tries at most 100 000 buildings, the heights of the squares together,
|
|
25
|
+
* and refuses a building that needs more.
|
|
26
|
+
* - `cubes-to-complete`: how many cubes to add to the structure `of` to make
|
|
27
|
+
* a wall, one row of cubes all of one height: of the givens' length and
|
|
28
|
+
* height, `{"length": 3, "height": 3}`, or as high and as long as the
|
|
29
|
+
* structure `to`, a wall drawn beside it, which takes no givens. The
|
|
30
|
+
* building stands in one row, as the statement says, and fits in the wall.
|
|
31
|
+
*
|
|
32
|
+
* A structure's marks are facts no count reads, and its colours carry none.
|
|
33
|
+
*/
|
|
34
|
+
export type CubesQuestion = {
|
|
35
|
+
ask: "how-many-cubes";
|
|
36
|
+
of?: StructureRef;
|
|
37
|
+
} | {
|
|
38
|
+
ask: "cubes-to-complete";
|
|
39
|
+
of?: StructureRef;
|
|
40
|
+
to?: StructureRef;
|
|
41
|
+
};
|
|
42
|
+
/** The givens of a question of cubes: where the cubes stand, or the size of the wall. */
|
|
43
|
+
export type CubesGivens = {
|
|
44
|
+
on?: Ground;
|
|
45
|
+
length?: number;
|
|
46
|
+
height?: number;
|
|
47
|
+
};
|
|
48
|
+
/** cubesSolver counts the cubes of a building: the questions of CubesQuestion. */
|
|
49
|
+
export declare const cubesSolver: Solver<CubesScene, CubesQuestion, CubesGivens>;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { cubesOf, faces, pictureOf, triangles, } from "../kinds/cubes.js";
|
|
2
|
+
import { int, isObj, list, only } from "../read.js";
|
|
3
|
+
import { noScene, solved } from "./proof.js";
|
|
4
|
+
import { askOf, refOf, sceneless } from "./question.js";
|
|
5
|
+
const members = {
|
|
6
|
+
"how-many-cubes": ["of"],
|
|
7
|
+
"cubes-to-complete": ["of", "to"],
|
|
8
|
+
};
|
|
9
|
+
/** The highest place a cube may have, as the kind's check allows: coordinates from 0 to 9. */
|
|
10
|
+
const reach = 9;
|
|
11
|
+
const most = 100_000;
|
|
12
|
+
function samePicture(a, b) {
|
|
13
|
+
if (a.size !== b.size)
|
|
14
|
+
return false;
|
|
15
|
+
for (const [t, seen] of a)
|
|
16
|
+
if (b.get(t)?.face !== seen.face)
|
|
17
|
+
return false;
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
const at = (c) => `${c.x},${c.y},${c.z}`;
|
|
21
|
+
/** floating is the first cube that stands neither on the table nor on a cube, if one does. */
|
|
22
|
+
function floating(cubes) {
|
|
23
|
+
const taken = new Set(cubes.map(at));
|
|
24
|
+
return cubes.findIndex((c) => c.z > 0 && !taken.has(at({ ...c, z: c.z - 1 })));
|
|
25
|
+
}
|
|
26
|
+
/** squaresOf lists the squares a ground names for a structure; "row" is the row of its cubes. */
|
|
27
|
+
function squaresOf(cubes, on) {
|
|
28
|
+
if (on !== "row")
|
|
29
|
+
return on;
|
|
30
|
+
const y = cubes[0].y;
|
|
31
|
+
return Array.from({ length: reach + 1 }, (_, x) => [x, y]);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* counts lists the numbers of cubes of every building on the ground that
|
|
35
|
+
* the picture shows as it shows the structure, each cube on the table or on
|
|
36
|
+
* a cube; undefined when there are more than most buildings to try. Every
|
|
37
|
+
* cube of such a building covers only triangles the structure covers, which
|
|
38
|
+
* bounds the height of each square.
|
|
39
|
+
*/
|
|
40
|
+
function counts(cubes, on) {
|
|
41
|
+
const picture = pictureOf(cubes);
|
|
42
|
+
const squares = squaresOf(cubes, on);
|
|
43
|
+
const least = on === "row" ? 0 : 1;
|
|
44
|
+
const inside = (p) => faces.every((f) => triangles(p, f).every((t) => picture.has(t)));
|
|
45
|
+
const tops = squares.map(([x, y]) => {
|
|
46
|
+
let h = 0;
|
|
47
|
+
while (h <= reach && inside({ x, y, z: h }))
|
|
48
|
+
h++;
|
|
49
|
+
return h;
|
|
50
|
+
});
|
|
51
|
+
let ways = 1;
|
|
52
|
+
for (const top of tops) {
|
|
53
|
+
ways *= Math.max(0, top - least + 1);
|
|
54
|
+
if (ways > most)
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const found = new Set();
|
|
58
|
+
const heights = tops.map(() => least);
|
|
59
|
+
if (tops.some((top) => top < least))
|
|
60
|
+
return [];
|
|
61
|
+
for (;;) {
|
|
62
|
+
const building = squares.flatMap(([x, y], i) => Array.from({ length: heights[i] }, (_, z) => ({ x, y, z })));
|
|
63
|
+
if (samePicture(pictureOf(building), picture))
|
|
64
|
+
found.add(building.length);
|
|
65
|
+
let i = 0;
|
|
66
|
+
while (i < heights.length && heights[i] === tops[i])
|
|
67
|
+
heights[i++] = least;
|
|
68
|
+
if (i === heights.length)
|
|
69
|
+
break;
|
|
70
|
+
heights[i] += 1;
|
|
71
|
+
}
|
|
72
|
+
return [...found].sort((a, b) => a - b);
|
|
73
|
+
}
|
|
74
|
+
function checkGround(on, cubes, path, problems) {
|
|
75
|
+
if (on === "row") {
|
|
76
|
+
if (cubes && new Set(cubes.map((c) => c.y)).size > 1)
|
|
77
|
+
problems.push(`${path}: the cubes stand in more than one row, not "row"`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(on)) {
|
|
81
|
+
problems.push(`${path}: must be "row", or the squares the building stands on, as [[0, 0], [1, 0]]`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const before = problems.length;
|
|
85
|
+
const named = new Set();
|
|
86
|
+
list(on, path, problems, 1, 20).forEach((sq, i) => {
|
|
87
|
+
const p = `${path}[${i}]`;
|
|
88
|
+
if (!Array.isArray(sq) || sq.length !== 2) {
|
|
89
|
+
problems.push(`${p}: must be [x, y], a square of the table as a cube's at has it`);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const x = int(sq[0], `${p}[0]`, problems, 0, reach);
|
|
93
|
+
const y = int(sq[1], `${p}[1]`, problems, 0, reach);
|
|
94
|
+
if (named.has(`${x},${y}`))
|
|
95
|
+
problems.push(`${p}: is named already`);
|
|
96
|
+
named.add(`${x},${y}`);
|
|
97
|
+
});
|
|
98
|
+
if (!cubes || problems.length > before)
|
|
99
|
+
return;
|
|
100
|
+
const used = new Set(cubes.map((c) => `${c.x},${c.y}`));
|
|
101
|
+
for (const sq of used)
|
|
102
|
+
if (!named.has(sq))
|
|
103
|
+
problems.push(`${path}: a cube stands on [${sq}], which it does not name`);
|
|
104
|
+
for (const sq of named)
|
|
105
|
+
if (!used.has(sq))
|
|
106
|
+
problems.push(`${path}: no cube stands on [${sq}]`);
|
|
107
|
+
}
|
|
108
|
+
const labelsOf = (scene) => scene.structures.map((st) => st.label);
|
|
109
|
+
const shapeOf = (scene, i) => `structures[${i}].${scene.structures[i].heights ? "heights" : "cubes"}`;
|
|
110
|
+
/** builtOf is the structure a question names, or every structure; undefined for a bad reference. */
|
|
111
|
+
function builtOf(scene, ref, problems) {
|
|
112
|
+
if (ref === undefined)
|
|
113
|
+
return scene.structures.map((st, index) => ({ index, cubes: cubesOf(st) }));
|
|
114
|
+
const index = refOf(ref, labelsOf(scene), "structures", "question.of", problems);
|
|
115
|
+
return index === undefined ? undefined : [{ index, cubes: cubesOf(scene.structures[index]) }];
|
|
116
|
+
}
|
|
117
|
+
/** wall reads a structure as a wall: one row of cubes, every place filled to one height. */
|
|
118
|
+
function wallOf(cubes) {
|
|
119
|
+
if (new Set(cubes.map((c) => c.y)).size > 1)
|
|
120
|
+
return undefined;
|
|
121
|
+
const xs = cubes.map((c) => c.x);
|
|
122
|
+
const length = Math.max(...xs) - Math.min(...xs) + 1;
|
|
123
|
+
const height = Math.max(...cubes.map((c) => c.z)) + 1;
|
|
124
|
+
return cubes.length === length * height ? { length, height } : undefined;
|
|
125
|
+
}
|
|
126
|
+
function checkHowMany(scene, q, givens, problems) {
|
|
127
|
+
const read = builtOf(scene, q.of, problems);
|
|
128
|
+
if (!read)
|
|
129
|
+
return;
|
|
130
|
+
for (const { index, cubes } of read) {
|
|
131
|
+
const path = `structures[${index}]`;
|
|
132
|
+
const off = floating(cubes);
|
|
133
|
+
if (off >= 0) {
|
|
134
|
+
const c = cubes[off];
|
|
135
|
+
problems.push(`${path}: the cube at [${c.x}, ${c.y}, ${c.z}] stands on no cube; the count holds each cube on the table or on a cube`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const before = problems.length;
|
|
139
|
+
checkGround(givens.on, cubes, "givens.on", problems);
|
|
140
|
+
if (problems.length > before)
|
|
141
|
+
continue;
|
|
142
|
+
const found = counts(cubes, givens.on);
|
|
143
|
+
if (found === undefined)
|
|
144
|
+
problems.push(`${path}: on that ground the picture leaves more than ${most} buildings to try; name fewer squares, or build it lower`);
|
|
145
|
+
else if (found.length > 1)
|
|
146
|
+
problems.push(`${path}: a cube could hide there, or a hidden one be missing: on that ground the picture shows ${found.join(" or ")} cubes alike; build it so that every place is in sight, or a hidden cube holds one up`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function checkComplete(scene, q, givens, problems) {
|
|
150
|
+
const labels = labelsOf(scene);
|
|
151
|
+
const to = q.to === undefined ? undefined : refOf(q.to, labels, "structures", "question.to", problems);
|
|
152
|
+
let of;
|
|
153
|
+
if (q.of !== undefined)
|
|
154
|
+
of = refOf(q.of, labels, "structures", "question.of", problems);
|
|
155
|
+
else {
|
|
156
|
+
const rest = scene.structures.map((_, i) => i).filter((i) => i !== to);
|
|
157
|
+
if (rest.length === 1)
|
|
158
|
+
of = rest[0];
|
|
159
|
+
else
|
|
160
|
+
problems.push("question.of: name the structure to complete");
|
|
161
|
+
}
|
|
162
|
+
if (q.to !== undefined && to === undefined)
|
|
163
|
+
return;
|
|
164
|
+
if (of === undefined)
|
|
165
|
+
return;
|
|
166
|
+
if (of === to)
|
|
167
|
+
problems.push("question.to: is the structure to complete itself");
|
|
168
|
+
const cubes = cubesOf(scene.structures[of]);
|
|
169
|
+
if (new Set(cubes.map((c) => c.y)).size > 1)
|
|
170
|
+
problems.push(`structures[${of}]: stands on more than one row; a wall is one row of cubes`);
|
|
171
|
+
let size;
|
|
172
|
+
if (to !== undefined) {
|
|
173
|
+
if (givens !== undefined)
|
|
174
|
+
problems.push("givens: the wall drawn as question.to gives its size; give no givens");
|
|
175
|
+
size = wallOf(cubesOf(scene.structures[to]));
|
|
176
|
+
if (!size)
|
|
177
|
+
problems.push(`structures[${to}]: is no wall: one row of cubes, every place filled to one height`);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const g = givens ?? {};
|
|
181
|
+
const length = int(g.length, "givens.length", problems, 1, reach + 1);
|
|
182
|
+
const height = int(g.height, "givens.height", problems, 1, reach + 1);
|
|
183
|
+
size = { length, height };
|
|
184
|
+
}
|
|
185
|
+
if (!size || problems.length > 0)
|
|
186
|
+
return;
|
|
187
|
+
const xs = cubes.map((c) => c.x);
|
|
188
|
+
if (Math.max(...xs) - Math.min(...xs) + 1 > size.length)
|
|
189
|
+
problems.push(`structures[${of}]: is longer than the wall, ${size.length} cubes`);
|
|
190
|
+
if (Math.max(...cubes.map((c) => c.z)) + 1 > size.height)
|
|
191
|
+
problems.push(`structures[${of}]: is higher than the wall, ${size.height} cubes`);
|
|
192
|
+
}
|
|
193
|
+
function check(question, givens, scene) {
|
|
194
|
+
const problems = [];
|
|
195
|
+
const read = askOf(question, members, problems);
|
|
196
|
+
if (givens !== undefined && !isObj(givens)) {
|
|
197
|
+
problems.push("givens: must be an object");
|
|
198
|
+
return problems;
|
|
199
|
+
}
|
|
200
|
+
const g = isObj(givens) ? givens : undefined;
|
|
201
|
+
if (!read)
|
|
202
|
+
return problems;
|
|
203
|
+
const { q, ask } = read;
|
|
204
|
+
if (!scene)
|
|
205
|
+
sceneless(ask, "a scene of cubes, the building", problems);
|
|
206
|
+
if (ask === "how-many-cubes") {
|
|
207
|
+
if (g)
|
|
208
|
+
only(g, ["on"], "givens", problems, "the givens of how-many-cubes");
|
|
209
|
+
if (g?.on === undefined)
|
|
210
|
+
problems.push('givens.on: say where the cubes stand, "row" or the squares of the table, as the statement does');
|
|
211
|
+
else if (!scene)
|
|
212
|
+
checkGround(g.on, undefined, "givens.on", problems);
|
|
213
|
+
if (scene && problems.length === 0)
|
|
214
|
+
checkHowMany(scene, q, g, problems);
|
|
215
|
+
return problems;
|
|
216
|
+
}
|
|
217
|
+
if (g)
|
|
218
|
+
only(g, ["length", "height"], "givens", problems, "the givens of cubes-to-complete");
|
|
219
|
+
if (scene && problems.length === 0)
|
|
220
|
+
checkComplete(scene, q, g, problems);
|
|
221
|
+
return problems;
|
|
222
|
+
}
|
|
223
|
+
function solve(scene, question, givens = {}) {
|
|
224
|
+
if (!scene)
|
|
225
|
+
return noScene;
|
|
226
|
+
const labels = labelsOf(scene);
|
|
227
|
+
const index = (ref) => (typeof ref === "number" ? ref : labels.indexOf(ref));
|
|
228
|
+
if (question.ask === "how-many-cubes") {
|
|
229
|
+
const read = question.of === undefined ? scene.structures.map((_, i) => i) : [index(question.of)];
|
|
230
|
+
const total = read.reduce((sum, i) => sum + cubesOf(scene.structures[i]).length, 0);
|
|
231
|
+
return solved({ number: total }, read.map((i) => shapeOf(scene, i)));
|
|
232
|
+
}
|
|
233
|
+
const to = question.to === undefined ? undefined : index(question.to);
|
|
234
|
+
const of = question.of === undefined ? scene.structures.findIndex((_, i) => i !== to) : index(question.of);
|
|
235
|
+
const size = to === undefined ? givens : wallOf(cubesOf(scene.structures[to]));
|
|
236
|
+
const reads = [shapeOf(scene, of), ...(to === undefined ? [] : [shapeOf(scene, to)])];
|
|
237
|
+
const have = cubesOf(scene.structures[of]).length;
|
|
238
|
+
return solved({ number: size.length * size.height - have }, reads);
|
|
239
|
+
}
|
|
240
|
+
/** The facts of a building are the cubes of each structure and the marks on their faces. */
|
|
241
|
+
function facts(scene) {
|
|
242
|
+
return scene.structures.flatMap((st, i) => [
|
|
243
|
+
shapeOf(scene, i),
|
|
244
|
+
...(st.marks ?? []).map((_, j) => `structures[${i}].marks[${j}]`),
|
|
245
|
+
]);
|
|
246
|
+
}
|
|
247
|
+
/** cubesSolver counts the cubes of a building: the questions of CubesQuestion. */
|
|
248
|
+
export const cubesSolver = {
|
|
249
|
+
name: "cubes",
|
|
250
|
+
kinds: ["cubes"],
|
|
251
|
+
questions: Object.keys(members),
|
|
252
|
+
check,
|
|
253
|
+
facts,
|
|
254
|
+
solve,
|
|
255
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { DieScene } from "../kinds/die.ts";
|
|
2
|
+
import { type Solver } from "./proof.ts";
|
|
3
|
+
/** A die of a scene of dice, by its index in dice or by its label. */
|
|
4
|
+
export type DieRef = number | string;
|
|
5
|
+
/** A face of a die in view that cannot be seen, across from one that can. */
|
|
6
|
+
export type HiddenFace = "bottom" | "back" | "left";
|
|
7
|
+
/**
|
|
8
|
+
* The questions about dice: each die in view, seen from above, the front
|
|
9
|
+
* and the right, or one face of a die laid flat. A die is the one the
|
|
10
|
+
* question names, by its index or its label; with none, the one die in view,
|
|
11
|
+
* or for a sum every die in view — and every face laid flat, for seen-pips.
|
|
12
|
+
*
|
|
13
|
+
* - `opposite-face`: the dots on the face, which cannot be seen, across
|
|
14
|
+
* from a face in sight of a standard die — the bottom from the top, the
|
|
15
|
+
* back from the front, the left from the right —, 7 take away it, a
|
|
16
|
+
* number. The scene must be standard.
|
|
17
|
+
* - `seen-pips`: how many dots the faces in sight show together — the three
|
|
18
|
+
* of a die in view, the one of a face laid flat —, a number.
|
|
19
|
+
* - `hidden-pips`: how many dots the three faces out of sight of dice in
|
|
20
|
+
* view show together, a number: on a die with each of 1 to 6 once, 21
|
|
21
|
+
* take away the dots in sight. The scene must be standard, for the
|
|
22
|
+
* statement leans on its faces across adding up to 7.
|
|
23
|
+
*
|
|
24
|
+
* A face that shows "?" cannot be read, so a question that adds it is
|
|
25
|
+
* refused. It takes no givens.
|
|
26
|
+
*/
|
|
27
|
+
export type DieQuestion = {
|
|
28
|
+
ask: "opposite-face";
|
|
29
|
+
face: HiddenFace;
|
|
30
|
+
die?: DieRef;
|
|
31
|
+
} | {
|
|
32
|
+
ask: "seen-pips";
|
|
33
|
+
die?: DieRef;
|
|
34
|
+
} | {
|
|
35
|
+
ask: "hidden-pips";
|
|
36
|
+
die?: DieRef;
|
|
37
|
+
};
|
|
38
|
+
/** dieSolver adds the dots of dice: the questions of DieQuestion. */
|
|
39
|
+
export declare const dieSolver: Solver<DieScene, DieQuestion, Record<string, never>>;
|
package/lib/solve/die.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { oneOf } from "../read.js";
|
|
2
|
+
import { noScene, solved } from "./proof.js";
|
|
3
|
+
import { askOf, noGivens, refOf, sceneless } from "./question.js";
|
|
4
|
+
const members = {
|
|
5
|
+
"opposite-face": ["face", "die"],
|
|
6
|
+
"seen-pips": ["die"],
|
|
7
|
+
"hidden-pips": ["die"],
|
|
8
|
+
};
|
|
9
|
+
const across = {
|
|
10
|
+
bottom: "top",
|
|
11
|
+
back: "front",
|
|
12
|
+
left: "right",
|
|
13
|
+
};
|
|
14
|
+
const inView = ["top", "front", "right"];
|
|
15
|
+
const isView = (d) => !("face" in d);
|
|
16
|
+
const dotsOf = (d) => (isView(d) ? inView.map((f) => d[f]) : [d.face]);
|
|
17
|
+
/**
|
|
18
|
+
* chosen gives the dice a question reads: the one it names; or, with none,
|
|
19
|
+
* every die that shows dots for seen-pips and every die in view for the others.
|
|
20
|
+
*/
|
|
21
|
+
function chosen(scene, ref, ask) {
|
|
22
|
+
if (ref !== undefined)
|
|
23
|
+
return [typeof ref === "number" ? ref : scene.dice.findIndex((d) => d.label === ref)];
|
|
24
|
+
return scene.dice.flatMap((d, i) => isView(d) || (ask === "seen-pips" && d.face !== "?") ? [i] : []);
|
|
25
|
+
}
|
|
26
|
+
function check(question, givens, scene) {
|
|
27
|
+
const problems = [];
|
|
28
|
+
noGivens(givens, problems);
|
|
29
|
+
const read = askOf(question, members, problems);
|
|
30
|
+
if (!read)
|
|
31
|
+
return problems;
|
|
32
|
+
const { q, ask } = read;
|
|
33
|
+
if (ask === "opposite-face")
|
|
34
|
+
oneOf(q.face, Object.keys(across), "question.face", problems);
|
|
35
|
+
if (!scene)
|
|
36
|
+
sceneless(ask, "a scene of dice", problems);
|
|
37
|
+
if (!scene || problems.length > 0)
|
|
38
|
+
return problems;
|
|
39
|
+
if (q.die !== undefined)
|
|
40
|
+
refOf(q.die, scene.dice.map((d) => d.label), "dice", "question.die", problems);
|
|
41
|
+
if (problems.length > 0)
|
|
42
|
+
return problems;
|
|
43
|
+
if (ask !== "seen-pips" && !scene.standard)
|
|
44
|
+
problems.push(`standard: ${ask} reads a standard die, whose faces across add up to 7`);
|
|
45
|
+
const dice = chosen(scene, q.die, ask);
|
|
46
|
+
if (ask === "opposite-face" && dice.length !== 1)
|
|
47
|
+
problems.push(`question.die: the scene shows ${dice.length} dice in view; name the one asked`);
|
|
48
|
+
if (ask === "hidden-pips" && dice.length === 0)
|
|
49
|
+
problems.push("dice: hidden-pips reads a die in view, and the scene shows none");
|
|
50
|
+
for (const i of dice) {
|
|
51
|
+
const d = scene.dice[i];
|
|
52
|
+
if (ask !== "seen-pips" && !isView(d))
|
|
53
|
+
problems.push(`dice[${i}]: is a face laid flat; ${ask} reads a die in view`);
|
|
54
|
+
else if (ask === "opposite-face") {
|
|
55
|
+
const face = across[q.face];
|
|
56
|
+
if (d[face] === "?")
|
|
57
|
+
problems.push(`dice[${i}].${face}: shows "?", which ${ask} has to read`);
|
|
58
|
+
}
|
|
59
|
+
else if (dotsOf(d).includes("?"))
|
|
60
|
+
problems.push(`dice[${i}]: shows "?", which ${ask} has to read`);
|
|
61
|
+
}
|
|
62
|
+
return problems;
|
|
63
|
+
}
|
|
64
|
+
function solve(scene, question) {
|
|
65
|
+
if (!scene)
|
|
66
|
+
return noScene;
|
|
67
|
+
const dice = chosen(scene, question.die, question.ask);
|
|
68
|
+
const reads = dice.map((i) => `dice[${i}]`);
|
|
69
|
+
const sum = (ds) => ds.reduce((s, i) => s + dotsOf(scene.dice[i]).reduce((t, v) => t + v, 0), 0);
|
|
70
|
+
switch (question.ask) {
|
|
71
|
+
case "opposite-face": {
|
|
72
|
+
const d = scene.dice[dice[0]];
|
|
73
|
+
return solved({ number: 7 - d[across[question.face]] }, reads);
|
|
74
|
+
}
|
|
75
|
+
case "seen-pips":
|
|
76
|
+
return solved({ number: sum(dice) }, reads);
|
|
77
|
+
case "hidden-pips":
|
|
78
|
+
return solved({ number: 21 * dice.length - sum(dice) }, reads);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** The facts of a scene of dice are its dice that show dots. */
|
|
82
|
+
function facts(scene) {
|
|
83
|
+
return scene.dice.flatMap((d, i) => (dotsOf(d).some((v) => v !== "?") ? [`dice[${i}]`] : []));
|
|
84
|
+
}
|
|
85
|
+
/** dieSolver adds the dots of dice: the questions of DieQuestion. */
|
|
86
|
+
export const dieSolver = {
|
|
87
|
+
name: "die",
|
|
88
|
+
kinds: ["die"],
|
|
89
|
+
questions: Object.keys(members),
|
|
90
|
+
check,
|
|
91
|
+
facts,
|
|
92
|
+
solve,
|
|
93
|
+
};
|