Skip to content

Commit

Permalink
Add sudoku solver to misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
harryttd committed Jun 13, 2017
1 parent 8717688 commit 28acb91
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
1 change: 0 additions & 1 deletion misc/primes-sieveOfEratosthenes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

function eratosthenes(n) {
// Eratosthenes algorithm to find all primes under n
const array = new Array(n).fill(true), upperLimit = Math.sqrt(n), output = [];

// Remove multiples of primes starting from 2, 3, 5,...
Expand Down
72 changes: 72 additions & 0 deletions misc/solveSudoku.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

function solveSudoku(grid) {
const cell = [0, 0];

if (!findEmptyLocation(grid, cell)) return grid;

const [row, col] = cell;

for (let num = 1; num < 10; num++) {
if (checkSafeLocation(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) return grid;
else grid[row][col] = 0;
}
}

return null;
}

function findEmptyLocation(grid, cell) {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (!grid[row][col]) {
cell[0] = row;
cell[1] = col;
return true;
}
}
}
return false;
}

function checkSafeLocation(grid, row, col, num) {
return (
!usedInRowOrColumn(grid, row, col, num) &&
!usedInBox(grid, row - row % 3, col - col % 3, num)
);
}

function usedInRowOrColumn(grid, row, col, num) {
for (let i = 0; i < 9; i++) {
if (grid[row][i] === num || grid[i][col] === num) {
return true;
}
}
return false;
}

function usedInBox(grid, row, col, num) {
for (let i = 0; i < 3; i++) {
for (let x = 0; x < 3; x++) {
if(grid[i + row][x + col] === num) {
return true;
}
}
}
return false;
}

const grid = [
[3, 0, 6, 5, 0, 8, 4, 0, 0],
[5, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 7, 0, 0, 0, 0, 3, 1],
[0, 0, 3, 0, 1, 0, 0, 8, 0],
[9, 0, 0, 8, 6, 3, 0, 0, 5],
[0, 5, 0, 0, 9, 0, 6, 0, 0],
[1, 3, 0, 0, 0, 0, 2, 5, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 4],
[0, 0, 5, 2, 0, 6, 3, 0, 0]
];
console.log(solveSudoku(grid));
21 changes: 11 additions & 10 deletions misc/sudokuValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function sudokuValidator(board) {
if (!gridMap.has(currentGrid)) gridMap.set(currentGrid, new Set());
const grid = gridMap.get(currentGrid);
grid.add(cell);

}

if (column.size !== 9) return false;
}

Expand All @@ -30,13 +30,14 @@ function sudokuValidator(board) {
}

const sudoku = [
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]];
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]
];
console.log(sudokuValidator(sudoku));

0 comments on commit 28acb91

Please sign in to comment.