@altnovatrail/picture-kit 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/README.md +1 -1
  2. package/lib/index.d.ts +45 -1
  3. package/lib/index.js +34 -3
  4. package/lib/kinds/balance.js +2 -3
  5. package/lib/kinds/calendar.d.ts +5 -1
  6. package/lib/kinds/calendar.js +33 -11
  7. package/lib/kinds/chart.js +2 -4
  8. package/lib/kinds/clock.d.ts +5 -0
  9. package/lib/kinds/clock.js +45 -4
  10. package/lib/kinds/coins.js +3 -2
  11. package/lib/kinds/column.js +2 -3
  12. package/lib/kinds/commands.d.ts +8 -6
  13. package/lib/kinds/commands.js +57 -15
  14. package/lib/kinds/containers.js +2 -1
  15. package/lib/kinds/countfig.d.ts +2 -0
  16. package/lib/kinds/countfig.js +6 -1
  17. package/lib/kinds/cubes.d.ts +29 -5
  18. package/lib/kinds/cubes.js +82 -34
  19. package/lib/kinds/die.d.ts +8 -0
  20. package/lib/kinds/die.js +75 -14
  21. package/lib/kinds/domino.js +3 -3
  22. package/lib/kinds/dotgrid.d.ts +3 -0
  23. package/lib/kinds/dotgrid.js +6 -4
  24. package/lib/kinds/figure.js +5 -4
  25. package/lib/kinds/fold-parts.d.ts +0 -2
  26. package/lib/kinds/fold-parts.js +0 -4
  27. package/lib/kinds/fold.d.ts +20 -1
  28. package/lib/kinds/fold.js +15 -6
  29. package/lib/kinds/fraction.js +3 -2
  30. package/lib/kinds/gauges.js +2 -1
  31. package/lib/kinds/gears.d.ts +21 -2
  32. package/lib/kinds/gears.js +113 -62
  33. package/lib/kinds/grid.d.ts +5 -0
  34. package/lib/kinds/grid.js +30 -2
  35. package/lib/kinds/groups.d.ts +3 -1
  36. package/lib/kinds/groups.js +142 -28
  37. package/lib/kinds/lanes.js +3 -2
  38. package/lib/kinds/layers.d.ts +6 -0
  39. package/lib/kinds/layers.js +23 -4
  40. package/lib/kinds/line.d.ts +4 -0
  41. package/lib/kinds/line.js +117 -32
  42. package/lib/kinds/matchsticks.d.ts +4 -0
  43. package/lib/kinds/matchsticks.js +5 -3
  44. package/lib/kinds/net.js +4 -3
  45. package/lib/kinds/overlay.d.ts +23 -1
  46. package/lib/kinds/overlay.js +7 -3
  47. package/lib/kinds/pieces.js +3 -2
  48. package/lib/kinds/plane-parts.d.ts +0 -2
  49. package/lib/kinds/plane-parts.js +0 -6
  50. package/lib/kinds/plane.js +6 -6
  51. package/lib/kinds/ropes.js +1 -3
  52. package/lib/kinds/shapes.js +5 -7
  53. package/lib/kinds/solids.js +3 -2
  54. package/lib/kinds/tangram.js +2 -4
  55. package/lib/kinds/target.js +4 -6
  56. package/lib/kinds/tiles.js +5 -4
  57. package/lib/kinds/venn.js +3 -5
  58. package/lib/noto.js +1 -0
  59. package/lib/objects.js +2 -1
  60. package/lib/read.d.ts +2 -2
  61. package/lib/read.js +3 -3
  62. package/lib/solve/balance.d.ts +11 -0
  63. package/lib/solve/balance.js +116 -1
  64. package/lib/solve/calendar.d.ts +46 -0
  65. package/lib/solve/calendar.js +206 -0
  66. package/lib/solve/choice-scenes.d.ts +20 -0
  67. package/lib/solve/choice-scenes.js +51 -0
  68. package/lib/solve/clock.d.ts +54 -0
  69. package/lib/solve/clock.js +240 -0
  70. package/lib/solve/coins.d.ts +71 -0
  71. package/lib/solve/coins.js +222 -0
  72. package/lib/solve/commands.d.ts +36 -0
  73. package/lib/solve/commands.js +210 -0
  74. package/lib/solve/countfig.d.ts +41 -0
  75. package/lib/solve/countfig.js +199 -0
  76. package/lib/solve/cubes.d.ts +49 -0
  77. package/lib/solve/cubes.js +255 -0
  78. package/lib/solve/die.d.ts +39 -0
  79. package/lib/solve/die.js +93 -0
  80. package/lib/solve/domino.d.ts +57 -0
  81. package/lib/solve/domino.js +263 -0
  82. package/lib/solve/enumerate.d.ts +75 -0
  83. package/lib/solve/enumerate.js +323 -0
  84. package/lib/solve/figure.d.ts +26 -0
  85. package/lib/solve/figure.js +88 -0
  86. package/lib/solve/fold.d.ts +22 -0
  87. package/lib/solve/fold.js +68 -0
  88. package/lib/solve/gaps.d.ts +65 -0
  89. package/lib/solve/gaps.js +292 -0
  90. package/lib/solve/gears.d.ts +33 -0
  91. package/lib/solve/gears.js +130 -0
  92. package/lib/solve/grid.d.ts +35 -0
  93. package/lib/solve/grid.js +194 -0
  94. package/lib/solve/groups.d.ts +93 -0
  95. package/lib/solve/groups.js +346 -0
  96. package/lib/solve/index.d.ts +60 -0
  97. package/lib/solve/index.js +194 -0
  98. package/lib/solve/layers.d.ts +30 -0
  99. package/lib/solve/layers.js +72 -0
  100. package/lib/solve/magic.d.ts +32 -0
  101. package/lib/solve/magic.js +216 -0
  102. package/lib/solve/matchsticks.d.ts +37 -0
  103. package/lib/solve/matchsticks.js +214 -0
  104. package/lib/solve/order.d.ts +94 -0
  105. package/lib/solve/order.js +419 -0
  106. package/lib/solve/overlay.d.ts +22 -0
  107. package/lib/solve/overlay.js +56 -0
  108. package/lib/solve/parity.d.ts +22 -0
  109. package/lib/solve/parity.js +27 -0
  110. package/lib/solve/pattern.d.ts +62 -0
  111. package/lib/solve/pattern.js +533 -0
  112. package/lib/solve/pieces.d.ts +57 -0
  113. package/lib/solve/pieces.js +309 -0
  114. package/lib/solve/pigeonhole.d.ts +38 -0
  115. package/lib/solve/pigeonhole.js +95 -0
  116. package/lib/solve/proof.d.ts +115 -0
  117. package/lib/solve/proof.js +105 -0
  118. package/lib/solve/pyramid.d.ts +28 -0
  119. package/lib/solve/pyramid.js +161 -0
  120. package/lib/solve/question.d.ts +25 -0
  121. package/lib/solve/question.js +75 -0
  122. package/lib/solve/shapes.d.ts +30 -0
  123. package/lib/solve/shapes.js +182 -0
  124. package/lib/solve/target.d.ts +40 -0
  125. package/lib/solve/target.js +190 -0
  126. package/lib/style.d.ts +1 -1
  127. package/lib/style.js +1 -1
  128. package/lib/words.d.ts +15 -0
  129. package/lib/words.js +39 -0
  130. package/package.json +1 -1
