@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.
Files changed (130) hide show
  1. package/README.md +2 -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,214 @@
1
+ import { countShapes } from "../kinds/countfig.js";
2
+ import { placesOf, shapes } from "../kinds/matchsticks.js";
3
+ import { int, isObj, only } from "../read.js";
4
+ import { noScene, solved, unsolved, } from "./proof.js";
5
+ import { askOf, sceneless } from "./question.js";
6
+ const members = {
7
+ "how-many-matches": [],
8
+ "count-squares": [],
9
+ "move-one": [],
10
+ };
11
+ const symbolsOf = (scene) => [...(scene.text ?? "")].map((c) => (c === "−" ? "-" : c));
12
+ const isDigit = (c) => /[0-9]/.test(c);
13
+ const key = (places) => [...places].sort((a, b) => a.localeCompare(b)).join(" ");
14
+ /** symbolOf names the symbol the matches at places make, where a symbol of that sort once was. */
15
+ function symbolOf(was, places) {
16
+ const k = key(places);
17
+ return Object.keys(shapes).find((c) => isDigit(c) === isDigit(was) && (c === "=") === (was === "=") && key(shapes[c]) === k);
18
+ }
19
+ /** numbersOf gives each symbol of a text the number it is a digit of, by the order of the numbers. */
20
+ function numbersOf(symbols) {
21
+ let n = -1;
22
+ return symbols.map((c, i) => {
23
+ if (!isDigit(c))
24
+ return undefined;
25
+ if (i === 0 || !isDigit(symbols[i - 1]))
26
+ n++;
27
+ return n;
28
+ });
29
+ }
30
+ const plain = (t) => t.replace(/ /g, "").replace(/−/g, "-");
31
+ /** reached lists every text one match moved makes, each with whether a move within a number makes it. */
32
+ function reached(symbols) {
33
+ const out = new Map();
34
+ const numbers = numbersOf(symbols);
35
+ const has = symbols.map((c) => shapes[c] ?? []);
36
+ symbols.forEach((_, i) => {
37
+ for (const p of has[i])
38
+ symbols.forEach((to, j) => {
39
+ if (!(to in shapes))
40
+ return;
41
+ for (const q of placesOf(to)) {
42
+ if (has[j].includes(q))
43
+ continue;
44
+ const next = has.map((s) => [...s]);
45
+ next[i] = next[i].filter((x) => x !== p);
46
+ next[j].push(q);
47
+ const made = symbols.map((c, k) => (c === " " ? " " : symbolOf(c, next[k])));
48
+ if (made.some((c) => c === undefined))
49
+ continue;
50
+ const words = plain(made.join(""));
51
+ const within = numbers[i] !== undefined && numbers[i] === numbers[j];
52
+ out.set(words, (out.get(words) ?? false) || within);
53
+ }
54
+ });
55
+ });
56
+ return out;
57
+ }
58
+ /** trueSum tells whether a text of numbers, + and − is a true equality. */
59
+ function trueSum(t) {
60
+ const sides = plain(t).split("=");
61
+ if (sides.length < 2 || sides.some((s) => !/^[0-9]+([+-][0-9]+)*$/.test(s)))
62
+ return false;
63
+ const value = (s) => (s.match(/[+-]?[0-9]+/g) ?? []).reduce((sum, term) => sum + Number(term), 0);
64
+ return sides.every((s) => value(s) === value(sides[0]));
65
+ }
66
+ const sticksOf = (scene) => (scene.sticks ?? []).map((v, i) => {
67
+ const s = Array.isArray(v) ? { at: v } : v;
68
+ return { at: s.at, ghost: s.mark === "ghost", path: `sticks[${i}]` };
69
+ });
70
+ /** rowOf is how many squares the matches make side by side in a row, if they make one and nothing else. */
71
+ function rowOf(scene) {
72
+ const sticks = sticksOf(scene);
73
+ if (sticks.some((s) => s.ghost) || (scene.grid ?? "square") !== "square")
74
+ return undefined;
75
+ const xs = sticks.flatMap((s) => [s.at[0], s.at[2]]);
76
+ const ys = sticks.flatMap((s) => [s.at[1], s.at[3]]);
77
+ const [x0, y0] = [Math.min(...xs), Math.min(...ys)];
78
+ const [w, h] = [Math.max(...xs) - x0, Math.max(...ys) - y0];
79
+ const k = Math.max(w, h);
80
+ // A row of k squares has 3k + 1 places for a match, so 3k + 1 matches in its box fill them all.
81
+ return Math.min(w, h) === 1 && sticks.length === 3 * k + 1 ? k : undefined;
82
+ }
83
+ /** figureOf is the figure of the matches of a square grid that lie there, as countfig reads one. */
84
+ function figureOf(scene) {
85
+ const names = new Map();
86
+ const name = (x, y) => {
87
+ const k = `${x},${y}`;
88
+ if (!names.has(k))
89
+ names.set(k, `P${names.size}`);
90
+ return names.get(k);
91
+ };
92
+ const segments = sticksOf(scene)
93
+ .filter((s) => !s.ghost)
94
+ .map((s) => [name(s.at[0], s.at[1]), name(s.at[2], s.at[3])]);
95
+ const points = [...names].map(([k, n]) => ({
96
+ name: n,
97
+ at: k.split(",").map(Number),
98
+ }));
99
+ return { kind: "countfig", points, segments };
100
+ }
101
+ /** said is an option of move-one as the text it names, its spaces left out. */
102
+ const said = (o) => ("number" in o ? String(o.number) : plain(o.text));
103
+ function checkOptions(offered, sum, problems) {
104
+ const options = offered?.options ?? [];
105
+ if (options.length === 0)
106
+ problems.push("options: move-one asks which option one match moved reaches; offer them");
107
+ const seen = new Map();
108
+ options.forEach((o, i) => {
109
+ const path = `options[${i}]`;
110
+ if (!("number" in o) && !("text" in o)) {
111
+ problems.push(`${path}: move-one offers a number or a text`);
112
+ return;
113
+ }
114
+ if ("number" in o && (!Number.isInteger(o.number) || o.number < 0))
115
+ problems.push(`${path}.number: must be a whole number, as a text of digits shows`);
116
+ if ("number" in o && sum)
117
+ problems.push(`${path}: a sum's options are texts, as {"text": "5 + 2 = 7"}`);
118
+ const other = seen.get(said(o));
119
+ if (other !== undefined)
120
+ problems.push(`${path}: is the same as options[${other}]`);
121
+ seen.set(said(o), i);
122
+ });
123
+ }
124
+ function check(question, givens, scene, offered) {
125
+ const problems = [];
126
+ const read = askOf(question, members, problems);
127
+ if (givens !== undefined && !isObj(givens)) {
128
+ problems.push("givens: must be an object");
129
+ return problems;
130
+ }
131
+ const g = isObj(givens) ? givens : {};
132
+ if (!read)
133
+ return problems;
134
+ const { ask } = read;
135
+ only(g, ask === "how-many-matches" ? ["length"] : [], "givens", problems, `the givens of ${ask}`);
136
+ if (!scene) {
137
+ sceneless(ask, "a scene of matchsticks", problems);
138
+ return problems;
139
+ }
140
+ const grid = scene.text === undefined;
141
+ if (ask === "how-many-matches") {
142
+ if (g.length === undefined)
143
+ return problems;
144
+ const before = problems.length;
145
+ const length = int(g.length, "givens.length", problems, 2, 20);
146
+ if (problems.length > before)
147
+ return problems;
148
+ const k = grid ? rowOf(scene) : undefined;
149
+ if (k === undefined)
150
+ problems.push("givens.length: a longer row is counted from a row of squares side by side, and the picture is none");
151
+ else if (length <= k)
152
+ problems.push(`givens.length: the row drawn has ${k} squares already; ask a longer one`);
153
+ return problems;
154
+ }
155
+ if (ask === "count-squares") {
156
+ if (!grid || (scene.grid ?? "square") !== "square")
157
+ problems.push("question.ask: count-squares counts the squares of sticks on a square grid");
158
+ return problems;
159
+ }
160
+ if (grid)
161
+ problems.push("question.ask: move-one moves a match of a text of digits");
162
+ else
163
+ checkOptions(offered, symbolsOf(scene).includes("="), problems);
164
+ return problems;
165
+ }
166
+ function solve(scene, question, givens = {}, offered) {
167
+ if (!scene)
168
+ return noScene;
169
+ const sticks = sticksOf(scene);
170
+ if (question.ask === "how-many-matches") {
171
+ if (scene.text !== undefined) {
172
+ const total = symbolsOf(scene).reduce((sum, c) => sum + (shapes[c]?.length ?? 0), 0);
173
+ return solved({ number: total }, ["text"]);
174
+ }
175
+ const lying = sticks.filter((s) => !s.ghost);
176
+ const reads = lying.map((s) => s.path);
177
+ if (givens.length !== undefined)
178
+ return solved({ number: 3 * givens.length + 1 }, reads);
179
+ return solved({ number: lying.length }, reads);
180
+ }
181
+ if (question.ask === "count-squares")
182
+ return solved({ number: countShapes(figureOf(scene)).squares }, sticks.map((s) => s.path));
183
+ const symbols = symbolsOf(scene);
184
+ const sum = symbols.includes("=");
185
+ const made = reached(symbols);
186
+ const options = (offered?.options ?? []).filter((o) => !("undecided" in o));
187
+ const hits = options.filter((o) => {
188
+ const t = said(o);
189
+ return made.has(t) && (!sum || trueSum(t));
190
+ });
191
+ if (hits.length === 1) {
192
+ if (made.get(said(hits[0])))
193
+ return solved(hits[0], ["text"]);
194
+ return unsolved("only a match moved between two numbers, or between a number and a sign, reaches it", ["text"]);
195
+ }
196
+ return unsolved(hits.length === 0
197
+ ? `no option offered is reached by moving one match${sum ? " to a true sum" : ""}`
198
+ : "more than one option offered is reached by moving one match", ["text"], hits);
199
+ }
200
+ /** The facts of matches: a text and its marks, or each stick of a grid. */
201
+ function facts(scene) {
202
+ if (scene.text !== undefined)
203
+ return ["text", ...(scene.marks ?? []).map((_, i) => `marks[${i}]`)];
204
+ return sticksOf(scene).map((s) => s.path);
205
+ }
206
+ /** matchsticksSolver counts and moves matches: the questions of MatchsticksQuestion. */
207
+ export const matchsticksSolver = {
208
+ name: "matchsticks",
209
+ kinds: ["matchsticks"],
210
+ questions: Object.keys(members),
211
+ check,
212
+ facts,
213
+ solve,
214
+ };
@@ -0,0 +1,94 @@
1
+ import type { LineScene } from "../kinds/line.ts";
2
+ import { type Thing } from "../things.ts";
3
+ import { type Solver } from "./proof.ts";
4
+ /**
5
+ * Who is a child or an animal of an order, as the givens name it: a name,
6
+ * "Ana", the label it has in the row; or a thing of the row, "animal.hen",
7
+ * where that thing stands in the row once with no name. A text that is an
8
+ * object of the list is that thing; any other text is a name.
9
+ */
10
+ export type Who = string | Thing;
11
+ /**
12
+ * A clue about the start order, before any event. ahead and behind compare
13
+ * two anywhere in the order, right-ahead and right-behind put one next to
14
+ * the other; at and not-at put one in a place counted from the front, a
15
+ * number or "last". An order of heights or ages is an order too: its front
16
+ * is the tallest or the oldest, so "Ana is taller than Mei" is Ana ahead of Mei.
17
+ */
18
+ export type OrderClue = {
19
+ who: Who;
20
+ is: "ahead" | "behind" | "right-ahead" | "right-behind";
21
+ of: Who;
22
+ } | {
23
+ who: Who;
24
+ is: "at" | "not-at";
25
+ place: number | "last";
26
+ };
27
+ /**
28
+ * An event changes the line after the clues, in the order given: one passes
29
+ * another who is ahead of it and takes that one's place, the others between
30
+ * moving back one; one leaves the line; one new joins its end.
31
+ */
32
+ export type OrderEvent = {
33
+ who: Who;
34
+ does: "passes";
35
+ whom: Who;
36
+ } | {
37
+ who: Who;
38
+ does: "leaves";
39
+ } | {
40
+ who: Who;
41
+ does: "joins";
42
+ };
43
+ /**
44
+ * The givens of an order. With no scene, children names everyone in the
45
+ * order; with a scene, the row is the start order — its first thing the
46
+ * front unless marker says that the first thing (an ice cream, a flag) only
47
+ * marks the front and stands in no place —, and fill names who stands in
48
+ * its question marks, in an order the clues decide. A label 1st, 2nd …
49
+ * names a place and must be that place; any other label is a name.
50
+ */
51
+ export type OrderGivens = {
52
+ children?: Who[];
53
+ fill?: Who[];
54
+ marker?: boolean;
55
+ clues?: OrderClue[];
56
+ events?: OrderEvent[];
57
+ /** at-place: the place asked, counted from the front after the events. */
58
+ place?: number;
59
+ /** place-of: the one whose place is asked. */
60
+ child?: Who;
61
+ /** line-length: one child's place counted from the front and from the back. */
62
+ fromFront?: number;
63
+ fromBack?: number;
64
+ };
65
+ /**
66
+ * The questions about an order of children or animals, told by clues and
67
+ * changed by events, each answered of the order after the events:
68
+ *
69
+ * - `first`, `last`: who is at the front or at the back — by name as a
70
+ * text, or a thing of the row with no name as a thing.
71
+ * - `at-place`: who is at givens.place, counted from the front.
72
+ * - `place-of`: the place of givens.child counted from the front, a number.
73
+ * - `line-length`: how many stand in a line where one child is
74
+ * givens.fromFront from the front and givens.fromBack from the back, a
75
+ * number; it reads no scene and takes no other givens.
76
+ *
77
+ * The solver tries every order of the children, or every way to fill the
78
+ * row's question marks, that keeps every clue and lets every pass happen,
79
+ * and proves an answer that holds in all of them; when they answer
80
+ * differently, it cannot be told, and its candidates are those answers. The
81
+ * search runs over at most six in an order (720 orders), with at most eight
82
+ * clues and four events.
83
+ */
84
+ export type OrderQuestion = {
85
+ ask: "first" | "last" | "at-place" | "place-of" | "line-length";
86
+ };
87
+ /** permutations gives every order of the items, each once. */
88
+ export declare function permutations<T>(items: readonly T[]): Generator<T[]>;
89
+ /**
90
+ * orderSolver joins the clues about an order of children or animals: the
91
+ * questions of OrderQuestion, the start order read from a row of a line or
92
+ * told in givens.children.
93
+ */
94
+ export declare const orderSolver: Solver<LineScene, OrderQuestion, OrderGivens>;