@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,206 @@
1
+ import { cellOf, columnsOf, datesOf, dayWords, ordinal, weekdays, } from "../kinds/calendar.js";
2
+ import { int, isObj, oneOf, only } from "../read.js";
3
+ import { noScene, solved } from "./proof.js";
4
+ import { askOf, sceneless } from "./question.js";
5
+ const members = {
6
+ "weekday-after": [],
7
+ "weekday-before": [],
8
+ "weekday-of-date": [],
9
+ "date-after": [],
10
+ };
11
+ const relative = { yesterday: -1, today: 0, tomorrow: 1 };
12
+ const days = ["yesterday", "today", "tomorrow"];
13
+ /** The members of a page that every question of a page reads, where they are there. */
14
+ const pageMembers = [
15
+ "first",
16
+ "days",
17
+ "start",
18
+ "count",
19
+ "weekStart",
20
+ "month",
21
+ "askWeekdays",
22
+ "ask",
23
+ "blots",
24
+ ];
25
+ /** The last date every month has: sheets show no month's length. */
26
+ const surely = 28;
27
+ const turn = (w, by) => weekdays[(((weekdays.indexOf(w) + by) % 7) + 7) % 7];
28
+ const styleOf = (c) => c.style ?? "month";
29
+ function pageReads(c) {
30
+ return pageMembers.filter((m) => c[m] !== undefined);
31
+ }
32
+ /** markOn is the read of the mark on a date, if one is there. */
33
+ function markOn(c, day) {
34
+ const at = (c.marks ?? []).findIndex((m) => m.day === day);
35
+ return at < 0 ? [] : [`marks[${at}]`];
36
+ }
37
+ function onSheets(ask, g, c, problems) {
38
+ const sheets = c.sheets;
39
+ const before = problems.length;
40
+ const n = int(g.days, "givens.days", problems, 1, 31);
41
+ const s = g.sheet === undefined ? 0 : int(g.sheet, "givens.sheet", problems, 0, sheets.length - 1);
42
+ const is = g.is === undefined ? "today" : oneOf(g.is, days, "givens.is", problems);
43
+ if (problems.length > before)
44
+ return undefined;
45
+ const start = sheets[s];
46
+ const shift = (ask === "weekday-before" ? -n : n) - relative[is];
47
+ const wanted = ask === "date-after" ? "day" : "weekday";
48
+ const asking = sheets.flatMap((sheet, i) => (sheet.ask === wanted && i !== s ? [i] : []));
49
+ if (asking.length > 1)
50
+ problems.push(`sheets: ${asking.length} sheets ask the ${wanted}; the question answers one`);
51
+ const t = asking[0];
52
+ const target = t === undefined ? undefined : sheets[t];
53
+ if (target?.month !== undefined && start.month !== undefined && target.month !== start.month)
54
+ problems.push(`sheets[${t}].month: the count runs into another month, and sheets show no month's length`);
55
+ const reads = [`sheets[${s}]`, ...(t === undefined ? [] : [`sheets[${t}]`])];
56
+ const date = start.day === undefined ? undefined : start.day + shift;
57
+ const weekday = start.weekday === undefined ? undefined : turn(start.weekday, shift);
58
+ if (wanted === "weekday") {
59
+ if (weekday === undefined)
60
+ problems.push(`sheets[${s}].weekday: the sheet shows no weekday to count from`);
61
+ if (target?.day !== undefined && date !== undefined && target.day !== date)
62
+ problems.push(`sheets[${t}].day: the sheet shows the ${ordinal(target.day)}, and the day counted to is the ${date >= 1 ? ordinal(date) : "day before the 1st"}`);
63
+ if (problems.length > before)
64
+ return undefined;
65
+ return { answer: { text: dayWords[weekday] }, reads };
66
+ }
67
+ if (date === undefined) {
68
+ problems.push(`sheets[${s}].day: the sheet shows no date to count from`);
69
+ return undefined;
70
+ }
71
+ if (date < 1 || date > surely)
72
+ problems.push(`givens.days: the count reaches past the ${ordinal(surely)}, and sheets show no month's length`);
73
+ if (target?.weekday !== undefined && weekday !== undefined && target.weekday !== weekday)
74
+ problems.push(`sheets[${t}].weekday: the sheet shows ${target.weekday}, and the day counted to is ${weekday}`);
75
+ if (problems.length > before)
76
+ return undefined;
77
+ return { answer: { number: date }, reads };
78
+ }
79
+ /** asked checks a date against the question marks of a page: the one it answers, if any. */
80
+ function asked(c, date, problems) {
81
+ if (c.ask !== undefined && !c.ask.includes(date))
82
+ problems.push(`ask: the page asks the ${c.ask.map(ordinal).join(", the ")}, and the question answers the ${ordinal(date)}`);
83
+ }
84
+ function weekdayOfDate(g, c, problems) {
85
+ const before = problems.length;
86
+ let date;
87
+ if (g.date !== undefined) {
88
+ date = int(g.date, "givens.date", problems, 1, c.days);
89
+ }
90
+ else if (c.ask?.length === 1)
91
+ date = c.ask[0];
92
+ else if (c.ask === undefined && c.marks?.length === 1)
93
+ date = c.marks[0].day;
94
+ else {
95
+ problems.push("givens.date: the page marks or asks no one date, so name it");
96
+ return undefined;
97
+ }
98
+ if (problems.length > before)
99
+ return undefined;
100
+ asked(c, date, problems);
101
+ if (problems.length > before)
102
+ return undefined;
103
+ const weekday = columnsOf(c)[cellOf(c, date).col];
104
+ return { answer: { text: dayWords[weekday] }, reads: [...pageReads(c), ...markOn(c, date)] };
105
+ }
106
+ function dateAfter(g, c, problems) {
107
+ const before = problems.length;
108
+ const n = int(g.days, "givens.days", problems, 1, 31);
109
+ const dates = datesOf(c);
110
+ const strip = styleOf(c) === "strip";
111
+ const weekdayAt = (i) => strip ? (c.first === undefined ? undefined : turn(c.first, i)) : turn(c.first, i);
112
+ let from;
113
+ if (typeof g.from === "number") {
114
+ from = dates.indexOf(g.from);
115
+ if (from < 0)
116
+ problems.push(`givens.from: the ${strip ? "strip" : "page"} shows no date ${g.from}`);
117
+ }
118
+ else if (isObj(g.from)) {
119
+ only(g.from, ["weekday", "nth"], "givens.from", problems, "a day of a weekday");
120
+ const weekday = oneOf(g.from.weekday, weekdays, "givens.from.weekday", problems);
121
+ const nth = int(g.from.nth, "givens.from.nth", problems, 1, 5);
122
+ if (strip && c.first === undefined)
123
+ problems.push("givens.from: the strip names no weekdays; count from a date");
124
+ else if (problems.length === before) {
125
+ const firstAt = dates.findIndex((_, i) => weekdayAt(i) === weekday);
126
+ from = firstAt + 7 * (nth - 1);
127
+ if (from >= dates.length)
128
+ problems.push(`givens.from: the ${strip ? "strip" : "page"} shows no ${ordinal(nth)} ${weekday}`);
129
+ }
130
+ }
131
+ else
132
+ problems.push('givens.from: must be a date, or a weekday and nth, as {"weekday": "Sat", "nth": 1}');
133
+ if (problems.length > before)
134
+ return undefined;
135
+ const to = from + n;
136
+ if (to >= dates.length) {
137
+ problems.push(`givens.days: ${n} days after the ${ordinal(dates[from])} is past the last date the ${strip ? "strip" : "page"} shows`);
138
+ return undefined;
139
+ }
140
+ const date = dates[to];
141
+ asked(c, date, problems);
142
+ if (problems.length > before)
143
+ return undefined;
144
+ const start = dates[from];
145
+ const marks = [...new Set([...markOn(c, start), ...markOn(c, date)])];
146
+ return { answer: { number: date }, reads: [...pageReads(c), ...marks] };
147
+ }
148
+ const takes = {
149
+ sheets: ["days", "sheet", "is"],
150
+ page: ["days", "from"],
151
+ "weekday-of-date": ["date"],
152
+ };
153
+ /** work answers a question of a scene, or lists why it cannot. */
154
+ function work(ask, givens, c, problems) {
155
+ if (givens !== undefined && !isObj(givens)) {
156
+ problems.push("givens: must be an object");
157
+ return undefined;
158
+ }
159
+ const g = givens ?? {};
160
+ const style = styleOf(c);
161
+ const sheets = style === "sheets";
162
+ only(g, takes[ask === "weekday-of-date" ? ask : sheets ? "sheets" : "page"], "givens", problems, `the givens of ${ask} on ${sheets ? "torn-off sheets" : `a ${style}`}`);
163
+ if ((ask === "weekday-after" || ask === "weekday-before") && !sheets)
164
+ problems.push(`style: ${ask} counts from torn-off sheets; a ${style} shows the weekdays`);
165
+ if (ask === "weekday-of-date" && style !== "month")
166
+ problems.push(`style: ${ask} reads a month page, not ${sheets ? "torn-off sheets" : "a strip"}`);
167
+ if (problems.length > 0)
168
+ return undefined;
169
+ if (sheets)
170
+ return onSheets(ask, g, c, problems);
171
+ return ask === "weekday-of-date" ? weekdayOfDate(g, c, problems) : dateAfter(g, c, problems);
172
+ }
173
+ function check(question, givens, scene) {
174
+ const problems = [];
175
+ const read = askOf(question, members, problems);
176
+ if (read && scene)
177
+ work(read.ask, givens, scene, problems);
178
+ else if (read)
179
+ sceneless(read.ask, "a scene of calendar", problems);
180
+ return problems;
181
+ }
182
+ function solve(scene, question, givens) {
183
+ if (!scene)
184
+ return noScene;
185
+ const worked = work(question.ask, givens, scene, []);
186
+ return solved(worked.answer, worked.reads);
187
+ }
188
+ /**
189
+ * The facts of a calendar are its sheets, or the members of its page that
190
+ * show what a date is — its first weekday, its days, its blots, its
191
+ * question marks — and its marks.
192
+ */
193
+ function facts(scene) {
194
+ if (styleOf(scene) === "sheets")
195
+ return (scene.sheets ?? []).map((_, i) => `sheets[${i}]`);
196
+ return [...pageReads(scene), ...(scene.marks ?? []).map((_, i) => `marks[${i}]`)];
197
+ }
198
+ /** calendarSolver counts the days of the week: the questions of CalendarQuestion. */
199
+ export const calendarSolver = {
200
+ name: "calendar",
201
+ kinds: ["calendar"],
202
+ questions: Object.keys(members),
203
+ check,
204
+ facts,
205
+ solve,
206
+ };
@@ -0,0 +1,20 @@
1
+ import { type Obj, type Problems } from "../read.ts";
2
+ import { type Offered, type Proof } from "./proof.ts";
3
+ /**
4
+ * choicesOf reads the picture options a question answers among: two or
5
+ * more, each a scene of kind that each finds nothing wrong with, a problem
6
+ * at the choice's path, choices[1]. The gate has checked each scene as
7
+ * render does. It gives the choices typed, every one of them even where one
8
+ * is refused, so a caller reads them only once the problems are none.
9
+ */
10
+ export declare function choicesOf<S>(offered: Offered | undefined, ask: string, kind: string, each: (choice: S, path: string, problems: Problems) => void, problems: Problems): S[];
11
+ /** alike refuses two choices that look the same: keyOf names what a choice shows. */
12
+ export declare function alike<S>(choices: readonly S[], keyOf: (choice: S) => string, problems: Problems): void;
13
+ /** shows refuses a choice whose show is not the one its question reads, fallback when it names none. */
14
+ export declare function shows(choice: Obj, path: string, show: string, fallback: string, problems: Problems): void;
15
+ /**
16
+ * pick answers a question asked of picture options: the letter of the one
17
+ * choice whose key is the answer's. None that is right has no answer, and
18
+ * two or more have their letters as candidates.
19
+ */
20
+ export declare function pick<S>(choices: readonly S[], answer: string, keyOf: (choice: S) => string, reads: string[], what: string): Proof;
@@ -0,0 +1,51 @@
1
+ // A question answered among the task's picture options: scenes the gate gives a solver as the
2
+ // choices of what the task offers, lettered A, B, … by their places, as the writer of tasks draws
3
+ // them together in one picture of choices.
4
+ import { isObj } from "../read.js";
5
+ import { letters, solved, unsolved } from "./proof.js";
6
+ /**
7
+ * choicesOf reads the picture options a question answers among: two or
8
+ * more, each a scene of kind that each finds nothing wrong with, a problem
9
+ * at the choice's path, choices[1]. The gate has checked each scene as
10
+ * render does. It gives the choices typed, every one of them even where one
11
+ * is refused, so a caller reads them only once the problems are none.
12
+ */
13
+ export function choicesOf(offered, ask, kind, each, problems) {
14
+ const choices = offered?.choices ?? [];
15
+ if (choices.length < 2)
16
+ problems.push(`choices: ${ask} answers by the letter of a picture option, and the task has ${choices.length === 0 ? "none" : "one"}; give two or more`);
17
+ choices.forEach((c, i) => {
18
+ const path = `choices[${i}]`;
19
+ if (!isObj(c) || c.kind !== kind)
20
+ problems.push(`${path}: must be a scene of ${kind}`);
21
+ else
22
+ each(c, path, problems);
23
+ });
24
+ return choices;
25
+ }
26
+ /** alike refuses two choices that look the same: keyOf names what a choice shows. */
27
+ export function alike(choices, keyOf, problems) {
28
+ const keys = choices.map(keyOf);
29
+ keys.forEach((k, i) => {
30
+ const first = keys.indexOf(k);
31
+ if (first < i)
32
+ problems.push(`choices[${i}]: looks the same as choices[${first}]`);
33
+ });
34
+ }
35
+ /** shows refuses a choice whose show is not the one its question reads, fallback when it names none. */
36
+ export function shows(choice, path, show, fallback, problems) {
37
+ if ((choice.show ?? fallback) !== show)
38
+ problems.push(`${path}.show: a picture option shows "${show}"`);
39
+ }
40
+ /**
41
+ * pick answers a question asked of picture options: the letter of the one
42
+ * choice whose key is the answer's. None that is right has no answer, and
43
+ * two or more have their letters as candidates.
44
+ */
45
+ export function pick(choices, answer, keyOf, reads, what) {
46
+ const right = choices.flatMap((o, i) => (keyOf(o) === answer ? [i] : []));
47
+ if (right.length === 1)
48
+ return solved({ letter: letters[right[0]] }, reads);
49
+ const candidates = right.map((i) => ({ letter: letters[i] }));
50
+ return unsolved(right.length === 0 ? `no picture option shows ${what}` : `options alike all show ${what}`, reads, candidates);
51
+ }
@@ -0,0 +1,54 @@
1
+ import type { ChoicesScene } from "../kinds/choices.ts";
2
+ import { type ClockScene } from "../kinds/clock.ts";
3
+ import { type Solver } from "./proof.ts";
4
+ /** A clock of a scene, by its index in clocks or by its label. */
5
+ export type ClockRef = number | string;
6
+ /**
7
+ * The givens of clocks: hours, the whole hours of each step of later-by
8
+ * (one step or two, one after the other) or of earlier-by; time, "H:MM",
9
+ * the time which-clock asks for.
10
+ */
11
+ export type ClockGivens = {
12
+ hours?: number[];
13
+ time?: string;
14
+ };
15
+ /**
16
+ * The questions of an analog clock, read round its 12 hours: a face shows
17
+ * no morning or evening, so 15:30 reads as 3:30, and after 12 comes 1. A
18
+ * time answers as a text "H:MM", H from 1 to 12, "8:30".
19
+ *
20
+ * - `read-time`: the time the clock shows, a time; clock names it where the
21
+ * scene has more than one.
22
+ * - `which-clock`: the letter of the clock that shows givens.time, of clocks
23
+ * offered as options: the options of a scene of choices or, with no scene,
24
+ * the task's picture options, each a clock scene of one clock.
25
+ * - `later-by`, `earlier-by`: the time givens.hours after or before the time
26
+ * of the clock, a time; it reads the clock that asks, if one does, as the
27
+ * place of the answer.
28
+ * - `how-long`: the whole hours from the clock from to the later clock to,
29
+ * the first two when the scene has two, a number; times whose minutes
30
+ * differ have no answer in whole hours.
31
+ * - `next-clock`: the time of the clock after the last of a row that moves
32
+ * on by one step, every clock read, the last one the asked one if it asks.
33
+ *
34
+ * A question that answers with a time answers instead, when the task's
35
+ * options are pictures, by the letter of the picture option whose clock
36
+ * shows that time, each a clock scene of one clock.
37
+ *
38
+ * A clock that shows its time in digits, or in a mirror, is refused: digits
39
+ * read the clock for the learner, and a mirror is a turn of a figure.
40
+ */
41
+ export type ClockQuestion = {
42
+ ask: "read-time" | "later-by" | "earlier-by";
43
+ clock?: ClockRef;
44
+ } | {
45
+ ask: "how-long";
46
+ from?: ClockRef;
47
+ to?: ClockRef;
48
+ } | {
49
+ ask: "which-clock" | "next-clock";
50
+ };
51
+ type Scene = ClockScene | ChoicesScene;
52
+ /** clockSolver reads analog clocks: the questions of ClockQuestion. */
53
+ export declare const clockSolver: Solver<Scene, ClockQuestion, ClockGivens>;
54
+ export {};
@@ -0,0 +1,240 @@
1
+ import { parse } from "../kinds/clock.js";
2
+ import { int, isObj, list, only } from "../read.js";
3
+ import { choicesOf } from "./choice-scenes.js";
4
+ import { letters, noScene, solved, unsolved, } from "./proof.js";
5
+ import { askOf, noGivens, refOf, sceneless } from "./question.js";
6
+ const members = {
7
+ "read-time": ["clock"],
8
+ "which-clock": [],
9
+ "later-by": ["clock"],
10
+ "earlier-by": ["clock"],
11
+ "how-long": ["from", "to"],
12
+ "next-clock": [],
13
+ };
14
+ const round = 12 * 60;
15
+ /** minutesOf is a time as minutes round the face, from 12:00. */
16
+ function minutesOf(time) {
17
+ const { h, m } = parse(time);
18
+ return (h % 12) * 60 + m;
19
+ }
20
+ /** timeOf words minutes round the face as a time, "H:MM", H from 1 to 12. */
21
+ function timeOf(minutes) {
22
+ const m = ((minutes % round) + round) % round;
23
+ const h = Math.floor(m / 60);
24
+ return `${h === 0 ? 12 : h}:${String(m % 60).padStart(2, "0")}`;
25
+ }
26
+ /** plain refuses a clock that shows its time in digits or in a mirror. */
27
+ function plain(c, path, problems) {
28
+ if (c.digital)
29
+ problems.push(`${path}.digital: the digits read the clock for the learner`);
30
+ if (c.mirror)
31
+ problems.push(`${path}.mirror: a clock in a mirror is a turn of a figure`);
32
+ }
33
+ /** shownRef reads a reference to a clock that shows a time, or the one there is. */
34
+ function shownRef(ref, scene, path, problems, fallback) {
35
+ const labels = scene.clocks.map((c) => c.label);
36
+ let at;
37
+ if (ref !== undefined)
38
+ at = refOf(ref, labels, "clocks", path, problems);
39
+ else if (fallback !== undefined)
40
+ at = fallback;
41
+ else {
42
+ const shown = scene.clocks.flatMap((c, i) => (c.ask ? [] : [i]));
43
+ if (shown.length === 1)
44
+ at = shown[0];
45
+ else
46
+ problems.push(`${path}: name the clock, as the scene shows ${shown.length}`);
47
+ }
48
+ if (at !== undefined && scene.clocks[at].ask) {
49
+ problems.push(`${path}: clocks[${at}] asks its time, and shows none`);
50
+ return undefined;
51
+ }
52
+ return at;
53
+ }
54
+ function checkClocks(q, ask, givens, scene, problems) {
55
+ scene.clocks.forEach((c, i) => {
56
+ plain(c, `clocks[${i}]`, problems);
57
+ });
58
+ const asking = scene.clocks.flatMap((c, i) => (c.ask ? [i] : []));
59
+ if (ask === "later-by" || ask === "earlier-by") {
60
+ if (!isObj(givens))
61
+ problems.push('givens: must give hours, as {"hours": [2]}');
62
+ else {
63
+ only(givens, ["hours"], "givens", problems, `the givens of ${ask}`);
64
+ list(givens.hours, "givens.hours", problems, 1, ask === "later-by" ? 2 : 1).forEach((h, i) => {
65
+ int(h, `givens.hours[${i}]`, problems, 1, 12);
66
+ });
67
+ }
68
+ }
69
+ else
70
+ noGivens(givens, problems);
71
+ if (ask === "next-clock") {
72
+ if (asking.some((i) => i !== scene.clocks.length - 1))
73
+ problems.push(`clocks[${asking[0]}]: only the last clock of a row asks its time`);
74
+ if (scene.clocks.length - asking.length < 2)
75
+ problems.push("clocks: a row moves on by a step from two clocks or more");
76
+ return;
77
+ }
78
+ if (asking.length > 1)
79
+ problems.push("clocks: more than one clock asks its time");
80
+ if (ask === "how-long") {
81
+ const two = scene.clocks.length === 2 && asking.length === 0;
82
+ if (!two && (q.from === undefined || q.to === undefined))
83
+ problems.push("question: name the clocks from and to, as the scene has more than two");
84
+ const from = shownRef(q.from, scene, "question.from", problems, two ? 0 : undefined);
85
+ const to = shownRef(q.to, scene, "question.to", problems, two ? 1 : undefined);
86
+ if (from === undefined || to === undefined)
87
+ return;
88
+ if (from === to)
89
+ problems.push("question.to: is the same clock as question.from");
90
+ const a = minutesOf(scene.clocks[from].time);
91
+ const b = minutesOf(scene.clocks[to].time);
92
+ if (a % 60 !== b % 60)
93
+ problems.push("question: the minutes of the two clocks differ, so no whole hours lie between");
94
+ else if (a === b)
95
+ problems.push("question: the two clocks show the same time");
96
+ return;
97
+ }
98
+ shownRef(q.clock, scene, "question.clock", problems);
99
+ if (ask === "read-time" && asking.length > 0)
100
+ problems.push(`clocks[${asking[0]}]: asks a time that read-time does not answer`);
101
+ }
102
+ /** oneClock refuses an option that is not a clock scene of one plain clock that shows its time. */
103
+ function oneClock(option, path, problems) {
104
+ const o = option;
105
+ if (!isObj(o) || o.kind !== "clock" || !Array.isArray(o.clocks) || o.clocks.length !== 1) {
106
+ problems.push(`${path}: must be a clock scene of one clock`);
107
+ return;
108
+ }
109
+ const c = o.clocks[0];
110
+ plain(c, `${path}.clocks[0]`, problems);
111
+ if (c.ask)
112
+ problems.push(`${path}.clocks[0]: asks its time, and shows none`);
113
+ }
114
+ function checkWhich(givens, scene, offered, problems) {
115
+ if (!isObj(givens))
116
+ problems.push('givens: must give the time, as {"time": "4:00"}');
117
+ else {
118
+ only(givens, ["time"], "givens", problems, "the givens of which-clock");
119
+ if (typeof givens.time !== "string" || parse(givens.time) === undefined)
120
+ problems.push('givens.time: must be a time "H:MM"');
121
+ }
122
+ const offers = offered?.choices ?? [];
123
+ if (!scene) {
124
+ if (offers.length === 0)
125
+ sceneless("which-clock", "clocks offered as options, a scene of choices or the task's picture options", problems);
126
+ else
127
+ choicesOf(offered, "which-clock", "clock", oneClock, problems);
128
+ return;
129
+ }
130
+ if (scene.kind !== "choices") {
131
+ problems.push("scene.kind: which-clock reads a scene of choices, each option a clock");
132
+ return;
133
+ }
134
+ if (offers.length > 0)
135
+ problems.push("choices: the scene of choices draws the clocks; offer them once");
136
+ scene.options.forEach((option, i) => {
137
+ oneClock(option, `options[${i}]`, problems);
138
+ });
139
+ }
140
+ function check(question, givens, scene, offered) {
141
+ const problems = [];
142
+ const read = askOf(question, members, problems);
143
+ if (!read)
144
+ return problems;
145
+ const { q, ask } = read;
146
+ if (ask === "which-clock") {
147
+ checkWhich(givens, scene, offered, problems);
148
+ return problems;
149
+ }
150
+ if (!scene) {
151
+ sceneless(ask, "a scene of clocks", problems);
152
+ return problems;
153
+ }
154
+ if (scene.kind !== "clock") {
155
+ problems.push(`scene.kind: ${ask} reads a scene of clocks`);
156
+ return problems;
157
+ }
158
+ checkClocks(q, ask, givens, scene, problems);
159
+ if ((offered?.choices ?? []).length > 0) {
160
+ if (ask === "how-long")
161
+ problems.push("choices: how-long answers with a number of hours, not a picture option");
162
+ else
163
+ choicesOf(offered, ask, "clock", oneClock, problems);
164
+ }
165
+ return problems;
166
+ }
167
+ /** shows answers by the letter of the one clock offered that shows a time, minutes round the face. */
168
+ function shows(clocks, want, reads) {
169
+ const hits = clocks.flatMap((o, i) => minutesOf(o.clocks[0].time) === want ? [{ letter: letters[i] }] : []);
170
+ if (hits.length === 1)
171
+ return solved(hits[0], reads);
172
+ return unsolved(hits.length === 0
173
+ ? `no option shows ${timeOf(want)}`
174
+ : `${hits.length} options show ${timeOf(want)}`, reads, hits);
175
+ }
176
+ function solve(scene, question, givens, offered) {
177
+ const offers = (offered?.choices ?? []);
178
+ if (question.ask === "which-clock") {
179
+ if (scene?.kind === "choices") {
180
+ const options = scene.options;
181
+ return shows(options, minutesOf(givens.time), options.map((_, i) => `options[${i}]`));
182
+ }
183
+ if (offers.length === 0)
184
+ return noScene;
185
+ return shows(offers, minutesOf(givens.time), []);
186
+ }
187
+ if (!scene)
188
+ return noScene;
189
+ const proof = solveClocks(scene, question, givens);
190
+ if (offers.length === 0 || !proof.proved || !("text" in proof.answer))
191
+ return proof;
192
+ return shows(offers, minutesOf(proof.answer.text), proof.reads);
193
+ }
194
+ function solveClocks(s, question, givens) {
195
+ const labels = s.clocks.map((c) => c.label);
196
+ const indexOf = (ref, fallback) => ref === undefined ? fallback : typeof ref === "number" ? ref : labels.indexOf(ref);
197
+ const shown = s.clocks.flatMap((c, i) => (c.ask ? [] : [i]));
198
+ const asking = s.clocks.flatMap((c, i) => (c.ask ? [`clocks[${i}]`] : []));
199
+ const at = (i) => minutesOf(s.clocks[i].time);
200
+ switch (question.ask) {
201
+ case "read-time":
202
+ case "later-by":
203
+ case "earlier-by": {
204
+ const i = indexOf(question.clock, shown[0]);
205
+ const hours = (givens?.hours ?? []).reduce((a, b) => a + b, 0);
206
+ const by = question.ask === "read-time" ? 0 : question.ask === "later-by" ? hours : -hours;
207
+ const reads = [`clocks[${i}]`, ...(question.ask === "read-time" ? [] : asking)];
208
+ return solved({ text: timeOf(at(i) + by * 60) }, reads);
209
+ }
210
+ case "how-long": {
211
+ const from = indexOf(question.from, 0);
212
+ const to = indexOf(question.to, 1);
213
+ const reads = [`clocks[${from}]`, `clocks[${to}]`];
214
+ return solved({ number: ((((at(to) - at(from)) % round) + round) % round) / 60 }, reads);
215
+ }
216
+ default: {
217
+ const reads = s.clocks.map((_, i) => `clocks[${i}]`);
218
+ const times = shown.map(at);
219
+ const steps = times.slice(1).map((t, i) => (((t - times[i]) % round) + round) % round);
220
+ if (steps.some((d) => d !== steps[0]))
221
+ return unsolved("the clocks of the row do not move on by one step", reads);
222
+ return solved({ text: timeOf(times[times.length - 1] + steps[0]) }, reads);
223
+ }
224
+ }
225
+ }
226
+ /** The facts of clocks are the clocks, each its time or its question mark; of choices, the options. */
227
+ function facts(scene) {
228
+ if (scene.kind === "choices")
229
+ return scene.options.map((_, i) => `options[${i}]`);
230
+ return scene.clocks.map((_, i) => `clocks[${i}]`);
231
+ }
232
+ /** clockSolver reads analog clocks: the questions of ClockQuestion. */
233
+ export const clockSolver = {
234
+ name: "clock",
235
+ kinds: ["clock", "choices"],
236
+ questions: Object.keys(members),
237
+ check,
238
+ facts,
239
+ solve,
240
+ };
@@ -0,0 +1,71 @@
1
+ import type { CoinsScene } from "../kinds/coins.ts";
2
+ import type { HolderRef } from "./groups.ts";
3
+ import { type Solver } from "./proof.ts";
4
+ /** A good on the shelf of a scene of coins, by its index in goods or by its label. */
5
+ export type GoodRef = number | string;
6
+ /**
7
+ * The questions about a scene of coins. A price is the question's own,
8
+ * price, or the tag of a good on the shelf, good — the tag is the price of
9
+ * all the copies it stands under. Money is the coins and notes of one group,
10
+ * in, or of all the groups; a coin or note shown with "?", or a price tag
11
+ * that is not a whole number, cannot be read, so a question that needs it is
12
+ * refused.
13
+ *
14
+ * - `total`: how much the money is worth, a number.
15
+ * - `can-pay`: whether the money pays the price, a truth: it is enough,
16
+ * change given; with exact, some of the coins and notes make the price
17
+ * exactly, every sum of them tried.
18
+ * - `fewest-coins`: the fewest coins and notes, alike, that make the price
19
+ * exactly: from the money itself, each piece once; with any, from the
20
+ * values the money shows, as many of each as needed. A price they cannot
21
+ * make has no answer.
22
+ * - `change`: the change from paying the price with paid, or with all the
23
+ * money when paid is not given, a number; less than the price has no
24
+ * answer.
25
+ * - `which-holder-most`, `which-holder-fewest`: the group with the most or
26
+ * the least money, named by its label as a text; every group needs a label.
27
+ * A tie has no answer.
28
+ * - `difference`: how much more money one group holds than the other, a
29
+ * number.
30
+ * - `missing-coin`: what the coins or notes shown with "?" are each worth,
31
+ * when the money is worth total, or the tag of good, a number; the "?"
32
+ * stands on one item of the money, which it reads too. A worth that is no
33
+ * whole number from 1 has no answer.
34
+ *
35
+ * It takes no givens.
36
+ */
37
+ export type CoinsQuestion = {
38
+ ask: "total";
39
+ in?: HolderRef;
40
+ } | {
41
+ ask: "can-pay";
42
+ price?: number;
43
+ good?: GoodRef;
44
+ in?: HolderRef;
45
+ exact?: boolean;
46
+ } | {
47
+ ask: "fewest-coins";
48
+ price?: number;
49
+ good?: GoodRef;
50
+ in?: HolderRef;
51
+ any?: boolean;
52
+ } | {
53
+ ask: "change";
54
+ price?: number;
55
+ good?: GoodRef;
56
+ paid?: number;
57
+ in?: HolderRef;
58
+ } | {
59
+ ask: "which-holder-most" | "which-holder-fewest";
60
+ } | {
61
+ ask: "difference";
62
+ a: HolderRef;
63
+ b: HolderRef;
64
+ } | {
65
+ ask: "missing-coin";
66
+ total?: number;
67
+ good?: GoodRef;
68
+ in?: HolderRef;
69
+ };
70
+ /** coinsSolver counts the money of a scene of coins: the questions of CoinsQuestion. */
71
+ export declare const coinsSolver: Solver<CoinsScene, CoinsQuestion, Record<string, never>>;