Skip to content

Commit

Permalink
stricter valid sudokus
Browse files Browse the repository at this point in the history
  • Loading branch information
icecream17 committed May 22, 2021
1 parent 002de4d commit bf6586f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,3 +41,5 @@ jobs:
build_dir: build
env:
GITHUB_TOKEN: ${{ secrets.PERSONALACCESSTOKEN }}

- uses: codecov/codecov-action@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Depedency analytics report
/target/npmlist.json
70 changes: 70 additions & 0 deletions HELP_README.md
Original file line number Diff line number Diff line change
@@ -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)
71 changes: 5 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 29 additions & 1 deletion src/Api/Strategies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +13,7 @@ describe('checkValidity', () => {
})

test('a blank sudoku is valid (subject to change)', () => {
console.clear()
expect(checkValidity(testSudoku).ok).toBe(true)
})

Expand All @@ -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', () => {
Expand Down
63 changes: 57 additions & 6 deletions src/Api/Strategies.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,19 +15,28 @@ type validityResult = {
export function checkValidity(sudoku: PureSudoku): validityResult {
const solvedInColumns = [] as Array<Set<SudokuDigits>>
const solvedInBoxes = [] as Array<Set<SudokuDigits>>
const candidatesInColumns = [] as Array<Set<SudokuDigits>>
const candidatesInBoxes = [] as Array<Set<SudokuDigits>>

for (let i = 0; i < 9; i++) {
solvedInColumns.push(new Set<SudokuDigits>())
solvedInBoxes.push(new Set<SudokuDigits>())
candidatesInColumns.push(new Set<SudokuDigits>())
candidatesInBoxes.push(new Set<SudokuDigits>())
}

// 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<SudokuDigits>()
const candidatesInRow = new Set<SudokuDigits>()

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) {
Expand All @@ -49,27 +58,69 @@ 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]}!`
}
}

// 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)}!`
}
}

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
}
Expand Down
1 change: 1 addition & 0 deletions src/Types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf6586f

Please sign in to comment.