@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,190 @@
1
+ import { sumOf } from "../kinds/target.js";
2
+ import { int, isObj, only } from "../read.js";
3
+ import { noScene, solved, unsolved } from "./proof.js";
4
+ import { askOf, refOf, sceneless } from "./question.js";
5
+ const members = {
6
+ score: ["board"],
7
+ "impossible-total": ["board"],
8
+ "which-holder-most": [],
9
+ "which-holder-fewest": [],
10
+ };
11
+ const mostDarts = 4;
12
+ const shown = (s) => typeof s === "number" ? s : s.ask ? undefined : s.score;
13
+ /** regionsOf names the members of a board that give scores, and the paths of those that show one. */
14
+ function regionsOf(b, at) {
15
+ const out = [];
16
+ for (const m of ["rings", "zones"])
17
+ if (b[m]?.some((s) => shown(s) !== undefined))
18
+ out.push(`${at}.${m}`);
19
+ if (b.bull !== undefined && shown(b.bull) !== undefined)
20
+ out.push(`${at}.bull`);
21
+ if (b.pegs?.some((p) => p.score !== undefined || (p.hoops ?? 0) > 0))
22
+ out.push(`${at}.pegs`);
23
+ return out;
24
+ }
25
+ /** boardFacts are what a board shows: its scores, its darts and a total that is a number. */
26
+ function boardFacts(b, i) {
27
+ const at = `targets[${i}]`;
28
+ return [
29
+ ...regionsOf(b, at),
30
+ ...((b.hits?.length ?? 0) > 0 ? [`${at}.hits`] : []),
31
+ ...(typeof b.total === "number" ? [`${at}.total`] : []),
32
+ ];
33
+ }
34
+ /** boardOf reads the board a question names, or the one board, and gives its index. */
35
+ function boardOf(q, scene, ask, problems) {
36
+ const labels = scene.targets.map((b) => b.label);
37
+ if (q.board !== undefined)
38
+ return refOf(q.board, labels, "targets", "question.board", problems);
39
+ if (scene.targets.length === 1)
40
+ return 0;
41
+ problems.push(`question.board: ${ask} reads one board of ${scene.targets.length}; name it`);
42
+ return undefined;
43
+ }
44
+ /** asked lists, for a board, why a question that adds its score cannot read it. */
45
+ function unreadable(b, at) {
46
+ if (b.pegs)
47
+ return b.pegs.flatMap((p, j) => p.ask && (p.hoops ?? 0) > 0 ? [`${at}.pegs[${j}]: holds hoops, and its score is asked`] : []);
48
+ return (b.hits ?? []).flatMap((h, j) => {
49
+ const s = h === "bull" ? b.bull : (b.rings ?? b.zones)[h - 1];
50
+ return shown(s) === undefined
51
+ ? [`${at}.hits[${j}]: lands where the score is asked, which the question has to read`]
52
+ : [];
53
+ });
54
+ }
55
+ /** totalsOf are the totals the options offer, each once. */
56
+ const totalsOf = (offered) => [
57
+ ...new Set((offered?.options ?? []).flatMap((o) => ("number" in o ? [o.number] : []))),
58
+ ];
59
+ function check(question, givens, scene, offered) {
60
+ const problems = [];
61
+ const read = askOf(question, members, problems);
62
+ const ask = read?.ask;
63
+ if (givens !== undefined) {
64
+ if (!isObj(givens))
65
+ problems.push("givens: must be an object");
66
+ else {
67
+ only(givens, ask === "impossible-total" ? ["darts"] : [], "givens", problems, `the givens of ${ask ?? "targets"}`);
68
+ if (givens.darts !== undefined)
69
+ int(givens.darts, "givens.darts", problems, 1, mostDarts);
70
+ }
71
+ }
72
+ if (ask === "impossible-total") {
73
+ if (!isObj(givens) || givens.darts === undefined)
74
+ problems.push("givens.darts: impossible-total needs the number of darts the statement names");
75
+ (offered?.options ?? []).forEach((o, i) => {
76
+ if (!("number" in o) || !Number.isInteger(o.number) || o.number < 0)
77
+ problems.push(`options[${i}]: impossible-total offers totals, each a whole number`);
78
+ });
79
+ if (totalsOf(offered).length < 2)
80
+ problems.push("options: impossible-total asks which of the totals offered; offer two or more");
81
+ }
82
+ if (!read)
83
+ return problems;
84
+ if (!scene) {
85
+ sceneless(read.ask, "a scene of targets", problems);
86
+ return problems;
87
+ }
88
+ const { q } = read;
89
+ if (ask === "score") {
90
+ const i = boardOf(q, scene, ask, problems);
91
+ if (i === undefined)
92
+ return problems;
93
+ const b = scene.targets[i];
94
+ const at = `targets[${i}]`;
95
+ if (typeof b.total === "number")
96
+ problems.push(`${at}.total: shows the score already`);
97
+ const things = b.pegs ? b.pegs.reduce((s, p) => s + (p.hoops ?? 0), 0) : (b.hits?.length ?? 0);
98
+ if (things === 0)
99
+ problems.push(`${at}: has no ${b.pegs ? "hoops" : "darts"} to score`);
100
+ problems.push(...unreadable(b, at));
101
+ }
102
+ if (ask === "impossible-total") {
103
+ const i = boardOf(q, scene, ask, problems);
104
+ if (i === undefined)
105
+ return problems;
106
+ const b = scene.targets[i];
107
+ const at = `targets[${i}]`;
108
+ if (b.pegs)
109
+ problems.push(`${at}: impossible-total counts darts on rings or zones, not hoops`);
110
+ else {
111
+ const scores = [...(b.rings ?? b.zones ?? []), ...(b.bull !== undefined ? [b.bull] : [])];
112
+ if (scores.some((s) => shown(s) === undefined))
113
+ problems.push(`${at}: a score is asked, and impossible-total adds every score`);
114
+ }
115
+ }
116
+ if (ask === "which-holder-most" || ask === "which-holder-fewest") {
117
+ const labels = scene.targets.map((b) => b.label);
118
+ if (labels.length < 2)
119
+ problems.push(`question.ask: ${ask} needs two boards or more`);
120
+ const bare = labels.indexOf(undefined);
121
+ if (bare >= 0)
122
+ problems.push(`question.ask: ${ask} names a board by its label, and targets[${bare}] has none`);
123
+ else if (new Set(labels).size < labels.length)
124
+ problems.push(`question.ask: ${ask} names a board by its label, and two boards share one`);
125
+ scene.targets.forEach((b, i) => {
126
+ problems.push(...unreadable(b, `targets[${i}]`));
127
+ });
128
+ }
129
+ return problems;
130
+ }
131
+ /** totals lists every total so many darts score on a board, each landing anywhere on it. */
132
+ function totals(b, darts) {
133
+ const scores = [
134
+ ...new Set([...(b.rings ?? b.zones ?? []), ...(b.bull !== undefined ? [b.bull] : [])].map((s) => shown(s))),
135
+ ];
136
+ let sums = new Set([0]);
137
+ for (let d = 0; d < darts; d++)
138
+ sums = new Set([...sums].flatMap((t) => scores.map((s) => t + s)));
139
+ return sums;
140
+ }
141
+ function solve(scene, question, givens = {}, offered) {
142
+ if (!scene)
143
+ return noScene;
144
+ const labels = scene.targets.map((b) => b.label);
145
+ const index = (ref) => ref === undefined ? 0 : typeof ref === "number" ? ref : labels.indexOf(ref);
146
+ switch (question.ask) {
147
+ case "score": {
148
+ const i = index(question.board);
149
+ const b = scene.targets[i];
150
+ return solved({ number: sumOf(b) }, boardFacts(b, i));
151
+ }
152
+ case "impossible-total": {
153
+ const i = index(question.board);
154
+ const b = scene.targets[i];
155
+ const reads = regionsOf(b, `targets[${i}]`);
156
+ const can = totals(b, givens.darts);
157
+ const offers = totalsOf(offered);
158
+ const cannot = offers.filter((t) => !can.has(t));
159
+ if (cannot.length === 1)
160
+ return solved({ number: cannot[0] }, reads);
161
+ const why = cannot.length === 0
162
+ ? `the darts can score every one of ${offers.join(", ")}`
163
+ : `the darts can score none of ${cannot.join(", ")}`;
164
+ return unsolved(why, reads);
165
+ }
166
+ case "which-holder-most":
167
+ case "which-holder-fewest": {
168
+ const reads = scene.targets.flatMap((b, i) => [...boardFacts(b, i), `targets[${i}].label`]);
169
+ const values = scene.targets.map((b) => sumOf(b));
170
+ const best = question.ask === "which-holder-most" ? Math.max(...values) : Math.min(...values);
171
+ const at = labels.filter((_, i) => values[i] === best);
172
+ if (at.length === 1)
173
+ return solved({ text: at[0] }, reads);
174
+ return unsolved(`${at.join(" and ")} score ${best} each, a tie`, reads);
175
+ }
176
+ }
177
+ }
178
+ /** The facts of a scene of targets are, board by board, its scores, its darts or hoops, and a total shown. */
179
+ function facts(scene) {
180
+ return scene.targets.flatMap((board, i) => boardFacts(board, i));
181
+ }
182
+ /** targetSolver scores the darts of a scene of targets: the questions of TargetQuestion. */
183
+ export const targetSolver = {
184
+ name: "target",
185
+ kinds: ["target"],
186
+ questions: Object.keys(members),
187
+ check,
188
+ facts,
189
+ solve,
190
+ };
package/lib/style.d.ts CHANGED
@@ -50,7 +50,7 @@ export declare const colors: {
50
50
  readonly gray: {
51
51
  readonly fill: "#a3abb8";
52
52
  readonly dark: "#77808f";
53
- readonly word: "gray";
53
+ readonly word: "grey";
54
54
  };
55
55
  readonly white: {
56
56
  readonly fill: "#ffffff";
package/lib/style.js CHANGED
@@ -19,7 +19,7 @@ export const colors = {
19
19
  purple: { fill: "#9b5de5", dark: "#7442b5", word: "purple" },
20
20
  pink: { fill: "#f37fb0", dark: "#c95a8a", word: "pink" },
21
21
  brown: { fill: "#9a6a44", dark: "#71492c", word: "brown" },
22
- gray: { fill: "#a3abb8", dark: "#77808f", word: "gray" },
22
+ gray: { fill: "#a3abb8", dark: "#77808f", word: "grey" },
23
23
  white: { fill: "#ffffff", dark: "#c9d0dc", word: "white" },
24
24
  black: { fill: "#2b3242", dark: "#141824", word: "black" },
25
25
  };
package/lib/words.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * article puts "a" or "an" before words, by how their first word sounds, not
3
+ * how it is spelt: "an orange ball", "a unicorn", "an hour", "an 8 by 3
4
+ * rectangle", "an F".
5
+ */
6
+ export declare function article(words: string): string;
7
+ /** capital starts a sentence with a capital letter. */
8
+ export declare function capital(words: string): string;
9
+ /**
10
+ * listOf says a list the British way, with commas and "and" before the last:
11
+ * "apples, pears and lemons". When an item has an "and" of its own, a comma
12
+ * stands before the last "and" too, so that the items stay apart: "lanes 1
13
+ * and 2, lanes 3 and 4, and lanes 5 and 6".
14
+ */
15
+ export declare function listOf(items: readonly string[]): string;
package/lib/words.js ADDED
@@ -0,0 +1,39 @@
1
+ // The words every kind's describe shares.
2
+ /**
3
+ * article puts "a" or "an" before words, by how their first word sounds, not
4
+ * how it is spelt: "an orange ball", "a unicorn", "an hour", "an 8 by 3
5
+ * rectangle", "an F".
6
+ */
7
+ export function article(words) {
8
+ return `${soundsVowel(words) ? "an" : "a"} ${words}`;
9
+ }
10
+ function soundsVowel(words) {
11
+ // A number: 8, 80, 800 …, and 11 and 18, alone or as thousands, start with a vowel's sound.
12
+ if (/^\d/.test(words))
13
+ return /^(?:8|1[18](?:\d{3})?(?!\d))/.test(words);
14
+ // A capital letter on its own is said by its name: "an F", "a U".
15
+ if (/^[A-Z](?![A-Za-z])/.test(words))
16
+ return /^[AEFHILMNORSX]/.test(words);
17
+ const w = words.toLowerCase();
18
+ if (/^(?:hour|honest|honou?r|heir)/.test(w))
19
+ return true;
20
+ if (/^(?:uni|use|usu|ura|eu|ewe|one\b|once)/.test(w))
21
+ return false;
22
+ return /^[aeiou]/.test(w);
23
+ }
24
+ /** capital starts a sentence with a capital letter. */
25
+ export function capital(words) {
26
+ return words.length > 0 ? `${words[0].toUpperCase()}${words.slice(1)}` : words;
27
+ }
28
+ /**
29
+ * listOf says a list the British way, with commas and "and" before the last:
30
+ * "apples, pears and lemons". When an item has an "and" of its own, a comma
31
+ * stands before the last "and" too, so that the items stay apart: "lanes 1
32
+ * and 2, lanes 3 and 4, and lanes 5 and 6".
33
+ */
34
+ export function listOf(items) {
35
+ if (items.length <= 1)
36
+ return items[0] ?? "";
37
+ const apart = items.some((item) => / and /.test(item)) ? "," : "";
38
+ return `${items.slice(0, -1).join(", ")}${apart} and ${items[items.length - 1]}`;
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/picture-kit",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Draws the pictures of tasks from scenes: a closed list of kinds, objects from Noto Emoji, the same scene always the same SVG.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {