@fgv/ts-sudoku-lib 2.0.2-alpha.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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/cspell.json +13 -0
  4. package/dist/ts-sudoku-lib.d.ts +763 -0
  5. package/dist/tsdoc-metadata.json +11 -0
  6. package/lib/index.d.ts +6 -0
  7. package/lib/index.d.ts.map +1 -0
  8. package/lib/index.js +59 -0
  9. package/lib/index.js.map +1 -0
  10. package/lib/packlets/collections/collections.d.ts +55 -0
  11. package/lib/packlets/collections/collections.d.ts.map +1 -0
  12. package/lib/packlets/collections/collections.js +137 -0
  13. package/lib/packlets/collections/collections.js.map +1 -0
  14. package/lib/packlets/collections/data/puzzles.json +61 -0
  15. package/lib/packlets/collections/index.d.ts +2 -0
  16. package/lib/packlets/collections/index.d.ts.map +1 -0
  17. package/lib/packlets/collections/index.js +41 -0
  18. package/lib/packlets/collections/index.js.map +1 -0
  19. package/lib/packlets/common/cage.d.ts +23 -0
  20. package/lib/packlets/common/cage.d.ts.map +1 -0
  21. package/lib/packlets/common/cage.js +87 -0
  22. package/lib/packlets/common/cage.js.map +1 -0
  23. package/lib/packlets/common/cell.d.ts +34 -0
  24. package/lib/packlets/common/cell.d.ts.map +1 -0
  25. package/lib/packlets/common/cell.js +92 -0
  26. package/lib/packlets/common/cell.js.map +1 -0
  27. package/lib/packlets/common/common.d.ts +77 -0
  28. package/lib/packlets/common/common.d.ts.map +1 -0
  29. package/lib/packlets/common/common.js +49 -0
  30. package/lib/packlets/common/common.js.map +1 -0
  31. package/lib/packlets/common/converters.d.ts +24 -0
  32. package/lib/packlets/common/converters.d.ts.map +1 -0
  33. package/lib/packlets/common/converters.js +73 -0
  34. package/lib/packlets/common/converters.js.map +1 -0
  35. package/lib/packlets/common/ids.d.ts +15 -0
  36. package/lib/packlets/common/ids.d.ts.map +1 -0
  37. package/lib/packlets/common/ids.js +107 -0
  38. package/lib/packlets/common/ids.js.map +1 -0
  39. package/lib/packlets/common/index.d.ts +12 -0
  40. package/lib/packlets/common/index.d.ts.map +1 -0
  41. package/lib/packlets/common/index.js +65 -0
  42. package/lib/packlets/common/index.js.map +1 -0
  43. package/lib/packlets/common/model.d.ts +15 -0
  44. package/lib/packlets/common/model.d.ts.map +1 -0
  45. package/lib/packlets/common/model.js +26 -0
  46. package/lib/packlets/common/model.js.map +1 -0
  47. package/lib/packlets/common/public.d.ts +65 -0
  48. package/lib/packlets/common/public.d.ts.map +1 -0
  49. package/lib/packlets/common/public.js +26 -0
  50. package/lib/packlets/common/public.js.map +1 -0
  51. package/lib/packlets/common/puzzle.d.ts +99 -0
  52. package/lib/packlets/common/puzzle.d.ts.map +1 -0
  53. package/lib/packlets/common/puzzle.js +389 -0
  54. package/lib/packlets/common/puzzle.js.map +1 -0
  55. package/lib/packlets/common/puzzleSession.d.ts +214 -0
  56. package/lib/packlets/common/puzzleSession.d.ts.map +1 -0
  57. package/lib/packlets/common/puzzleSession.js +361 -0
  58. package/lib/packlets/common/puzzleSession.js.map +1 -0
  59. package/lib/packlets/common/puzzleState.d.ts +52 -0
  60. package/lib/packlets/common/puzzleState.d.ts.map +1 -0
  61. package/lib/packlets/common/puzzleState.js +100 -0
  62. package/lib/packlets/common/puzzleState.js.map +1 -0
  63. package/lib/packlets/file/converters.d.ts +17 -0
  64. package/lib/packlets/file/converters.d.ts.map +1 -0
  65. package/lib/packlets/file/converters.js +49 -0
  66. package/lib/packlets/file/converters.js.map +1 -0
  67. package/lib/packlets/file/index.d.ts +4 -0
  68. package/lib/packlets/file/index.d.ts.map +1 -0
  69. package/lib/packlets/file/index.js +54 -0
  70. package/lib/packlets/file/index.js.map +1 -0
  71. package/lib/packlets/file/model.d.ts +9 -0
  72. package/lib/packlets/file/model.d.ts.map +1 -0
  73. package/lib/packlets/file/model.js +26 -0
  74. package/lib/packlets/file/model.js.map +1 -0
  75. package/lib/packlets/puzzles/anyPuzzle.d.ts +10 -0
  76. package/lib/packlets/puzzles/anyPuzzle.d.ts.map +1 -0
  77. package/lib/packlets/puzzles/anyPuzzle.js +51 -0
  78. package/lib/packlets/puzzles/anyPuzzle.js.map +1 -0
  79. package/lib/packlets/puzzles/index.d.ts +6 -0
  80. package/lib/packlets/puzzles/index.d.ts.map +1 -0
  81. package/lib/packlets/puzzles/index.js +35 -0
  82. package/lib/packlets/puzzles/index.js.map +1 -0
  83. package/lib/packlets/puzzles/killerSudokuPuzzle.d.ts +14 -0
  84. package/lib/packlets/puzzles/killerSudokuPuzzle.d.ts.map +1 -0
  85. package/lib/packlets/puzzles/killerSudokuPuzzle.js +136 -0
  86. package/lib/packlets/puzzles/killerSudokuPuzzle.js.map +1 -0
  87. package/lib/packlets/puzzles/sudokuPuzzle.d.ts +10 -0
  88. package/lib/packlets/puzzles/sudokuPuzzle.d.ts.map +1 -0
  89. package/lib/packlets/puzzles/sudokuPuzzle.js +47 -0
  90. package/lib/packlets/puzzles/sudokuPuzzle.js.map +1 -0
  91. package/lib/packlets/puzzles/sudokuXPuzzle.d.ts +11 -0
  92. package/lib/packlets/puzzles/sudokuXPuzzle.d.ts.map +1 -0
  93. package/lib/packlets/puzzles/sudokuXPuzzle.js +65 -0
  94. package/lib/packlets/puzzles/sudokuXPuzzle.js.map +1 -0
  95. package/package.json +70 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/packlets/common/model.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG","sourcesContent":["/*\n * MIT License\n *\n * Copyright (c) 2023 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { PuzzleType } from './common';\n\n/**\n * Description of a single puzzle.\n * @public\n */\nexport interface IPuzzleDescription {\n id?: string;\n description: string;\n type: PuzzleType;\n level: number;\n rows: number;\n cols: number;\n cells: string;\n}\n"]}