@@ -0,0 +1,533 @@
1
+ import { hexEdges, squareEdges } from "../kinds/tiles.js";
2
+ import { int, isObj, only } from "../read.js";
3
+ import { checkThing, sameThing, wordOf } from "../things.js";
4
+ import { choicesOf } from "./choice-scenes.js";
5
+ import { lineFacts, necklaceFacts } from "./gaps.js";
6
+ import { letters, noScene, sameValue, solved, unsolved, } from "./proof.js";
7
+ import { askOf, flagOf, sceneless } from "./question.js";
8
+ const members = {
9
+ "next-in-row": [],
10
+ "missing-in-row": [],
11
+ "at-place": [],
12
+ "how-many-of": ["of"],
13
+ "part-of-ring": [],
14
+ "lands-on": ["not"],
15
+ };
16
+ const takes = {
17
+ "next-in-row": [],
18
+ "missing-in-row": [],
19
+ "at-place": ["place"],
20
+ "how-many-of": ["length"],
21
+ "part-of-ring": [],
22
+ "lands-on": [],
23
+ };
24
+ /** The longest part that repeats, which the search for rules tries up to. */
25
+ const longest = 4;
26
+ /** The largest number a row grows to, and the furthest place asked. */
27
+ const most = 20;
28
+ function thingPlace(t, path) {
29
+ const ask = typeof t === "object" && "ask" in t;
30
+ const empty = typeof t === "object" && "empty" in t;
31
+ return { ...(ask || empty ? {} : { cell: t }), ask, empty, path };
32
+ }
33
+ /** rowOf reads a scene's places in order, or records why the scene is no row. */
34
+ function rowOf(scene, problems) {
35
+ if (scene.kind === "line") {
36
+ if (scene.style !== "row") {
37
+ problems.push(`style: the pattern solver reads things in a row, style row, not ${scene.style}`);
38
+ return undefined;
39
+ }
40
+ if (scene.unseen !== undefined) {
41
+ problems.push("unseen: a row whose middle is not shown hides its places");
42
+ return undefined;
43
+ }
44
+ return { ring: false, places: scene.things.map((t, i) => thingPlace(t, `things[${i}]`)) };
45
+ }
46
+ if (scene.kind === "necklace") {
47
+ if (scene.necklaces.length !== 1) {
48
+ problems.push("necklaces: the pattern solver reads one string or necklace; part-of-ring reads a necklace and labelled strings");
49
+ return undefined;
50
+ }
51
+ const k = scene.necklaces[0];
52
+ return {
53
+ ring: k.open !== true && k.clasp !== true,
54
+ places: k.beads.map((b, j) => thingPlace(b, `necklaces[0].beads[${j}]`)),
55
+ };
56
+ }
57
+ if (scene.layout === "grid") {
58
+ problems.push("layout: the pattern solver reads tiles in a row or round a ring, not a grid");
59
+ return undefined;
60
+ }
61
+ return {
62
+ ring: scene.layout === "ring",
63
+ shape: scene.shape,
64
+ tiles: scene.tiles,
65
+ places: scene.tiles.map((t, i) => ({
66
+ ...(t.ask || t.empty ? {} : { cell: t }),
67
+ ask: t.ask === true,
68
+ empty: t.empty === true,
69
+ path: `tiles[${i}]`,
70
+ })),
71
+ };
72
+ }
73
+ const cards = (row) => row.shape === undefined &&
74
+ row.places.every((p) => !p.cell || (typeof p.cell === "object" && "card" in p.cell && /^\d{1,2}$/.test(p.cell.card)));
75
+ const onlyDots = (row) => row.shape !== undefined &&
76
+ row.places.every((p) => {
77
+ const t = p.cell;
78
+ return !t || (t.dots !== undefined && !t.parts && !t.links && t.thing === undefined);
79
+ });
80
+ const wrap = (x, m) => ((x % m) + m) % m;
81
+ /**
82
+ * rules gives what each rule that agrees with a feature's shown values puts
83
+ * at the targets: a part of up to longest values that repeats, fitting a ring
84
+ * a whole number of times, whose places the shown values fill again, after
85
+ * the first time, at least once and at least one fewer times than the part is
86
+ * long; or, from three shown values, a steady step, of numbers along a row or
87
+ * of turns round a tile.
88
+ */
89
+ function rules(f, n, ring, targets) {
90
+ const out = new Map();
91
+ const add = (values) => out.set(JSON.stringify(values), values);
92
+ const known = f.values.flatMap((v, i) => (v === undefined ? [] : [[i, v]]));
93
+ for (let p = 1; p <= longest; p++) {
94
+ if (ring && n % p !== 0)
95
+ continue;
96
+ const part = new Map();
97
+ const fits = known.every(([i, v]) => {
98
+ const was = part.get(i % p);
99
+ if (was === undefined)
100
+ part.set(i % p, v);
101
+ return was === undefined || was === v;
102
+ });
103
+ if (!fits || known.length - part.size < Math.max(1, p - 1))
104
+ continue;
105
+ const values = targets.map((t) => part.get(t % p));
106
+ if (values.every((v) => v !== undefined))
107
+ add(values);
108
+ }
109
+ if (known.length < 3)
110
+ return [...out.values()];
111
+ const [a, first] = known[0];
112
+ if (f.mod !== undefined)
113
+ for (let d = 1; d < f.mod; d++) {
114
+ const at = (i) => wrap(first + (i - a) * d, f.mod);
115
+ if ((!ring || wrap(n * d, f.mod) === 0) && known.every(([i, v]) => at(i) === v))
116
+ add(targets.map(at));
117
+ }
118
+ if (f.grows && !ring) {
119
+ const [b, second] = known[1];
120
+ const d = (second - first) / (b - a);
121
+ const at = (i) => first + (i - a) * d;
122
+ if (Number.isInteger(d) && d !== 0 && known.every(([i, v]) => at(i) === v))
123
+ add(targets.map(at));
124
+ }
125
+ return [...out.values()];
126
+ }
127
+ // --- tiles -----------------------------------------------------------------------------------
128
+ /** formOf is a tile's pattern but for its colours, its turn and how many dots it has. */
129
+ function formOf(t) {
130
+ return JSON.stringify({
131
+ parts: (t.parts ?? []).map((p) => [p.part, p.at ?? 0]),
132
+ links: (t.links ?? []).map((l) => [l.from, l.to, l.straight === true]),
133
+ dots: t.dots !== undefined,
134
+ thing: t.thing ?? null,
135
+ });
136
+ }
137
+ /** coloursOf is a tile's colours: its fill, each part's, each line's and its dots'. */
138
+ function coloursOf(t) {
139
+ return JSON.stringify([
140
+ t.fill ?? "white",
141
+ ...(t.parts ?? []).map((p) => p.color),
142
+ ...(t.links ?? []).map((l) => l.color),
143
+ t.dots?.color ?? "black",
144
+ ]);
145
+ }
146
+ /** tileOf puts a tile together from its form, colours, turn and dots; undefined if they do not fit. */
147
+ function tileOf(form, colours, turn, dots) {
148
+ const f = JSON.parse(form);
149
+ const c = JSON.parse(colours);
150
+ if (c.length !== f.parts.length + f.links.length + 2)
151
+ return undefined;
152
+ if (f.dots && (dots < 1 || dots > 9))
153
+ return undefined;
154
+ let k = 1;
155
+ const tile = { fill: c[0], turn };
156
+ if (f.parts.length > 0)
157
+ tile.parts = f.parts.map(([part, at]) => ({ part, at, color: c[k++] }));
158
+ if (f.links.length > 0)
159
+ tile.links = f.links.map(([from, to, straight]) => ({ from, to, straight, color: c[k++] }));
160
+ if (f.dots)
161
+ tile.dots = { count: dots, color: c[k] };
162
+ if (f.thing !== null)
163
+ tile.thing = f.thing;
164
+ return tile;
165
+ }
166
+ /** dotsTurn is the turn of a tile's dots as they are drawn: some counts look the same turned. */
167
+ function dotsTurn(count, turn, shape) {
168
+ if (shape === "hexagon")
169
+ return count === 1 ? 0 : turn % 3;
170
+ return [1, 4, 5, 8, 9].includes(count) ? 0 : turn % 2;
171
+ }
172
+ /**
173
+ * drawnOf is a tile as the picture draws it: each part and line where the
174
+ * turn puts it, so that two tiles that look the same are the same.
175
+ */
176
+ function drawnOf(t, shape) {
177
+ const sides = shape === "square" ? 4 : 6;
178
+ const edges = shape === "square" ? squareEdges : hexEdges;
179
+ const turn = t.turn ?? 0;
180
+ const turned = (k) => (k + turn) % sides;
181
+ const parts = (t.parts ?? [])
182
+ .map((p) => `${p.part} ${p.color} ${turned(p.at ?? 0)}`)
183
+ .sort((a, b) => a.localeCompare(b));
184
+ const links = (t.links ?? [])
185
+ .map((l) => {
186
+ const a = turned(edges.indexOf(l.from));
187
+ const b = turned(edges.indexOf(l.to));
188
+ const straight = l.straight === true || wrap(a - b, sides) === sides / 2;
189
+ return `${Math.min(a, b)} ${Math.max(a, b)} ${l.color} ${straight}`;
190
+ })
191
+ .sort((a, b) => a.localeCompare(b));
192
+ const dots = t.dots
193
+ ? `${t.dots.count} ${t.dots.color ?? "black"} ${dotsTurn(t.dots.count, turn, shape)}`
194
+ : "";
195
+ const thing = t.thing === undefined ? "" : `${JSON.stringify(t.thing)} ${turn}`;
196
+ return JSON.stringify([t.fill ?? "white", parts, links, dots, thing]);
197
+ }
198
+ // --- the check -------------------------------------------------------------------------------
199
+ function givensOf(givens, ask, problems) {
200
+ if (givens === undefined)
201
+ return {};
202
+ if (!isObj(givens)) {
203
+ problems.push("givens: must be an object");
204
+ return {};
205
+ }
206
+ only(givens, takes[ask], "givens", problems, `the givens of ${ask}`);
207
+ if (givens.place !== undefined)
208
+ int(givens.place, "givens.place", problems, 1, most);
209
+ if (givens.length !== undefined)
210
+ int(givens.length, "givens.length", problems, 2, most);
211
+ return givens;
212
+ }
213
+ /** choiceKeys are what each picture option draws, in the terms a predicted tile is compared in. */
214
+ function choiceKeys(choices, row) {
215
+ return choices.map((o) => drawnOf(o.tiles[0], row.shape));
216
+ }
217
+ function checkChoices(row, ask, offered, problems) {
218
+ const pictures = (offered?.choices ?? []).length > 0;
219
+ if (pictures && row.shape === undefined) {
220
+ problems.push(`choices: a row of ${cards(row) ? "numbers" : "things"} is answered by its ${cards(row) ? "number" : "thing"}, and its options are no pictures`);
221
+ return;
222
+ }
223
+ if (row.shape === undefined || ask === "how-many-of")
224
+ return;
225
+ if (!pictures && onlyDots(row))
226
+ return;
227
+ const before = problems.length;
228
+ const choices = choicesOf(offered, ask, "tiles", (o, path) => {
229
+ if (o.shape !== row.shape || o.tiles.length !== 1)
230
+ problems.push(`${path}: an option for a row of tiles is one ${row.shape} tile`);
231
+ else if (o.tiles[0].ask || o.tiles[0].empty)
232
+ problems.push(`${path}: an option shows a tile, not a "?" or an empty place`);
233
+ }, problems);
234
+ if (problems.length > before)
235
+ return;
236
+ const keys = choiceKeys(choices, row);
237
+ keys.forEach((key, j) => {
238
+ const i = keys.indexOf(key);
239
+ if (i < j)
240
+ problems.push(`choices[${j}]: draws what choices[${i}] draws`);
241
+ });
242
+ }
243
+ function checkRow(row, ask, q, g, problems) {
244
+ const asks = row.places.flatMap((p, i) => (p.ask ? [i] : []));
245
+ const empties = row.places.flatMap((p, i) => (p.empty ? [i] : []));
246
+ const last = row.places.length - 1;
247
+ const at = (i) => row.places[i].path;
248
+ if (ask === "next-in-row" || ask === "missing-in-row") {
249
+ if (asks.length !== 1)
250
+ problems.push(`question.ask: ${ask} asks of one place, a "?", and the row has ${asks.length}`);
251
+ if (empties.length > 0)
252
+ problems.push(`${at(empties[0])}: ${ask} reads a whole row; leave out the empty places`);
253
+ if (ask === "next-in-row" && row.ring)
254
+ problems.push("question.ask: a ring has no last place; ask missing-in-row");
255
+ else if (ask === "next-in-row" && asks.length === 1 && asks[0] !== last)
256
+ problems.push('question.ask: the "?" does not end the row; ask missing-in-row');
257
+ if (ask === "missing-in-row" && !row.ring && asks.length === 1 && asks[0] === last)
258
+ problems.push('question.ask: the "?" ends the row; ask next-in-row');
259
+ }
260
+ if (ask === "at-place") {
261
+ if (row.ring)
262
+ problems.push("question.ask: at-place counts from the start of a row; a ring has none");
263
+ if (asks.length > 1)
264
+ problems.push('question.ask: at-place asks of one place; one "?" at most stands for it');
265
+ else if (asks.length === 1 && asks[0] !== last)
266
+ problems.push(`${at(asks[0])}: the "?" stands for the place asked, so it ends the row`);
267
+ const place = g.place;
268
+ if (place === undefined)
269
+ problems.push("givens.place: the statement tells the place asked, counted from the start");
270
+ else if (asks.length === 0 ? place - 1 <= last && !row.places[place - 1].empty : place - 1 < last)
271
+ problems.push(`givens.place: the row draws place ${place}, so the picture answers`);
272
+ if (row.shape !== undefined && place !== undefined)
273
+ (row.tiles ?? []).forEach((t, i) => {
274
+ const named = t.label !== undefined && /^\d{1,2}$/.test(t.label) ? Number(t.label) : 0;
275
+ const stands = t.ask ? place : i + 1;
276
+ if (named > 0 && named !== stands)
277
+ problems.push(`${at(i)}.label: names place ${named}, and the tile stands for place ${stands}`);
278
+ });
279
+ }
280
+ if (ask === "how-many-of") {
281
+ if (row.shape !== undefined)
282
+ problems.push("question.ask: how-many-of counts things of a row of things or beads");
283
+ if (asks.length > 0)
284
+ problems.push(`${at(asks[0])}: how-many-of fills a row's empty places; a "?" asks of one, as at-place does`);
285
+ const length = g.length;
286
+ if (row.ring && length !== undefined)
287
+ problems.push("givens.length: a ring has as many places as it draws");
288
+ else if (length !== undefined && length < row.places.length)
289
+ problems.push(`givens.length: the row draws ${row.places.length} places already`);
290
+ if (empties.length === 0 && (length ?? row.places.length) === row.places.length)
291
+ problems.push("question.ask: the row draws every place, so how-many-of has none to fill; draw empty places or give the length");
292
+ const of = q.of;
293
+ if (!row.places.some((p) => p.cell !== undefined && sameThing(p.cell, of)))
294
+ problems.push(`question.of: no place of the row holds ${wordOf(of, 2)}`);
295
+ }
296
+ }
297
+ function checkRing(scene, problems) {
298
+ if (scene.kind !== "necklace") {
299
+ problems.push("scene.kind: part-of-ring reads a necklace and the labelled strings after it");
300
+ return;
301
+ }
302
+ const [ring, ...strings] = scene.necklaces;
303
+ if (ring.open)
304
+ problems.push("necklaces[0].open: part-of-ring reads strings on a closed necklace, the first");
305
+ if (ring.clasp)
306
+ problems.push("necklaces[0].clasp: a clasp breaks the necklace; part-of-ring reads one that goes round");
307
+ if (strings.length < 2)
308
+ problems.push("necklaces: part-of-ring needs a necklace and two strings or more");
309
+ const labels = strings.map((s) => s.label);
310
+ strings.forEach((s, i) => {
311
+ const path = `necklaces[${i + 1}]`;
312
+ if (!s.open)
313
+ problems.push(`${path}.open: an option of part-of-ring is a string laid open`);
314
+ if (s.label === undefined)
315
+ problems.push(`${path}.label: an option is named by its label`);
316
+ else if (labels.indexOf(s.label) < i)
317
+ problems.push(`${path}.label: another string is labelled ${s.label} too`);
318
+ if (s.beads.length > ring.beads.length)
319
+ problems.push(`${path}.beads: ${s.beads.length} beads, more than the necklace's ${ring.beads.length}`);
320
+ });
321
+ scene.necklaces.forEach((k, i) => {
322
+ const j = k.beads.findIndex((b) => typeof b === "object" && ("ask" in b || "empty" in b));
323
+ if (j >= 0)
324
+ problems.push(`necklaces[${i}].beads[${j}]: part-of-ring reads every bead; a "?" or an empty place has none`);
325
+ });
326
+ }
327
+ function checkLands(scene, problems) {
328
+ if (scene.kind !== "line" || scene.style !== "row") {
329
+ problems.push("scene.kind: lands-on reads a row of things that a jumper jumps along, a line of style row");
330
+ return;
331
+ }
332
+ if (scene.unseen !== undefined) {
333
+ problems.push("unseen: a row whose middle is not shown hides its places");
334
+ return;
335
+ }
336
+ const jumps = scene.jumps ?? [];
337
+ if (jumps.length === 0)
338
+ problems.push("jumps: lands-on goes on from the first jump; draw it");
339
+ const step = jumps[0] ? jumps[0].to - jumps[0].from : 0;
340
+ jumps.forEach((j, k) => {
341
+ if (k > 0 && (j.from !== jumps[k - 1].to || j.to - j.from !== step))
342
+ problems.push(`jumps[${k}]: each jump goes on from where the one before lands, by the same step`);
343
+ });
344
+ }
345
+ function check(question, givens, scene, offered) {
346
+ const problems = [];
347
+ const read = askOf(question, members, problems);
348
+ if (!read)
349
+ return problems;
350
+ const { q, ask } = read;
351
+ const g = givensOf(givens, ask, problems);
352
+ if (ask === "how-many-of") {
353
+ if (q.of === undefined)
354
+ problems.push('question.of: must be the thing counted, as {"ball": "blue"}');
355
+ else
356
+ checkThing(q.of, "question.of", problems);
357
+ }
358
+ if (ask === "lands-on")
359
+ flagOf(q.not, "question.not", problems);
360
+ if (!scene) {
361
+ sceneless(ask, "a row: a line of style row, a necklace or tiles", problems);
362
+ return problems;
363
+ }
364
+ if (problems.length > 0)
365
+ return problems;
366
+ if (ask === "part-of-ring")
367
+ checkRing(scene, problems);
368
+ else if (ask === "lands-on")
369
+ checkLands(scene, problems);
370
+ else {
371
+ const row = rowOf(scene, problems);
372
+ if (row) {
373
+ checkRow(row, ask, q, g, problems);
374
+ checkChoices(row, ask, offered, problems);
375
+ }
376
+ }
377
+ return problems;
378
+ }
379
+ /** featuresOf are the features each place of a row is read by, in the mode the answer takes. */
380
+ function featuresOf(cells, mode, row) {
381
+ const of = (f) => cells.map((c) => (c === undefined ? undefined : f(c)));
382
+ if (mode === "thing")
383
+ return [{ values: of((t) => JSON.stringify(t)) }];
384
+ if (mode === "number")
385
+ return [{ values: of((t) => Number(t.card)), grows: true }];
386
+ const dots = { values: of((t) => t.dots?.count ?? 0), grows: true };
387
+ if (mode === "dots")
388
+ return [dots];
389
+ return [
390
+ { values: of(formOf) },
391
+ { values: of(coloursOf) },
392
+ { values: of((t) => t.turn ?? 0), mod: row.shape === "square" ? 4 : 6 },
393
+ dots,
394
+ ];
395
+ }
396
+ function product(lists) {
397
+ return lists.reduce((acc, list) => acc.flatMap((a) => list.map((x) => [...a, x])), [[]]);
398
+ }
399
+ function tell(found) {
400
+ const values = [];
401
+ for (const v of found)
402
+ if (v && !values.some((o) => sameValue(o, v)))
403
+ values.push(v);
404
+ return { values, none: found.some((v) => v === undefined) };
405
+ }
406
+ function solveRow(scene, question, g, choices) {
407
+ const row = rowOf(scene, []);
408
+ const reads = row.places.map((p) => p.path);
409
+ const askAt = row.places.findIndex((p) => p.ask);
410
+ // A "?" that ends a row for at-place stands for the place asked, not for its own place.
411
+ const shown = question.ask === "at-place" && askAt >= 0 ? row.places.slice(0, -1) : row.places;
412
+ const length = question.ask === "how-many-of"
413
+ ? (g.length ?? shown.length)
414
+ : Math.max(shown.length, g.place ?? 0);
415
+ const cells = Array.from({ length }, (_, i) => shown[i]?.cell);
416
+ const targets = question.ask === "how-many-of"
417
+ ? cells.flatMap((c, i) => (c === undefined ? [i] : []))
418
+ : question.ask === "at-place"
419
+ ? [g.place - 1]
420
+ : [askAt];
421
+ const mode = question.ask === "how-many-of"
422
+ ? "thing"
423
+ : row.shape === undefined
424
+ ? cards(row)
425
+ ? "number"
426
+ : "thing"
427
+ : choices.length > 0
428
+ ? "tile"
429
+ : "dots";
430
+ const features = featuresOf(cells, mode, row);
431
+ const found = features.map((f) => rules(f, length, row.ring, targets));
432
+ if (found.some((f) => f.length === 0))
433
+ return unsolved(`the row shows no part of up to ${longest} that repeats, nor a steady step, that tells it`, reads);
434
+ const keys = choices.length > 0 ? choiceKeys(choices, row) : [];
435
+ const letterOf = (key) => {
436
+ const i = keys.indexOf(key);
437
+ return i < 0 ? undefined : { letter: letters[i] };
438
+ };
439
+ const answers = product(found).map((vs) => {
440
+ if (question.ask === "how-many-of") {
441
+ const of = JSON.stringify(question.of);
442
+ const filled = [...features[0].values];
443
+ targets.forEach((t, i) => {
444
+ filled[t] = vs[0][i];
445
+ });
446
+ return { number: filled.filter((v) => v === of).length };
447
+ }
448
+ const at = vs.map((v) => v[0]);
449
+ if (mode === "number" || mode === "dots") {
450
+ const n = at[0];
451
+ return n >= 0 && n <= most ? { number: n } : undefined;
452
+ }
453
+ if (mode === "thing")
454
+ return { thing: JSON.parse(at[0]) };
455
+ const tile = tileOf(at[0], at[1], at[2], at[3]);
456
+ return tile ? letterOf(drawnOf(tile, row.shape)) : undefined;
457
+ });
458
+ const told = tell(answers);
459
+ if (told.values.length === 1 && !told.none)
460
+ return solved(told.values[0], reads);
461
+ // Each rule is shown once more than its length, so two rules that fit the same places rarely put
462
+ // different things at the place asked; where they do, the candidates let "it cannot be told" match.
463
+ const many = told.values.length > 1;
464
+ const why = many
465
+ ? "the row goes on in more than one way"
466
+ : choices.length > 0
467
+ ? "no picture option draws what the row puts there"
468
+ : `the row would run past ${most} or below none`;
469
+ return unsolved(why, reads, many ? told.values : []);
470
+ }
471
+ function solveRing(scene) {
472
+ const [ring, ...strings] = scene.necklaces;
473
+ const beads = ring.beads;
474
+ const n = beads.length;
475
+ const reads = [
476
+ ...beads.map((_, j) => `necklaces[0].beads[${j}]`),
477
+ ...strings.flatMap((s, i) => [
478
+ ...s.beads.map((_, j) => `necklaces[${i + 1}].beads[${j}]`),
479
+ `necklaces[${i + 1}].label`,
480
+ ]),
481
+ ];
482
+ const fits = (s) => beads.some((_, start) => [1, -1].some((way) => s.every((b, j) => sameThing(beads[wrap(start + way * j, n)], b))));
483
+ const yes = strings.filter((s) => fits(s.beads)).map((s) => ({ text: s.label }));
484
+ if (yes.length === 1)
485
+ return solved(yes[0], reads);
486
+ if (yes.length === 0)
487
+ return unsolved("no string can be read round the necklace", reads);
488
+ return unsolved("more than one string can be read round the necklace", reads, yes);
489
+ }
490
+ function solveLands(scene, question) {
491
+ const n = scene.things.length;
492
+ const jumps = scene.jumps;
493
+ const reads = [
494
+ ...scene.things.map((_, i) => `things[${i}]`),
495
+ ...(scene.labels?.some((l) => l !== "") ? ["labels"] : []),
496
+ ...jumps.map((_, k) => `jumps[${k}]`),
497
+ ];
498
+ const step = jumps[0].to - jumps[0].from;
499
+ let landed = 0;
500
+ for (let at = jumps[0].from; at >= 1 && at <= n; at += step)
501
+ landed++;
502
+ return solved({ number: question.not ? n - landed : landed }, reads);
503
+ }
504
+ function solve(scene, question, givens, offered) {
505
+ if (!scene)
506
+ return noScene;
507
+ if (question.ask === "part-of-ring")
508
+ return solveRing(scene);
509
+ if (question.ask === "lands-on")
510
+ return solveLands(scene, question);
511
+ return solveRow(scene, question, givens ?? {}, (offered?.choices ?? []));
512
+ }
513
+ /**
514
+ * The facts of a row are each of its places — a thing, a bead, a tile —, and
515
+ * what else its picture shows: a line's labels, gaps, jumps and marks, a
516
+ * necklace's label and cut, the middle tile of a ring of tiles.
517
+ */
518
+ function facts(scene) {
519
+ if (scene.kind === "line")
520
+ return lineFacts(scene);
521
+ if (scene.kind === "necklace")
522
+ return necklaceFacts(scene);
523
+ return [...scene.tiles.map((_, i) => `tiles[${i}]`), ...(scene.centre ? ["centre"] : [])];
524
+ }
525
+ /** patternSolver continues rows that repeat a part or grow by a step: the questions of PatternQuestion. */
526
+ export const patternSolver = {
527
+ name: "pattern",
528
+ kinds: ["line", "necklace", "tiles"],
529
+ questions: Object.keys(members),
530
+ check,
531
+ facts,
532
+ solve,
533
+ };
@@ -0,0 +1,57 @@
1
+ import type { PiecesScene } from "../kinds/pieces.ts";
2
+ import type { TangramScene } from "../kinds/tangram.ts";
3
+ import type { TilesScene } from "../kinds/tiles.ts";
4
+ import { type Solver } from "./proof.ts";
5
+ /**
6
+ * The questions about pieces cut from squared paper or from a tangram. A
7
+ * piece may be laid turned and is still the same piece; flipped over, a
8
+ * mirror image, it is the same only when the question says flip, as the
9
+ * statement does when pieces may be turned over. Pieces of squared paper lie
10
+ * square on square, a half square — a square cut along a diagonal — on a
11
+ * half square; the colour of a piece is never its shape.
12
+ *
13
+ * - `fills-gap`: which of the options covers the gap exactly, the one that
14
+ * fits. In a tangram figure the gap is its missing piece and the options
15
+ * are its pieces apart, each with a label, the answer a text; a piece fits
16
+ * when it is the same piece, the parallelogram the same way round unless
17
+ * flip. On a floor of square tiles in a grid, its tiles plain, the gap is
18
+ * its empty places and the options the task's picture options, pieces
19
+ * scenes of one piece each with no board, the answer a letter. None or two
20
+ * options that fit have no answer.
21
+ * - `how-many-cover`: how many copies of the one piece of a scene of pieces
22
+ * cover its board with no gap and no overlap, a number; a board they
23
+ * cannot cover has no answer. The search tries every way to lay the
24
+ * copies, and runs on a board of at most 36 squares.
25
+ * - `which-pair`: the two pieces, of a scene of labelled pieces and a board,
26
+ * that together cover the board with no gap and no overlap, as a text,
27
+ * their labels in the order of the scene joined by "and": "A and B". Every
28
+ * pair is tried, and none or two pairs that cover it have no answer.
29
+ * - `odd-one-out`: the one piece, of three labelled pieces or more, whose
30
+ * shape is not that of the others, which all have one shape, as a text,
31
+ * its label; otherwise no answer. Pieces of more than one colour are
32
+ * refused unless the question says anyColor, as the statement does when
33
+ * the colour does not count.
34
+ *
35
+ * Every question reads every piece, and the board or the gap. None takes
36
+ * givens.
37
+ */
38
+ export type PiecesQuestion = {
39
+ ask: "fills-gap";
40
+ flip?: boolean;
41
+ } | {
42
+ ask: "how-many-cover";
43
+ flip?: boolean;
44
+ } | {
45
+ ask: "which-pair";
46
+ flip?: boolean;
47
+ } | {
48
+ ask: "odd-one-out";
49
+ flip?: boolean;
50
+ anyColor?: boolean;
51
+ };
52
+ /** The givens of pieces: none. */
53
+ export type PiecesGivens = Record<string, never>;
54
+ type Scene = PiecesScene | TangramScene | TilesScene;
55
+ /** piecesSolver fits pieces of squared paper and of a tangram: the questions of PiecesQuestion. */
56
+ export declare const piecesSolver: Solver<Scene, PiecesQuestion, PiecesGivens>;
57
+ export {};