diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index e5504d98..aedbd629 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -32,7 +32,7 @@ jobs: check-latest: false - run: npm ci - run: npm run build --if-present - - run: npm test + - run: npm run test-with-coverage if: "!contains(github.event.head_commit.message, 'skip test')" - name: Deploy to github pages @@ -41,3 +41,5 @@ jobs: build_dir: build env: GITHUB_TOKEN: ${{ secrets.PERSONALACCESSTOKEN }} + + - uses: codecov/codecov-action@v1 diff --git a/.gitignore b/.gitignore index 4d29575d..102b447d 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +# Depedency analytics report +/target/npmlist.json diff --git a/HELP_README.md b/HELP_README.md new file mode 100644 index 00000000..58beeacc --- /dev/null +++ b/HELP_README.md @@ -0,0 +1,70 @@ +# Getting Started with Create React App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in your browser. + +The page will reload when you make changes.\ +You may also see any lint errors in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can't go back!** + +If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. + +You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). + +### Code Splitting + +This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) + +### Analyzing the Bundle Size + +This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) + +### Making a Progressive Web App + +This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) + +### Advanced Configuration + +This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) + +### Deployment + +This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) + +### `npm run build` fails to minify + +This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) diff --git a/README.md b/README.md index 58beeacc..afa02e8d 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,9 @@ -# Getting Started with Create React App +# solver -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +A simple sudoku solver I made. -## Available Scripts +## Silly dependency note -In the project directory, you can run: +This projects uses the latest versions of its dependencies, but those dependencies themselves aren't updated. -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in your browser. - -The page will reload when you make changes.\ -You may also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can't go back!** - -If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. - -You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +In particular, the dependency `react-scripts` depends on outdated versions of `postcss` in 80 different ways. The only reason I know about this is because of `npm audit` which shows security vulnerabilities. diff --git a/src/Api/Strategies.test.ts b/src/Api/Strategies.test.ts index d9d5ba85..71c7317f 100644 --- a/src/Api/Strategies.test.ts +++ b/src/Api/Strategies.test.ts @@ -3,7 +3,7 @@ import Strategies, { checkValidity } from "./Strategies" import testBoards from "./boards" import Solver from "./Solver"; import Sudoku from "./Sudoku"; -import { MAX_CELL_INDEX } from "../Types"; +import { IndexToNine, MAX_CELL_INDEX } from "../Types"; describe('checkValidity', () => { let testSudoku: PureSudoku; @@ -13,6 +13,7 @@ describe('checkValidity', () => { }) test('a blank sudoku is valid (subject to change)', () => { + console.clear() expect(checkValidity(testSudoku).ok).toBe(true) }) @@ -38,6 +39,33 @@ describe('checkValidity', () => { testSudoku.set(1, 2).to(4) expect(checkValidity(testSudoku).ok).toBe(false) }) + + test('it must be possible for each digit to be in a row', () => { + for (let i: IndexToNine = 0; i < 9; i = i + 1 as IndexToNine) { + testSudoku.set(0, i).to(1, 2, 3, 4, 5, 6, 7, 8) + } + expect(checkValidity(testSudoku).ok).toBe(false) + }) + + test('it must be possible for each digit to be in a column', () => { + for (let i: IndexToNine = 0; i < 9; i = i + 1 as IndexToNine) { + testSudoku.set(i, 0).to(1, 2, 3, 4, 5, 6, 7, 8) + } + expect(checkValidity(testSudoku).ok).toBe(false) + }) + + test('it must be possible for each digit to be in a box', () => { + testSudoku.set(0, 0).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(0, 1).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(0, 2).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(1, 0).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(1, 1).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(1, 2).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(2, 0).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(2, 1).to(1, 2, 3, 4, 5, 6, 7, 8) + testSudoku.set(2, 2).to(1, 2, 3, 4, 5, 6, 7, 8) + expect(checkValidity(testSudoku).ok).toBe(false) + }) }) describe('strategies', () => { diff --git a/src/Api/Strategies.ts b/src/Api/Strategies.ts index 3af70ab7..2be7a6d4 100644 --- a/src/Api/Strategies.ts +++ b/src/Api/Strategies.ts @@ -1,4 +1,4 @@ -import { COLUMN_NAMES, IndexToNine, MAX_CELL_INDEX, ROW_NAMES, SudokuDigits } from "../Types"; +import { ALL_CANDIDATES, COLUMN_NAMES, IndexToNine, MAX_CELL_INDEX, ROW_NAMES, SudokuDigits } from "../Types"; import PureSudoku from "./PureSudoku"; import Solver from "./Solver"; import Sudoku from "./Sudoku"; @@ -15,19 +15,28 @@ type validityResult = { export function checkValidity(sudoku: PureSudoku): validityResult { const solvedInColumns = [] as Array> const solvedInBoxes = [] as Array> + const candidatesInColumns = [] as Array> + const candidatesInBoxes = [] as Array> for (let i = 0; i < 9; i++) { solvedInColumns.push(new Set()) solvedInBoxes.push(new Set()) + candidatesInColumns.push(new Set()) + candidatesInBoxes.push(new Set()) } // Every time an index is changed, typing is there so that `typeof i !== number` // Expanding `i++` into `i = i+1` so that the type assertion works - for (let i: IndexToNine = 0; i < 9; i = i+1 as IndexToNine) { + for (let i: IndexToNine = 0; i < 9; i = i + 1 as IndexToNine) { const solvedInRow = new Set() + const candidatesInRow = new Set() for (let j: IndexToNine = 0; j < 9; j = j+1 as IndexToNine) { const candidates = sudoku.data[i][j] + const current = { + column: j, + box: boxAt(i, j) + } as const // No possibilities if (candidates.length === 0) { @@ -49,7 +58,7 @@ export function checkValidity(sudoku: PureSudoku): validityResult { } // Same in column - if (solvedInColumns[j].has(solvedCandidate)) { + if (solvedInColumns[current.column].has(solvedCandidate)) { return { ok: false, message: `Two (or more) ${solvedCandidate}s in column ${COLUMN_NAMES[j]}!` @@ -57,7 +66,7 @@ export function checkValidity(sudoku: PureSudoku): validityResult { } // Same in box - if (solvedInBoxes[boxAt(i, j)].has(solvedCandidate)) { + if (solvedInBoxes[current.box].has(solvedCandidate)) { return { ok: false, message: `Two (or more) ${solvedCandidate}s in box ${boxNameAt(i, j)}!` @@ -65,11 +74,53 @@ export function checkValidity(sudoku: PureSudoku): validityResult { } solvedInRow.add(solvedCandidate) - solvedInColumns[j].add(solvedCandidate) - solvedInBoxes[boxAt(i, j)].add(solvedCandidate) + solvedInColumns[current.column].add(solvedCandidate) + solvedInBoxes[current.box].add(solvedCandidate) + } + + for (const candidate of candidates) { + candidatesInRow.add(candidate) + candidatesInColumns[current.column].add(candidate) + candidatesInBoxes[current.box].add(candidate) + } + } + + if (candidatesInRow.size !== 9) { + const missingCandidates = ALL_CANDIDATES.filter( + candidate => !candidatesInRow.has(candidate) + ) + + return { + ok: false, + message: `Row ${i} has 0 possibilities for ${missingCandidates}!!!` } } } + + for (let i = 0; i < 9; i++) { + if (candidatesInColumns[i].size !== 9) { + const missingCandidates = ALL_CANDIDATES.filter( + candidate => !candidatesInColumns[i].has(candidate) + ) + + return { + ok: false, + message: `Column ${i} has 0 possibilities for ${missingCandidates}!!!` + } + } + + if (candidatesInBoxes[i].size !== 9) { + const missingCandidates = ALL_CANDIDATES.filter( + candidate => !candidatesInBoxes[i].has(candidate) + ) + + return { + ok: false, + message: `Box ${i} has 0 possibilities for ${missingCandidates}!!!` + } + } + } + return { ok: true } diff --git a/src/Types.tsx b/src/Types.tsx index 54a4ac84..adb13e91 100644 --- a/src/Types.tsx +++ b/src/Types.tsx @@ -11,6 +11,7 @@ export type IndexTo81 = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 1 export const MAX_CELL_INDEX = 80 export const ROW_NAMES = ["A", "B", "C", "D", "E", "F", "G", "H", "J"] as const export const COLUMN_NAMES = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] as const +export const ALL_CANDIDATES = [1, 2, 3, 4, 5, 6, 7, 8, 9] as const //////////// // Utility types