@@ -0,0 +1,65 @@
1
+ import { CageId, CageType, CellId } from './common';
2
+ /**
3
+ * Describes the structure of a single cage in a {@link PuzzleSession | puzzle}.
4
+ * Does not describe state.
5
+ * @public
6
+ */
7
+ export interface ICage {
8
+ /**
9
+ * Unique identifier for the cage.
10
+ */
11
+ readonly id: CageId;
12
+ /**
13
+ * The {@link CageType | type} of the cage.
14
+ */
15
+ readonly cageType: CageType;
16
+ /**
17
+ * The expected sum of all cells in the cage.
18
+ */
19
+ readonly total: number;
20
+ /**
21
+ * The number of cells in the cage.
22
+ */
23
+ readonly numCells: number;
24
+ /**
25
+ * The identity of each cell in the cage.
26
+ */
27
+ readonly cellIds: CellId[];
28
+ /**
29
+ * Determines if a supplied cell is present in the cage.
30
+ * @param id - the identifier to be searched.
31
+ */
32
+ containsCell(id: CellId): boolean;
33
+ }
34
+ /**
35
+ * Describes the structure of a single cell in a {@link PuzzleSession | puzzle}.
36
+ * Does not describe state.
37
+ * @public
38
+ */
39
+ export interface ICell {
40
+ /**
41
+ * Unique identifier for the cell.
42
+ */
43
+ readonly id: CellId;
44
+ /**
45
+ * Row number of the cell.
46
+ */
47
+ readonly row: number;
48
+ /**
49
+ * Column number of the cell.
50
+ */
51
+ readonly col: number;
52
+ /**
53
+ * All of the {@Link ICage | cages} to which this cell belongs.
54
+ */
55
+ readonly cages: readonly ICage[];
56
+ /**
57
+ * Indicates whether this cell is a given value (immutable).
58
+ */
59
+ readonly immutable: boolean;
60
+ /**
61
+ * Given value of this cell, or `undefined` if the cell is not immutable.
62
+ */
63
+ readonly immutableValue?: number;
64
+ }
65
+ //# sourceMappingURL=public.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../../src/packlets/common/public.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEpD;;;;GAIG;AACH,MAAM,WAAW,KAAK;IACpB;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAE5B;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAE3B;;;OAGG;IACH,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;CACnC;AAED;;;;GAIG;AACH,MAAM,WAAW,KAAK;IACpB;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,CAAC;IAEjC;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;CAClC"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /*
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2023 Erik Fortune
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ //# sourceMappingURL=public.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public.js","sourceRoot":"","sources":["../../../src/packlets/common/public.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG","sourcesContent":["/*\n * MIT License\n *\n * Copyright (c) 2023 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { CageId, CageType, CellId } from './common';\n\n/**\n * Describes the structure of a single cage in a {@link PuzzleSession | puzzle}.\n * Does not describe state.\n * @public\n */\nexport interface ICage {\n /**\n * Unique identifier for the cage.\n */\n readonly id: CageId;\n\n /**\n * The {@link CageType | type} of the cage.\n */\n readonly cageType: CageType;\n\n /**\n * The expected sum of all cells in the cage.\n */\n readonly total: number;\n\n /**\n * The number of cells in the cage.\n */\n readonly numCells: number;\n\n /**\n * The identity of each cell in the cage.\n */\n readonly cellIds: CellId[];\n\n /**\n * Determines if a supplied cell is present in the cage.\n * @param id - the identifier to be searched.\n */\n containsCell(id: CellId): boolean;\n}\n\n/**\n * Describes the structure of a single cell in a {@link PuzzleSession | puzzle}.\n * Does not describe state.\n * @public\n */\nexport interface ICell {\n /**\n * Unique identifier for the cell.\n */\n readonly id: CellId;\n\n /**\n * Row number of the cell.\n */\n readonly row: number;\n\n /**\n * Column number of the cell.\n */\n readonly col: number;\n\n /**\n * All of the {@Link ICage | cages} to which this cell belongs.\n */\n readonly cages: readonly ICage[];\n\n /**\n * Indicates whether this cell is a given value (immutable).\n */\n readonly immutable: boolean;\n\n /**\n * Given value of this cell, or `undefined` if the cell is not immutable.\n */\n readonly immutableValue?: number;\n}\n"]}
@@ -0,0 +1,99 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { CageId, CellId, ICellContents, ICellState, IRowColumn, NavigationDirection, NavigationWrap } from './common';
3
+ import { Cage } from './cage';
4
+ import { Cell } from './cell';
5
+ import { IPuzzleDescription } from './model';
6
+ import { ICell } from './public';
7
+ import { PuzzleState } from './puzzleState';
8
+ /**
9
+ * @internal
10
+ */
11
+ export interface ICellUpdate {
12
+ from: ICellState;
13
+ to: ICellState;
14
+ }
15
+ /**
16
+ * @internal
17
+ */
18
+ export interface IPuzzleUpdate {
19
+ from: PuzzleState;
20
+ to: PuzzleState;
21
+ cells: ICellUpdate[];
22
+ }
23
+ /**
24
+ * @internal
25
+ */
26
+ export declare class Puzzle {
27
+ readonly id?: string;
28
+ readonly description: string;
29
+ readonly initialState: PuzzleState;
30
+ /**
31
+ * @internal
32
+ */
33
+ protected readonly _rows: Map<CageId, Cage>;
34
+ /**
35
+ * @internal
36
+ */
37
+ protected readonly _columns: Map<CageId, Cage>;
38
+ /**
39
+ * @internal
40
+ */
41
+ protected readonly _sections: Map<CageId, Cage>;
42
+ /**
43
+ * @internal
44
+ */
45
+ protected readonly _cages: Map<CageId, Cage>;
46
+ /**
47
+ * @internal
48
+ */
49
+ protected readonly _cells: Map<CellId, Cell>;
50
+ /**
51
+ * Constructs a new puzzle state.
52
+ * @param puzzle - {@Link Data.Model.PuzzleDescription | Puzzle description} from which this puzzle state
53
+ * is to be initialized.
54
+ */
55
+ protected constructor(puzzle: IPuzzleDescription, extraCages?: [CageId, Cage][]);
56
+ get numRows(): number;
57
+ get numColumns(): number;
58
+ get rows(): Cage[];
59
+ get cols(): Cage[];
60
+ get sections(): Cage[];
61
+ get cages(): Cage[];
62
+ get cells(): Cell[];
63
+ /**
64
+ * @internal
65
+ */
66
+ protected static _createRowCages(numRows: number, numCols: number): Result<[CageId, Cage][]>;
67
+ /**
68
+ * @internal
69
+ */
70
+ protected static _createColumnCages(numRows: number, numCols: number): Result<[CageId, Cage][]>;
71
+ /**
72
+ * @internal
73
+ */
74
+ private static _createSectionCages;
75
+ checkIsSolved(state: PuzzleState): boolean;
76
+ checkIsValid(state: PuzzleState): boolean;
77
+ getEmptyCells(state: PuzzleState): Cell[];
78
+ getInvalidCells(state: PuzzleState): Cell[];
79
+ getCellContents(spec: string | IRowColumn, state: PuzzleState): Result<{
80
+ cell: Cell;
81
+ contents: ICellContents;
82
+ }>;
83
+ getCell(spec: string | IRowColumn | ICell): Result<Cell>;
84
+ getCellNeighbor(spec: string | IRowColumn | ICell, direction: NavigationDirection, wrap: NavigationWrap): Result<ICell>;
85
+ updateContents(wantUpdates: ICellState[] | ICellState, state: PuzzleState): Result<IPuzzleUpdate>;
86
+ updateValues(wantUpdates: ICellState[] | ICellState, state: PuzzleState): Result<IPuzzleUpdate>;
87
+ updateNotes(wantUpdates: ICellState[] | ICellState, state: PuzzleState): Result<IPuzzleUpdate>;
88
+ updateCellValue(want: string | IRowColumn, value: number | undefined, state: PuzzleState): Result<IPuzzleUpdate>;
89
+ updateCellNotes(want: string | IRowColumn, notes: number[], state: PuzzleState): Result<IPuzzleUpdate>;
90
+ getRow(row: CageId | number): Result<Cage>;
91
+ getColumn(col: CageId | number): Result<Cage>;
92
+ getSection(spec: CageId | IRowColumn): Result<Cage>;
93
+ getCage(id: CageId): Result<Cage>;
94
+ toStrings(state: PuzzleState): string[];
95
+ toString(state: PuzzleState): string;
96
+ private _moveColumn;
97
+ private _moveRow;
98
+ }
99
+ //# sourceMappingURL=puzzle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"puzzle.d.ts","sourceRoot":"","sources":["../../../src/packlets/common/puzzle.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,MAAM,EAA6B,MAAM,eAAe,CAAC;AAClE,OAAO,EACL,MAAM,EACN,MAAM,EACN,aAAa,EACb,UAAU,EACV,UAAU,EACV,mBAAmB,EACnB,c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erik Fortune
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Puzzle = void 0;
27
+ const ts_utils_1 = require("@fgv/ts-utils");
28
+ const cage_1 = require("./cage");
29
+ const cell_1 = require("./cell");
30
+ const ids_1 = require("./ids");
31
+ const puzzleState_1 = require("./puzzleState");
32
+ const basicCageTotal = 45;
33
+ /**
34
+ * @internal
35
+ */
36
+ class Puzzle {
37
+ /**
38
+ * Constructs a new puzzle state.
39
+ * @param puzzle - {@Link Data.Model.PuzzleDescription | Puzzle description} from which this puzzle state
40
+ * is to be initialized.
41
+ */
42
+ constructor(puzzle, extraCages) {
43
+ /* c8 ignore next - ?? is defense in depth */
44
+ extraCages = extraCages !== null && extraCages !== void 0 ? extraCages : [];
45
+ if (puzzle.rows !== 9) {
46
+ throw new Error(`Puzzle '${puzzle.description}' unsupported row count ${puzzle.rows}`);
47
+ }
48
+ if (puzzle.cols !== 9) {
49
+ throw new Error(`Puzzle '${puzzle.description}' unsupported column count ${puzzle.cols}`);
50
+ }
51
+ if (puzzle.cells.length !== puzzle.rows * puzzle.cols) {
52
+ throw new Error(`Puzzle '${puzzle.description}" expected ${puzzle.rows * puzzle.cols} cells, found ${puzzle.cells.length}`);
53
+ }
54
+ this.id = puzzle.id;
55
+ this.description = puzzle.description;
56
+ const rows = Puzzle._createRowCages(puzzle.rows, puzzle.cols).orThrow();
57
+ const columns = Puzzle._createColumnCages(puzzle.rows, puzzle.cols).orThrow();
58
+ const sections = Puzzle._createSectionCages(puzzle.rows, puzzle.cols).orThrow();
59
+ const cages = [...rows, ...columns, ...sections, ...extraCages];
60
+ this._rows = new Map(rows);
61
+ this._columns = new Map(columns);
62
+ this._sections = new Map(sections);
63
+ this._cages = new Map(cages);
64
+ this._cells = new Map();
65
+ const cellInit = [...puzzle.cells];
66
+ for (let row = 0; row < this._rows.size; row++) {
67
+ const rowCage = this.getRow(row).orThrow();
68
+ for (let col = 0; col < this._columns.size; col++) {
69
+ const id = ids_1.Ids.cellId({ row, col }).orThrow();
70
+ const colCage = this.getColumn(col).orThrow();
71
+ const sectionCage = this.getSection({ row, col }).orThrow();
72
+ const otherCages = extraCages.filter(([__key, cage]) => cage.containsCell(id));
73
+ const init = cellInit.shift();
74
+ /* c8 ignore next - defense in depth/make type check happy. undefined init should never happen */
75
+ const immutableValue = init === '.' ? undefined : Number.parseInt(init !== null && init !== void 0 ? init : '0');
76
+ if (immutableValue !== undefined &&
77
+ (Number.isNaN(immutableValue) || immutableValue < 1 || immutableValue > 9)) {
78
+ throw new Error(`Puzzle ${puzzle.description} illegal value "${init}" for cell ${id}`);
79
+ }
80
+ const cages = [rowCage, colCage, sectionCage, ...otherCages.map(([__key, state]) => state)];
81
+ const cell = new cell_1.Cell({ id, immutableValue, row, col }, cages);
82
+ this._cells.set(id, cell);
83
+ }
84
+ }
85
+ this.initialState = puzzleState_1.PuzzleState.create(Array.from(this._cells.entries()).map(([id, state]) => {
86
+ return { id, value: state.immutableValue, notes: [] };
87
+ })).orThrow();
88
+ }
89
+ get numRows() {
90
+ return this._rows.size;
91
+ }
92
+ get numColumns() {
93
+ return this._columns.size;
94
+ }
95
+ get rows() {
96
+ return Array.from(this._rows.values());
97
+ }
98
+ get cols() {
99
+ return Array.from(this._columns.values());
100
+ }
101
+ get sections() {
102
+ return Array.from(this._sections.values());
103
+ }
104
+ get cages() {
105
+ return Array.from(this._cages.values());
106
+ }
107
+ get cells() {
108
+ return Array.from(this._cells.values());
109
+ }
110
+ /**
111
+ * @internal
112
+ */
113
+ static _createRowCages(numRows, numCols) {
114
+ const cages = [];
115
+ for (let r = 0; r < numRows; r++) {
116
+ const id = ids_1.Ids.rowCageId(r);
117
+ const cellIds = ids_1.Ids.cellIds(r, 1, 0, numCols);
118
+ /* c8 ignore next 3 - defense in depth should never happen */
119
+ if (cellIds.isFailure()) {
120
+ return (0, ts_utils_1.fail)(cellIds.message);
121
+ }
122
+ const result = cage_1.Cage.create(id, 'row', basicCageTotal, cellIds.value).onSuccess((cage) => {
123
+ cages.push([id, cage]);
124
+ return (0, ts_utils_1.succeed)(cage);
125
+ });
126
+ /* c8 ignore next 3 - defense in depth should never happen */
127
+ if (result.isFailure()) {
128
+ return (0, ts_utils_1.fail)(result.message);
129
+ }
130
+ }
131
+ return (0, ts_utils_1.succeed)(cages);
132
+ }
133
+ /**
134
+ * @internal
135
+ */
136
+ static _createColumnCages(numRows, numCols) {
137
+ const cages = [];
138
+ for (let c = 0; c < numCols; c++) {
139
+ const id = ids_1.Ids.columnCageId(c);
140
+ const cellIds = ids_1.Ids.cellIds(0, numRows, c, 1);
141
+ /* c8 ignore next 3 - defense in depth should never happen */
142
+ if (cellIds.isFailure()) {
143
+ return (0, ts_utils_1.fail)(cellIds.message);
144
+ }
145
+ const result = cage_1.Cage.create(id, 'column', basicCageTotal, cellIds.value).onSuccess((cage) => {
146
+ cages.push([id, cage]);
147
+ return (0, ts_utils_1.succeed)(cage);
148
+ });
149
+ /* c8 ignore next 3 - defense in depth should never happen */
150
+ if (result.isFailure()) {
151
+ return (0, ts_utils_1.fail)(result.message);
152
+ }
153
+ }
154
+ return (0, ts_utils_1.succeed)(cages);
155
+ }
156
+ /**
157
+ * @internal
158
+ */
159
+ static _createSectionCages(numRows, numCols) {
160
+ const cages = [];
161
+ for (let r = 0; r < numRows; r += 3) {
162
+ for (let c = 0; c < numCols; c += 3) {
163
+ const id = ids_1.Ids.sectionCageId(r, c);
164
+ const cellIds = ids_1.Ids.cellIds(r, 3, c, 3);
165
+ /* c8 ignore next 3 - defense in depth should never happen */
166
+ if (cellIds.isFailure()) {
167
+ return (0, ts_utils_1.fail)(cellIds.message);
168
+ }
169
+ const result = cage_1.Cage.create(id, 'section', basicCageTotal, cellIds.value).onSuccess((cage) => {
170
+ cages.push([id, cage]);
171
+ return (0, ts_utils_1.succeed)(cage);
172
+ });
173
+ /* c8 ignore next 3 - defense in depth should never happen */
174
+ if (result.isFailure()) {
175
+ return (0, ts_utils_1.fail)(result.message);
176
+ }
177
+ }
178
+ }
179
+ return (0, ts_utils_1.succeed)(cages);
180
+ }
181
+ checkIsSolved(state) {
182
+ for (const id of this._cells.keys()) {
183
+ if (!state.hasValue(id)) {
184
+ return false;
185
+ }
186
+ }
187
+ for (const cell of this._cells.values()) {
188
+ if (!cell.isValid(state)) {
189
+ return false;
190
+ }
191
+ }
192
+ return true;
193
+ }
194
+ checkIsValid(state) {
195
+ for (const cell of this._cells.values()) {
196
+ if (!cell.isValid(state)) {
197
+ return false;
198
+ }
199
+ }
200
+ return true;
201
+ }
202
+ getEmptyCells(state) {
203
+ return Array.from(this._cells.values()).filter((c) => !state.hasValue(c.id));
204
+ }
205
+ getInvalidCells(state) {
206
+ return Array.from(this._cells.values()).filter((c) => !c.isValid(state));
207
+ }
208
+ getCellContents(spec, state) {
209
+ return this.getCell(spec).onSuccess((cell) => {
210
+ if (cell.immutable) {
211
+ const contents = { value: cell.immutableValue, notes: [] };
212
+ return (0, ts_utils_1.succeed)({ cell, contents });
213
+ }
214
+ return state.getCellContents(cell.id).onSuccess((contents) => {
215
+ return (0, ts_utils_1.succeed)({ cell, contents });
216
+ });
217
+ });
218
+ }
219
+ getCell(spec) {
220
+ const want = ids_1.Ids.cellId(spec);
221
+ if (want.isFailure()) {
222
+ return (0, ts_utils_1.fail)(want.message);
223
+ }
224
+ const cell = this._cells.get(want.value);
225
+ return cell ? (0, ts_utils_1.succeed)(cell) : (0, ts_utils_1.fail)(`Cell ${want.value} not found`);
226
+ }
227
+ getCellNeighbor(spec, direction, wrap) {
228
+ return this.getCell(spec).onSuccess((cell) => {
229
+ const move = direction === 'left' || direction === 'right'
230
+ ? this._moveColumn(cell, direction, wrap)
231
+ : this._moveRow(cell, direction, wrap);
232
+ return move.onSuccess((next) => this.getCell(next));
233
+ });
234
+ }
235
+ updateContents(wantUpdates, state) {
236
+ wantUpdates = Array.isArray(wantUpdates) ? wantUpdates : [wantUpdates];
237
+ return (0, ts_utils_1.mapResults)(wantUpdates.map((u) => this.getCellContents(u.id, state).onSuccess((existing) => existing.cell.update(u.value, u.notes).onSuccess((to) => {
238
+ const from = Object.assign({ id: u.id }, existing.contents);
239
+ return (0, ts_utils_1.succeed)({ from, to });
240
+ })))).onSuccess((cellUpdates) => {
241
+ return state.update(cellUpdates.map(({ to }) => to)).onSuccess((updatedState) => {
242
+ return (0, ts_utils_1.succeed)({
243
+ from: state,
244
+ to: updatedState,
245
+ cells: cellUpdates
246
+ });
247
+ });
248
+ });
249
+ }
250
+ updateValues(wantUpdates, state) {
251
+ wantUpdates = Array.isArray(wantUpdates) ? wantUpdates : [wantUpdates];
252
+ return (0, ts_utils_1.mapResults)(wantUpdates.map((u) => this.getCellContents(u.id, state).onSuccess((existing) => existing.cell.update(u.value, existing.contents.notes).onSuccess((to) => {
253
+ const from = Object.assign({ id: u.id }, existing.contents);
254
+ return (0, ts_utils_1.succeed)({ from, to });
255
+ })))).onSuccess((cellUpdates) => {
256
+ return state.update(cellUpdates.map(({ to }) => to)).onSuccess((updatedState) => {
257
+ return (0, ts_utils_1.succeed)({
258
+ from: state,
259
+ to: updatedState,
260
+ cells: cellUpdates
261
+ });
262
+ });
263
+ });
264
+ }
265
+ updateNotes(wantUpdates, state) {
266
+ wantUpdates = Array.isArray(wantUpdates) ? wantUpdates : [wantUpdates];
267
+ return (0, ts_utils_1.mapResults)(wantUpdates.map((u) => this.getCellContents(u.id, state).onSuccess((existing) => existing.cell.update(existing.contents.value, u.notes).onSuccess((to) => {
268
+ const from = Object.assign({ id: u.id }, existing.contents);
269
+ return (0, ts_utils_1.succeed)({ from, to });
270
+ })))).onSuccess((cellUpdates) => {
271
+ return state.update(cellUpdates.map(({ to }) => to)).onSuccess((updatedState) => {
272
+ return (0, ts_utils_1.succeed)({
273
+ from: state,
274
+ to: updatedState,
275
+ cells: cellUpdates
276
+ });
277
+ });
278
+ });
279
+ }
280
+ updateCellValue(want, value, state) {
281
+ const idResult = typeof want === 'string' ? ids_1.Ids.cellId(want) : ids_1.Ids.cellId(want);
282
+ const notes = [];
283
+ return idResult.onSuccess((id) => {
284
+ const update = { id, value, notes };
285
+ return this.updateValues([update], state);
286
+ });
287
+ }
288
+ updateCellNotes(want, notes, state) {
289
+ const idResult = typeof want === 'string' ? ids_1.Ids.cellId(want) : ids_1.Ids.cellId(want);
290
+ const value = undefined;
291
+ return idResult.onSuccess((id) => {
292
+ const update = { id, value, notes };
293
+ return this.updateNotes([update], state);
294
+ });
295
+ }
296
+ getRow(row) {
297
+ const id = typeof row === 'number' ? ids_1.Ids.rowCageId(row) : row;
298
+ const cage = this._rows.get(id);
299
+ return cage ? (0, ts_utils_1.succeed)(cage) : (0, ts_utils_1.fail)(`Row ${id} not found`);
300
+ }
301
+ getColumn(col) {
302
+ const id = typeof col === 'number' ? ids_1.Ids.columnCageId(col) : col;
303
+ const cage = this._columns.get(id);
304
+ return cage ? (0, ts_utils_1.succeed)(cage) : (0, ts_utils_1.fail)(`Column ${id} not found`);
305
+ }
306
+ getSection(spec) {
307
+ const id = typeof spec === 'object' ? ids_1.Ids.sectionCageId(spec.row, spec.col) : spec;
308
+ const cage = this._sections.get(id);
309
+ return cage ? (0, ts_utils_1.succeed)(cage) : (0, ts_utils_1.fail)(`Section ${id} not found`);
310
+ }
311
+ getCage(id) {
312
+ const cage = this._cages.get(id);
313
+ return cage ? (0, ts_utils_1.succeed)(cage) : (0, ts_utils_1.fail)(`Cage ${id} not found`);
314
+ }
315
+ toStrings(state) {
316
+ return Array.from(this._rows.values()).map((row) => row.toString(state));
317
+ }
318
+ toString(state) {
319
+ return this.toStrings(state).join('\n');
320
+ }
321
+ _moveColumn(current, direction, wrap) {
322
+ const row = current.row;
323
+ let col = current.col;
324
+ if (direction === 'left') {
325
+ if (col > 0) {
326
+ col = col - 1;
327
+ }
328
+ else if (wrap !== 'none') {
329
+ col = this.numColumns - 1;
330
+ if (wrap === 'wrap-next') {
331
+ return this._moveRow({ row, col }, 'up', 'wrap-around');
332
+ }
333
+ }
334
+ else {
335
+ return (0, ts_utils_1.fail)(`cannot move left from column ${current.col}`);
336
+ }
337
+ }
338
+ else if (direction === 'right') {
339
+ if (col < this.numColumns - 1) {
340
+ col = col + 1;
341
+ }
342
+ else if (wrap !== 'none') {
343
+ col = 0;
344
+ if (wrap === 'wrap-next') {
345
+ return this._moveRow({ row, col }, 'down', 'wrap-around');
346
+ }
347
+ }
348
+ else {
349
+ return (0, ts_utils_1.fail)(`cannot move right from column ${current.col}`);
350
+ }
351
+ }
352
+ return (0, ts_utils_1.succeed)({ row, col });
353
+ }
354
+ _moveRow(current, direction, wrap) {
355
+ let row = current.row;
356
+ const col = current.col;
357
+ if (direction === 'up') {
358
+ if (row > 0) {
359
+ row = row - 1;
360
+ }
361
+ else if (wrap !== 'none') {
362
+ row = this.numRows - 1;
363
+ if (wrap === 'wrap-next') {
364
+ return this._moveColumn({ row, col }, 'left', 'wrap-around');
365
+ }
366
+ }
367
+ else {
368
+ return (0, ts_utils_1.fail)(`cannot move up from row ${current.row}`);
369
+ }
370
+ }
371
+ else if (direction === 'down') {
372
+ if (row < this.numRows - 1) {
373
+ row = row + 1;
374
+ }
375
+ else if (wrap !== 'none') {
376
+ row = 0;
377
+ if (wrap === 'wrap-next') {
378
+ return this._moveColumn({ row, col }, 'right', 'wrap-around');
379
+ }
380
+ }
381
+ else {
382
+ return (0, ts_utils_1.fail)(`cannot move down from row ${current.row}`);
383
+ }
384
+ }
385
+ return (0, ts_utils_1.succeed)({ row, col });
386
+ }
387
+ }
388
+ exports.Puzzle = Puzzle;
389
+ //# sourceMappingURL=puzzle.js.map