Last active
January 16, 2016 09:27
-
-
Save bluepnume/e4f5e4d03276e4818804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var VALID_BOARD = [ | |
| [ 8,9,5,7,4,2,1,3,6 ], | |
| [ 2,7,1,9,6,3,4,8,5 ], | |
| [ 4,6,3,5,8,1,7,9,2 ], | |
| [ 9,3,4,6,1,7,2,5,8 ], | |
| [ 5,1,7,2,3,8,9,6,4 ], | |
| [ 6,8,2,4,5,9,3,7,1 ], | |
| [ 1,5,9,8,7,4,6,2,3 ], | |
| [ 7,4,6,3,2,5,8,1,9 ], | |
| [ 3,2,8,1,9,6,5,4,7 ] | |
| ]; | |
| var INVALID_BOARD_1 = [ | |
| [ 8,9,5,7,6,2,1,3,6 ], | |
| [ 2,7,1,9,6,3,4,8,5 ], | |
| [ 4,6,3,5,8,1,7,9,2 ], | |
| [ 9,3,4,6,1,7,2,5,8 ], | |
| [ 5,1,7,2,3,8,9,6,4 ], | |
| [ 6,8,2,4,5,9,3,7,1 ], | |
| [ 1,5,9,8,7,4,6,2,3 ], | |
| [ 7,4,6,3,2,5,8,1,9 ], | |
| [ 3,2,8,1,9,6,5,4,7 ] | |
| ]; | |
| var INVALID_BOARD_2 = [ | |
| [ 8,9,5,7,6,2,1,3,6 ], | |
| [ 2,7,1,9,6,3,4,8,5 ], | |
| [ 4,6,3,5,8,1,7,9,2 ], | |
| [ 9,3,4,6,1,7,2,5,8 ], | |
| [ 5,1,7,2,3,8,9,6,4 ], | |
| [ 6,8,2,4,5,9,3,7,1 ], | |
| [ 1,5,9,8,7,4,7,2,3 ], | |
| [ 7,4,6,3,2,5,8,1,9 ], | |
| [ 3,2,8,1,9,6,5,4,7 ] | |
| ]; | |
| function assert(condition, error) { | |
| if (!condition) { | |
| throw new Error(error); | |
| } | |
| } | |
| var BOARD_SIZE = 9; | |
| var SECTION_SIZE = 3; | |
| assert(validateSudoku(VALID_BOARD) === true, 'Expected VALID_BOARD to be valid'); | |
| assert(validateSudoku(INVALID_BOARD_1) === false, 'Expected INVALID_BOARD_1 to be invalid'); | |
| assert(validateSudoku(INVALID_BOARD_2) === false, 'Expected INVALID_BOARD_2 to be invalid'); | |
| console.log('All tests passing'); | |
| function validateSudoku(board) { | |
| var validator = groupValidator(); | |
| // Validate rows | |
| var x = 0, y = 0; | |
| while (true) { | |
| if (!validator.check(board[y][x])) { | |
| return false | |
| } | |
| x += 1; | |
| if (x === BOARD_SIZE) { | |
| x = 0; | |
| y += 1; | |
| validator.reset(); | |
| if (y === BOARD_SIZE) { | |
| break; | |
| } | |
| } | |
| } | |
| // Validate columns | |
| var x = 0, y = 0; | |
| while (true) { | |
| if (!validator.check(board[y][x])) { | |
| return false | |
| } | |
| y += 1; | |
| if (y === BOARD_SIZE) { | |
| y = 0; | |
| x += 1; | |
| validator.reset(); | |
| if (x === BOARD_SIZE) { | |
| break; | |
| } | |
| } | |
| } | |
| // Validate sections | |
| var x = 0, y = 0; | |
| while (true) { | |
| if (!validator.check(board[y][x])) { | |
| return false | |
| } | |
| x += 1; | |
| if (x % SECTION_SIZE === 0) { | |
| x -= SECTION_SIZE; | |
| y += 1; | |
| if (y % SECTION_SIZE === 0) { | |
| x += SECTION_SIZE; | |
| y -= SECTION_SIZE; | |
| validator.reset(); | |
| } | |
| } | |
| if (x === BOARD_SIZE) { | |
| x = 0; | |
| y += SECTION_SIZE; | |
| } | |
| if (y === BOARD_SIZE) { | |
| break | |
| } | |
| } | |
| return true; | |
| } | |
| function groupValidator() { | |
| var numbers = {}; | |
| return { | |
| check: function(number) { | |
| if (number < 1 || number > BOARD_SIZE) { | |
| return false; | |
| } | |
| // hash-map access is O(1), woot | |
| if (numbers[number]) { | |
| return false; | |
| } | |
| numbers[number] = true; | |
| return true; | |
| }, | |
| reset: function() { | |
| numbers = {}; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment