@altnovatrail/widgets 0.1.3 → 0.2.1

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 (82) hide show
  1. package/README.md +1 -1
  2. package/kinds/cells/cells.handles.json +3 -3
  3. package/kinds/cells/cells.schema.json +69 -9
  4. package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
  5. package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
  6. package/kinds/cells/vectors/paper-drawn-lines.json +36 -1
  7. package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
  8. package/kinds/line/line.handles.json +195 -0
  9. package/kinds/line/line.schema.json +321 -0
  10. package/kinds/line/vectors/cases.json +213 -0
  11. package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
  12. package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
  13. package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
  14. package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
  15. package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
  16. package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
  17. package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
  18. package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
  19. package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
  20. package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
  21. package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
  22. package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
  23. package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
  24. package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
  25. package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
  26. package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
  27. package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
  28. package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
  29. package/kinds/line/vectors/invalid-line.json +34 -0
  30. package/kinds/line/vectors/invalid-ring.json +34 -0
  31. package/kinds/line/vectors/invalid.json +43 -0
  32. package/kinds/line/vectors/jump-unreachable.json +104 -0
  33. package/kinds/line/vectors/limit.json +40 -0
  34. package/kinds/line/vectors/op-not-in-scene.json +47 -0
  35. package/kinds/line/vectors/ring-cut.json +104 -0
  36. package/kinds/line/vectors/ring-every.json +107 -0
  37. package/kinds/line/vectors/ring-jump.json +135 -0
  38. package/lib/cores/arith.d.ts +62 -0
  39. package/lib/cores/arith.js +197 -0
  40. package/lib/cores/enumerate.js +2 -2
  41. package/lib/cores/kind.d.ts +28 -3
  42. package/lib/cores/kind.js +2 -2
  43. package/lib/cores/registry.d.ts +43 -0
  44. package/lib/cores/registry.js +87 -0
  45. package/lib/cores/search.d.ts +21 -3
  46. package/lib/cores/search.js +80 -17
  47. package/lib/kinds/cells/bishop.d.ts +1 -1
  48. package/lib/kinds/cells/index.d.ts +8 -0
  49. package/lib/kinds/cells/index.js +10 -2
  50. package/lib/kinds/cells/kind.d.ts +3 -2
  51. package/lib/kinds/cells/kind.js +26 -66
  52. package/lib/kinds/cells/knight.d.ts +1 -1
  53. package/lib/kinds/cells/operation.d.ts +21 -1
  54. package/lib/kinds/cells/paper.js +27 -13
  55. package/lib/kinds/cells/piece.d.ts +17 -6
  56. package/lib/kinds/cells/piece.js +119 -38
  57. package/lib/kinds/cells/polyomino.d.ts +14 -0
  58. package/lib/kinds/cells/polyomino.js +122 -35
  59. package/lib/kinds/cells/registry.js +1 -1
  60. package/lib/kinds/cells/schema.js +4 -1
  61. package/lib/kinds/cells/step.d.ts +1 -1
  62. package/lib/kinds/line/cut.d.ts +2 -0
  63. package/lib/kinds/line/cut.js +63 -0
  64. package/lib/kinds/line/goals.d.ts +22 -0
  65. package/lib/kinds/line/goals.js +137 -0
  66. package/lib/kinds/line/index.d.ts +13 -0
  67. package/lib/kinds/line/index.js +17 -0
  68. package/lib/kinds/line/jump.d.ts +10 -0
  69. package/lib/kinds/line/jump.js +95 -0
  70. package/lib/kinds/line/kind.d.ts +25 -0
  71. package/lib/kinds/line/kind.js +125 -0
  72. package/lib/kinds/line/line.d.ts +63 -0
  73. package/lib/kinds/line/line.js +157 -0
  74. package/lib/kinds/line/operation.d.ts +69 -0
  75. package/lib/kinds/line/operation.js +1 -0
  76. package/lib/kinds/line/posts.d.ts +11 -0
  77. package/lib/kinds/line/posts.js +85 -0
  78. package/lib/kinds/line/registry.d.ts +2 -0
  79. package/lib/kinds/line/registry.js +11 -0
  80. package/lib/kinds/line/schema.d.ts +4 -0
  81. package/lib/kinds/line/schema.js +157 -0
  82. package/package.json +9 -2
@@ -81,19 +81,48 @@ function everyShape(n) {
81
81
  }
82
82
  return shapes;
83
83
  }
84
+ /** The rows and the columns a form spans. */
85
+ function spans(form) {
86
+ return [Math.max(...form.map(([r]) => r)) + 1, Math.max(...form.map(([, c]) => c)) + 1];
87
+ }
88
+ /** Each place on the sheet where a form lies, its cells by index, holes and all. */
89
+ function* placesOn(sheet, form) {
90
+ const [height, width] = spans(form);
91
+ for (let top = 0; top + height <= sheet.rows; top++) {
92
+ for (let left = 0; left + width <= sheet.cols; left++) {
93
+ yield form.map(([r, c]) => (top + r) * sheet.cols + left + c);
94
+ }
95
+ }
96
+ }
97
+ /**
98
+ * How many places on the sheet off the holes the forms lie on, without making them: two forms,
99
+ * each at its grid's corner, never lie on the same cells.
100
+ */
101
+ function fitsOf(sheet, forms) {
102
+ let fits = 0;
103
+ for (const form of forms) {
104
+ const [height, width] = spans(form);
105
+ if (sheet.holes.size === 0) {
106
+ fits += Math.max(0, sheet.rows - height + 1) * Math.max(0, sheet.cols - width + 1);
107
+ continue;
108
+ }
109
+ for (let top = 0; top + height <= sheet.rows; top++) {
110
+ for (let left = 0; left + width <= sheet.cols; left++) {
111
+ if (!form.some(([r, c]) => sheet.holes.has((top + r) * sheet.cols + left + c)))
112
+ fits++;
113
+ }
114
+ }
115
+ }
116
+ return fits;
117
+ }
84
118
  /** Every place on the sheet off the holes where one of the forms lies, each once, in order. */
85
119
  function placingsOf(sheet, all) {
86
120
  const found = new Map();
87
121
  for (const form of all) {
88
- const height = Math.max(...form.map(([r]) => r)) + 1;
89
- const width = Math.max(...form.map(([, c]) => c)) + 1;
90
- for (let top = 0; top + height <= sheet.rows; top++) {
91
- for (let left = 0; left + width <= sheet.cols; left++) {
92
- const cells = form.map(([r, c]) => (top + r) * sheet.cols + left + c);
93
- if (cells.some((cell) => sheet.holes.has(cell)))
94
- continue;
95
- found.set(cells.join(","), cells);
96
- }
122
+ for (const cells of placesOn(sheet, form)) {
123
+ if (cells.some((cell) => sheet.holes.has(cell)))
124
+ continue;
125
+ found.set(cells.join(","), cells);
97
126
  }
98
127
  }
99
128
  return [...found.values()].sort((a, b) => {
@@ -104,6 +133,40 @@ function placingsOf(sheet, all) {
104
133
  return a.length - b.length;
105
134
  });
106
135
  }
136
+ // The places of each scene's figures, by its parameters. They are made the first time a state needs
137
+ // them, not as the scene is read: on a large sheet they are a hundred thousand and more, and a scene
138
+ // whose first state's moves pass the limit of work stops on their count alone.
139
+ const placesByOptions = new WeakMap();
140
+ /**
141
+ * The places of the scene's figures, each its cells by index, as a state's figures name them. The
142
+ * sheet is the one the options were read by: the places are kept by the options alone.
143
+ */
144
+ export function placesOf(sheet, options) {
145
+ let places = placesByOptions.get(options);
146
+ if (places === undefined) {
147
+ const placings = placingsOf(sheet, options.forms);
148
+ places = { placings, placed: new Map(placings.map((placing, i) => [placing.join(","), i])) };
149
+ placesByOptions.set(options, places);
150
+ }
151
+ return places;
152
+ }
153
+ // The places still free in each state, by its figures read, which the count of its moves and their
154
+ // list share.
155
+ const freeByLaid = new WeakMap();
156
+ /** The places of the state's figures that no figure laid covers a cell of, by their indices. */
157
+ function freeOf(sheet, options, laid) {
158
+ let free = freeByLaid.get(laid);
159
+ if (free === undefined) {
160
+ const found = [];
161
+ placesOf(sheet, options).placings.forEach((placing, i) => {
162
+ if (placing.every((cell) => !laid.covered.has(cell)))
163
+ found.push(i);
164
+ });
165
+ free = found;
166
+ freeByLaid.set(laid, free);
167
+ }
168
+ return free;
169
+ }
107
170
  // The figures of each state read so far, by the array of its member "polyomino", for each scene's
108
171
  // parameters: null for a member the rules never make.
109
172
  const laidByOptions = new WeakMap();
@@ -133,6 +196,9 @@ export function laidOf(sheet, options, state) {
133
196
  function readLaid(sheet, options, value) {
134
197
  if (value.length > options.pieces)
135
198
  return null;
199
+ if (value.length === 0)
200
+ return { figures: [], covered: new Set() };
201
+ const { placed } = placesOf(sheet, options);
136
202
  const figures = [];
137
203
  const covered = new Set();
138
204
  let last = -1;
@@ -140,7 +206,7 @@ function readLaid(sheet, options, value) {
140
206
  if (!Array.isArray(figure))
141
207
  return null;
142
208
  const cells = figure.map((name) => typeof name === "string" ? cellAt(sheet, name) : undefined);
143
- const placing = options.placed.get(cells.join(","));
209
+ const placing = placed.get(cells.join(","));
144
210
  const first = cells[0];
145
211
  if (placing === undefined || first === undefined || first <= last)
146
212
  return null;
@@ -156,9 +222,10 @@ function readLaid(sheet, options, value) {
156
222
  }
157
223
  /** The member of the state with the figures given, by their places. */
158
224
  function laidJson(sheet, options, figures) {
225
+ const { placings } = placesOf(sheet, options);
159
226
  return [...figures]
160
227
  .sort((a, b) => a - b)
161
- .map((placing) => options.placings[placing].map((cell) => cellName(sheet, cell)));
228
+ .map((placing) => placings[placing].map((cell) => cellName(sheet, cell)));
162
229
  }
163
230
  function refuse(reason) {
164
231
  return { ok: false, reason };
@@ -308,16 +375,14 @@ export const polyomino = {
308
375
  const all = cells === undefined
309
376
  ? shapes.flatMap((shape) => forms(shape, flags.turn, flags.flip))
310
377
  : everyShape(cells);
311
- const placings = placingsOf(sheet, all);
312
- if (placings.length === 0) {
378
+ // Shapes given may turn into one another, so a form is kept once.
379
+ const once = [...new Map(all.map((form) => [formKey(form), form])).values()];
380
+ const fits = fitsOf(sheet, once);
381
+ if (fits === 0) {
313
382
  errors.push({ path, message: "no figure fits on the sheet off its holes" });
314
383
  return undefined;
315
384
  }
316
- return {
317
- pieces,
318
- placings,
319
- placed: new Map(placings.map((placing, i) => [placing.join(","), i])),
320
- };
385
+ return { pieces, forms: once, fits };
321
386
  },
322
387
  start: () => [],
323
388
  play(sheet, options, state, move) {
@@ -333,7 +398,8 @@ export const polyomino = {
333
398
  const laid = laidOf(sheet, options, state);
334
399
  if (laid === undefined)
335
400
  return refuse("no-figures");
336
- const figures = laid.figures.filter((placing) => !options.placings[placing].includes(cell));
401
+ const { placings } = placesOf(sheet, options);
402
+ const figures = laid.figures.filter((placing) => !placings[placing].includes(cell));
337
403
  if (figures.length === laid.figures.length)
338
404
  return refuse("not-laid");
339
405
  return { ok: true, state: { ...state, polyomino: laidJson(sheet, options, figures) } };
@@ -350,7 +416,7 @@ export const polyomino = {
350
416
  return refuse("all-laid");
351
417
  if (cells.some((cell) => sheet.holes.has(cell)))
352
418
  return refuse("hole");
353
- const placing = options.placed.get([...cells].sort((a, b) => a - b).join(","));
419
+ const placing = placesOf(sheet, options).placed.get([...cells].sort((a, b) => a - b).join(","));
354
420
  if (placing === undefined || new Set(cells).size !== cells.length)
355
421
  return refuse("not-a-shape");
356
422
  if (cells.some((cell) => laid.covered.has(cell)))
@@ -366,13 +432,28 @@ export const polyomino = {
366
432
  if (laid === undefined || laid.figures.length >= options.pieces) {
367
433
  return [];
368
434
  }
369
- return options.placings
370
- .filter((placing) => placing.every((cell) => !laid.covered.has(cell)))
371
- .map((placing) => ({ lay: { cells: placing.map((cell) => cellName(sheet, cell)) } }));
435
+ const { placings } = placesOf(sheet, options);
436
+ return freeOf(sheet, options, laid).map((i) => ({
437
+ lay: { cells: placings[i].map((cell) => cellName(sheet, cell)) },
438
+ }));
372
439
  },
440
+ // The first state's places are counted from the forms, so that a scene whose first state passes
441
+ // the limit of work stops before they are made.
442
+ leastMoves(sheet, options, state) {
443
+ const laid = laidOf(sheet, options, state);
444
+ if (laid === undefined || laid.figures.length >= options.pieces) {
445
+ return 0;
446
+ }
447
+ return laid.figures.length === 0 ? options.fits : freeOf(sheet, options, laid).length;
448
+ },
449
+ };
450
+ const said = {
451
+ beside: "",
452
+ alone: " alone, so that its states are the figures",
453
+ one: " alone with pieces 1, so that its states are the one figure's places",
373
454
  };
374
455
  /** The goal's figures, the polyomino's parameters given, or an error where the scene lays none. */
375
- function readFigures(value, path, errors, operations, alone) {
456
+ function readFigures(value, path, errors, operations, takes) {
376
457
  const before = errors.length;
377
458
  object(value, path, errors, []);
378
459
  if (!operations.has("polyomino")) {
@@ -381,13 +462,19 @@ function readFigures(value, path, errors, operations, alone) {
381
462
  const others = [...operations]
382
463
  .filter(([named, options]) => named !== "polyomino" && options !== undefined)
383
464
  .map(([named]) => named);
384
- if (alone && operations.get("polyomino") !== undefined && others.length > 0) {
465
+ if (takes !== "beside" && operations.get("polyomino") !== undefined && others.length > 0) {
385
466
  errors.push({
386
467
  path,
387
468
  message: `the scene allows ${others.join(", ")} beside polyomino: a count of figures counts the figures' states alone`,
388
469
  });
389
470
  }
390
471
  const options = operations.get("polyomino");
472
+ if (takes === "one" && options !== undefined && options.pieces !== 1) {
473
+ errors.push({
474
+ path,
475
+ message: "a count of one figure takes pieces 1 of the operation polyomino, so that no second figure lies at once",
476
+ });
477
+ }
391
478
  return errors.length > before ? undefined : options === undefined ? {} : { polyomino: options };
392
479
  }
393
480
  /** The figures of the state as a goal reads them, by the figures' parameters where they read. */
@@ -396,15 +483,15 @@ function figuresOf(sheet, goal, state) {
396
483
  }
397
484
  /** The cells of the sheet that are no hole. */
398
485
  const free = (sheet) => sheet.rows * sheet.cols - sheet.holes.size;
399
- function goalOf(name, description, alone, meets, asks) {
486
+ function goalOf(name, description, takes, meets, asks) {
400
487
  return {
401
488
  name,
402
489
  schema: {
403
- description: `${description} The scene allows the operation polyomino${alone ? " alone, so that its states are the figures" : ""}.`,
490
+ description: `${description} The scene allows the operation polyomino${said[takes]}.`,
404
491
  type: "object",
405
492
  unevaluatedProperties: false,
406
493
  },
407
- read: (value, _sheet, path, errors, operations) => readFigures(value, path, errors, operations, alone),
494
+ read: (value, _sheet, path, errors, operations) => readFigures(value, path, errors, operations, takes),
408
495
  meets(sheet, goal, state) {
409
496
  const laid = figuresOf(sheet, goal, state);
410
497
  return laid !== undefined && meets(sheet, laid);
@@ -415,23 +502,23 @@ function goalOf(name, description, alone, meets, asks) {
415
502
  const covers = (sheet, laid) => laid.covered.size === free(sheet);
416
503
  const one = (_sheet, laid) => laid.figures.length === 1;
417
504
  /** The figures cover every cell but the holes; the key is the fewest figures. */
418
- export const polyominoCover = goalOf("polyomino_cover", "The figures cover every cell that is no hole; the key is the fewest figures laid.", false, covers);
505
+ export const polyominoCover = goalOf("polyomino_cover", "The figures cover every cell that is no hole; the key is the fewest figures laid.", "beside", covers);
419
506
  /** As polyomino_cover, and the key counts the ways. */
420
- export const polyominoTilings = goalOf("polyomino_tilings", "The figures cover every cell that is no hole; the key counts the ways, two ways one where their figures cover the same cells.", true, covers, () => ({ key: "count" }));
507
+ export const polyominoTilings = goalOf("polyomino_tilings", "The figures cover every cell that is no hole; the key counts the ways, two ways one where their figures cover the same cells.", "alone", covers, () => ({ key: "count" }));
421
508
  /** Any figures; the key is the most figures that lie on the sheet at once. */
422
- export const polyominoMost = goalOf("polyomino_most", "Met by every state; the key is the most figures that lie on the sheet at once.", false, () => true, (sheet, goal) => ({
509
+ export const polyominoMost = goalOf("polyomino_most", "Met by every state; the key is the most figures that lie on the sheet at once.", "beside", () => true, (sheet, goal) => ({
423
510
  key: "max",
424
511
  measure: (state) => figuresOf(sheet, goal, state)?.figures.length ?? 0,
425
512
  }));
426
513
  /** Any figures; the key is the fewest cells left empty, no figure on them, the holes aside. */
427
- export const polyominoEmpty = goalOf("polyomino_empty", "Met by every state; the key is the fewest cells that are no hole and no figure covers.", false, () => true, (sheet, goal) => ({
514
+ export const polyominoEmpty = goalOf("polyomino_empty", "Met by every state; the key is the fewest cells that are no hole and no figure covers.", "beside", () => true, (sheet, goal) => ({
428
515
  key: "min",
429
516
  measure: (state) => free(sheet) - (figuresOf(sheet, goal, state)?.covered.size ?? 0),
430
517
  }));
431
518
  /** One figure on the sheet; the key counts its places. */
432
- export const polyominoPlacings = goalOf("polyomino_placings", "One figure lies on the sheet; the key counts the places it may lie on, two places one where they cover the same cells.", true, one, () => ({ key: "count" }));
519
+ export const polyominoPlacings = goalOf("polyomino_placings", "One figure lies on the sheet; the key counts the places it may lie on, two places one where they cover the same cells.", "one", one, () => ({ key: "count" }));
433
520
  /** One figure on the sheet; the key counts its shapes up to a move, a turn and a turn over. */
434
- export const polyominoShapes = goalOf("polyomino_shapes", "One figure lies on the sheet; the key counts its shapes, two figures one shape where one moved, turned or turned over lies on the other.", true, one, (sheet, goal) => ({ key: "count", images: (state) => sameShape(sheet, goal, state) }));
521
+ export const polyominoShapes = goalOf("polyomino_shapes", "One figure lies on the sheet; the key counts its shapes, two figures one shape where one moved, turned or turned over lies on the other.", "one", one, (sheet, goal) => ({ key: "count", images: (state) => sameShape(sheet, goal, state) }));
435
522
  /**
436
523
  * Every state with one figure of the state's own figure's shape, wherever on the sheet off its
437
524
  * holes it lies, moved, turned or turned over: the class a count of shapes counts once.
@@ -442,7 +529,7 @@ function sameShape(sheet, goal, state) {
442
529
  if (goal.polyomino === undefined || figure === undefined) {
443
530
  return [state];
444
531
  }
445
- const squares = goal.polyomino.placings[figure].map((cell) => [Math.floor(cell / sheet.cols), cell % sheet.cols]);
532
+ const squares = placesOf(sheet, goal.polyomino).placings[figure].map((cell) => [Math.floor(cell / sheet.cols), cell % sheet.cols]);
446
533
  return placingsOf(sheet, forms(squares, true, true)).map((cells) => ({
447
534
  ...state,
448
535
  polyomino: [cells.map((cell) => cellName(sheet, cell))],
@@ -17,7 +17,7 @@ import { sieve, sieveLeast } from "./sieve.js";
17
17
  import { step, stepOn, stepReach } from "./step.js";
18
18
  import { mirrorSymmetric, quarterTurnSymmetric } from "./symmetric.js";
19
19
  export const registry = {
20
- version: "2.5",
20
+ version: "3.1",
21
21
  operations: [fold, paint, step, knight, bishop, paper, counter, sieve, path, cut, polyomino],
22
22
  goals: [
23
23
  allPainted,
@@ -28,6 +28,9 @@ export function cellsSchema(registry) {
28
28
  if (op.schema.state !== undefined) {
29
29
  state[op.schema.state.member] = op.schema.state.schema;
30
30
  }
31
+ if (op.schema.state?.beside !== undefined) {
32
+ state[op.schema.state.beside.member] = op.schema.state.beside.schema;
33
+ }
31
34
  }
32
35
  const goals = {};
33
36
  for (const goal of registry.goals) {
@@ -166,7 +169,7 @@ export function cellsSchema(registry) {
166
169
  maxProperties: 1,
167
170
  },
168
171
  state: {
169
- description: "A state: the cells' marks, and a member for each operation of the scene that keeps one.",
172
+ description: "A state: the cells' marks, and a member for each operation of the scene that keeps one, with a second beside it where the scene's parameters of the operation ask for it.",
170
173
  type: "object",
171
174
  properties: state,
172
175
  required: ["marks"],
@@ -1 +1 @@
1
- export declare const step: import("./operation.js").Operation<number>, stepOn: import("./operation.js").Goal<number>, stepReach: import("./operation.js").Goal<import("./piece.js").Reach>;
1
+ export declare const step: import("./operation.js").Operation<import("./piece.js").Start>, stepOn: import("./operation.js").Goal<import("./piece.js").On>, stepReach: import("./operation.js").Goal<import("./piece.js").Reach>;
@@ -0,0 +1,2 @@
1
+ import type { Operation } from "./operation.js";
2
+ export declare const cut: Operation<true>;
@@ -0,0 +1,63 @@
1
+ import { lastPlace, moveAt, placesOf, withPlace } from "./line.js";
2
+ import { noOptions, placeMove, readNone } from "./posts.js";
3
+ const isEnd = (line, place) => !line.closed && (place === line.first || place === lastPlace(line));
4
+ // The cuts at places that may be cut alone: a state reported from outside the kind may name an end.
5
+ const cuts = (line, state) => placesOf(line, state, "cuts").filter((place) => !isEnd(line, place));
6
+ function refuse(reason) {
7
+ return { ok: false, reason };
8
+ }
9
+ export const cut = {
10
+ name: "cut",
11
+ schema: {
12
+ options: noOptions("Cut the line, a log or a ribbon from its first place to its last, at any inner place, or a ring at any place; no parameters."),
13
+ move: placeMove("Cut at a place: never at an end of an open line."),
14
+ state: {
15
+ member: "cuts",
16
+ schema: {
17
+ description: "The places cut, by their numbers, in ascending order.",
18
+ type: "array",
19
+ items: { description: "A place cut.", $ref: "#/$defs/place" },
20
+ uniqueItems: true,
21
+ },
22
+ },
23
+ },
24
+ read: (value, _line, path, errors) => readNone(value, path, errors),
25
+ start: () => [],
26
+ play(line, _options, state, move) {
27
+ const at = moveAt(line, move);
28
+ if (at === undefined) {
29
+ return refuse("no-place");
30
+ }
31
+ if (isEnd(line, at)) {
32
+ return refuse("end");
33
+ }
34
+ const made = cuts(line, state);
35
+ if (made.includes(at)) {
36
+ return refuse("cut");
37
+ }
38
+ return { ok: true, state: { ...state, cuts: withPlace(made, at) } };
39
+ },
40
+ moves(line, _options, state) {
41
+ const made = new Set(cuts(line, state));
42
+ const moves = [];
43
+ for (let place = line.first; place <= lastPlace(line); place++) {
44
+ if (!made.has(place) && !isEnd(line, place)) {
45
+ moves.push({ at: place });
46
+ }
47
+ }
48
+ return moves;
49
+ },
50
+ counters: {
51
+ cuts: {
52
+ description: "The cuts made.",
53
+ value: (line, _options, state) => cuts(line, state).length,
54
+ },
55
+ pieces: {
56
+ description: "The pieces the cuts leave: on an open line one more than the cuts; on a ring as many as the cuts, and one where there is none.",
57
+ value(line, _options, state) {
58
+ const made = cuts(line, state).length;
59
+ return line.closed && made > 0 ? made : made + 1;
60
+ },
61
+ },
62
+ },
63
+ };
@@ -0,0 +1,22 @@
1
+ import type { Count, Goal } from "./operation.js";
2
+ /** The greatest value a goal asks a counter to have. */
3
+ export declare const maxValue = 1000000000;
4
+ export interface CountersOptions {
5
+ /**
6
+ * The counters a state meets the goal by, each with the value it is to have and its name, which
7
+ * the drawing shows the value beside.
8
+ */
9
+ readonly is: readonly (readonly [Count, number, string])[];
10
+ /** The counter whose greatest or least value the goal asks, by its name too, where it asks one. */
11
+ readonly asks?: {
12
+ readonly key: "max" | "min";
13
+ readonly count: Count;
14
+ readonly name: string;
15
+ };
16
+ }
17
+ export declare const counters: Goal<CountersOptions>;
18
+ export interface Stretch {
19
+ readonly from: number;
20
+ readonly to: number;
21
+ }
22
+ export declare const every: Goal<Stretch>;
@@ -0,0 +1,137 @@
1
+ // The goals of the kind line. A statement of counting gaps asks a number of a built line — how many
2
+ // gaps between six trees, how long a fence of eight posts — or how many moves build it — how many
3
+ // cuts make four pieces, how many jumps reach the top —, so one goal names counters: the values a
4
+ // state's counters are to have, and, where it asks a number of the state rather than the fewest
5
+ // moves, the counter whose greatest or least value it asks. The other asks a post on every place of
6
+ // a stretch, as pages read from one to another, both included, or bushes round a flowerbed.
7
+ import { divideWithRemainder } from "../../cores/arith.js";
8
+ import { integer, isObject, map, object, pointer, string } from "../../cores/json.js";
9
+ import { lastPlace, placeOn, placesOf, readPlace } from "./line.js";
10
+ /** The greatest value a goal asks a counter to have. */
11
+ export const maxValue = 1_000_000_000;
12
+ const counterName = (description) => ({ description, $ref: "#/$defs/counter" });
13
+ export const counters = {
14
+ name: "counters",
15
+ schema: {
16
+ description: "A state whose counters have the values given; the key is the fewest moves to one, or, where max or min names a counter, its greatest or least value over such states. At least one of is, max and min; never both max and min.",
17
+ type: "object",
18
+ properties: {
19
+ is: {
20
+ description: "The counters, each by its name, with the value it is to have.",
21
+ type: "object",
22
+ propertyNames: { $ref: "#/$defs/counter" },
23
+ additionalProperties: {
24
+ description: "The value the counter is to have.",
25
+ type: "integer",
26
+ minimum: 0,
27
+ maximum: maxValue,
28
+ },
29
+ minProperties: 1,
30
+ },
31
+ max: counterName("The counter whose greatest value the key is."),
32
+ min: counterName("The counter whose least value the key is."),
33
+ },
34
+ minProperties: 1,
35
+ // Never both max and min. Both are named again here: a validator in strict mode, as Ajv, takes
36
+ // a required member only where the same schema names it.
37
+ not: {
38
+ properties: {
39
+ max: { description: "Max, never beside min." },
40
+ min: { description: "Min, never beside max." },
41
+ },
42
+ required: ["max", "min"],
43
+ },
44
+ unevaluatedProperties: false,
45
+ },
46
+ read(value, _line, path, errors, known) {
47
+ const before = errors.length;
48
+ const o = object(value, path, errors, [], ["is", "max", "min"]);
49
+ if (o === undefined) {
50
+ return undefined;
51
+ }
52
+ const counter = (name, at) => {
53
+ const named = string(name, at, errors, /^[a-z][a-z0-9_]*$/, "a counter's name");
54
+ if (named !== undefined && !known.has(named)) {
55
+ errors.push({ path: at, message: "no counter of the scene has this name" });
56
+ }
57
+ return named === undefined ? undefined : known.get(named);
58
+ };
59
+ const is = [];
60
+ if (o.is !== undefined) {
61
+ const isPath = pointer(path, "is");
62
+ for (const [name, item] of Object.entries(map(o.is, isPath, errors) ?? {})) {
63
+ const at = pointer(isPath, name);
64
+ const count = counter(name, at);
65
+ const wanted = integer(item, at, errors, 0, maxValue);
66
+ if (count !== undefined && wanted !== undefined) {
67
+ is.push([count, wanted, name]);
68
+ }
69
+ }
70
+ }
71
+ const max = o.max === undefined ? undefined : counter(o.max, pointer(path, "max"));
72
+ const min = o.min === undefined ? undefined : counter(o.min, pointer(path, "min"));
73
+ if (o.max !== undefined && o.min !== undefined) {
74
+ errors.push({ path, message: "max or min, never both" });
75
+ }
76
+ const key = o.max !== undefined ? "max" : "min";
77
+ const measured = key === "max" ? max : min;
78
+ if (o[key] === undefined && (!isObject(o.is) || Object.keys(o.is).length === 0)) {
79
+ errors.push({ path, message: "a counter's value to have, or a counter to measure" });
80
+ }
81
+ if (errors.length > before) {
82
+ return undefined;
83
+ }
84
+ return measured === undefined
85
+ ? { is }
86
+ : { is, asks: { key, count: measured, name: String(o[key]) } };
87
+ },
88
+ meets: (_line, goal, state) => goal.is.every(([count, wanted]) => count(state) === wanted),
89
+ asks: (_line, goal) => goal.asks === undefined ? { key: "fewest" } : { key: goal.asks.key, measure: goal.asks.count },
90
+ };
91
+ export const every = {
92
+ name: "every",
93
+ schema: {
94
+ description: "A post on every place from one to another, both included; the whole line where neither is given.",
95
+ type: "object",
96
+ properties: {
97
+ from: {
98
+ description: "The first place of the stretch; the line's first where left out.",
99
+ $ref: "#/$defs/place",
100
+ },
101
+ to: {
102
+ description: "The last place of the stretch, never before from on an open line, and on a ring past the last place round to it where before from; the line's last where left out.",
103
+ $ref: "#/$defs/place",
104
+ },
105
+ },
106
+ unevaluatedProperties: false,
107
+ },
108
+ read(value, line, path, errors) {
109
+ const before = errors.length;
110
+ const o = object(value, path, errors, [], ["from", "to"]);
111
+ if (o === undefined) {
112
+ return undefined;
113
+ }
114
+ const from = o.from === undefined ? line.first : readPlace(line, o.from, pointer(path, "from"), errors);
115
+ const to = o.to === undefined ? lastPlace(line) : readPlace(line, o.to, pointer(path, "to"), errors);
116
+ if (from !== undefined && to !== undefined && to < from && !line.closed) {
117
+ errors.push({ path: pointer(path, "to"), message: "a place no earlier than from" });
118
+ }
119
+ return from === undefined || to === undefined || errors.length > before
120
+ ? undefined
121
+ : { from, to };
122
+ },
123
+ meets(line, stretch, state) {
124
+ const posts = new Set(placesOf(line, state, "posts"));
125
+ // On a ring the stretch goes forward from its first place, past the last to the first where it
126
+ // ends before it starts.
127
+ const count = line.closed
128
+ ? divideWithRemainder(stretch.to - stretch.from, line.places).remainder + 1
129
+ : stretch.to - stretch.from + 1;
130
+ for (let i = 0; i < count; i++) {
131
+ if (!posts.has(placeOn(line, stretch.from, i))) {
132
+ return false;
133
+ }
134
+ }
135
+ return true;
136
+ },
137
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The most states a scene's whole state graph may hold, at a build and on a device alike: the
3
+ * same as the kind cells', which bounds a hint on a phone; posts on eleven free places, the
4
+ * course's largest scene of posts, take 2,048 states.
5
+ */
6
+ export declare const limit = 3000;
7
+ /**
8
+ * The most work a scene's whole state graph may take, as the search core counts it, at a build and
9
+ * on a device alike: the same as the kind cells'. A state of the line carries a few numbers, so the
10
+ * course's scenes take a small part of it; a change of it proves every scene again.
11
+ */
12
+ export declare const workLimit = 6000000;
13
+ export declare const line: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./line.js").State, import("./kind.js").Move>;
@@ -0,0 +1,17 @@
1
+ // The kind line as a module's build and the widget take it: the registry's operations and goals,
2
+ // within the kind's one limit of states and one of work.
3
+ import { lineKind } from "./kind.js";
4
+ import { registry } from "./registry.js";
5
+ /**
6
+ * The most states a scene's whole state graph may hold, at a build and on a device alike: the
7
+ * same as the kind cells', which bounds a hint on a phone; posts on eleven free places, the
8
+ * course's largest scene of posts, take 2,048 states.
9
+ */
10
+ export const limit = 3_000;
11
+ /**
12
+ * The most work a scene's whole state graph may take, as the search core counts it, at a build and
13
+ * on a device alike: the same as the kind cells'. A state of the line carries a few numbers, so the
14
+ * course's scenes take a small part of it; a change of it proves every scene again.
15
+ */
16
+ export const workLimit = 6_000_000;
17
+ export const line = lineKind(registry, limit, workLimit);
@@ -0,0 +1,10 @@
1
+ import type { Operation } from "./operation.js";
2
+ export interface Jumper {
3
+ /** The place it starts on. */
4
+ readonly from: number;
5
+ /** The places one jump spans. */
6
+ readonly step: number;
7
+ /** Whether it may jump back too. */
8
+ readonly back: boolean;
9
+ }
10
+ export declare const jump: Operation<Jumper>;