Guest User

Untitled

a guest
Feb 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. const INVALID_SUDOKU_GRID = [
  2. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  3. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  4. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  5. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  6. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  7. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  8. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  9. [1, 2, 3, 4, 5, 6, 7, 8, 9],
  10. [1, 2, 3, 4, 5, 6, 7, 8, 9]
  11. ];
  12.  
  13. const VALID_SUDOKU_GRID = [
  14. [5, 4, 6, 8, 9, 3, 2, 1, 7],
  15. [3, 9, 2, 1, 7, 4, 6, 8, 5],
  16. [8, 1, 7, 5, 2, 6, 9, 3, 4],
  17. [7, 6, 9, 3, 4, 1, 8, 5, 2],
  18. [2, 3, 4, 7, 8, 5, 1, 9, 6],
  19. [1, 8, 5, 2, 6, 9, 7, 4, 3],
  20. [4, 5, 8, 6, 1, 2, 3, 7, 9],
  21. [9, 2, 1, 4, 3, 7, 5, 6, 8],
  22. [6, 7, 3, 9, 5, 8, 4, 2, 1]
  23. ];
  24.  
  25. // main driver function
  26. const main = sudokuGrid => {
  27. // rows_grid --> rows as rows
  28. // columns_grid --> columns as rows
  29. // blocks_grid --> 3 x 3 blocks as rows
  30.  
  31. let rows_grid = [...sudokuGrid];
  32.  
  33. let columns_grid = [];
  34. for (let i = 0; i < 9; i++) {
  35. columns_grid[i] = [];
  36. for (let j = 0; j < 9; j++) {
  37. columns_grid[i][j] = sudokuGrid[j][i];
  38. }
  39. }
  40.  
  41. let blocks_grid = [];
  42. for (let i = 0; i < 3; i++) {
  43. for (let j = 0; j < 3; j++) {
  44. blocks_grid[3 * i + j] = [];
  45. for (let k = 0; k < 3; k++) {
  46. for (let l = 0; l < 3; l++) {
  47. // console.log(3 * i + k, 3 * j + l);
  48. // console.log(3 * i + j, 3 * k + l);
  49. blocks_grid[3 * i + j][3 * k + l] = sudokuGrid[3 * i + k][3 * j + l];
  50. }
  51. }
  52. }
  53. }
  54.  
  55. rows_grid = sortSudokuGridRows(rows_grid);
  56. rows_grid_flag = checkSudokuGridRows(rows_grid);
  57. if (rows_grid_flag) {
  58. columns_grid = sortSudokuGridRows(columns_grid);
  59. columns_grid_flag = checkSudokuGridRows(columns_grid);
  60. if (columns_grid_flag) {
  61. blocks_grid = sortSudokuGridRows(blocks_grid);
  62. blocks_grid_flag = checkSudokuGridRows(blocks_grid);
  63. if (blocks_grid_flag) {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. } else {
  69. return false;
  70. }
  71. } else {
  72. return false;
  73. }
  74.  
  75. // console.log(rows_grid);
  76. // console.log(columns_grid);
  77. // console.log(blocks_grid);
  78. };
  79.  
  80. // sorts every row
  81. const sortSudokuGridRows = sudokuGrid => {
  82. for (let i = 0; i < 9; i++) {
  83. sudokuGrid[i] = sudokuGrid[i].sort();
  84. }
  85. return sudokuGrid;
  86. };
  87.  
  88. // checks if every row has numbers from 1 to 9
  89. const checkSudokuGridRows = sudokuGrid => {
  90. for (let i = 0; i < 9; i++) {
  91. for (let j = 0; j < 9; j++) {
  92. if (sudokuGrid[i][j] !== j + 1) {
  93. return false;
  94. }
  95. }
  96. }
  97. return true;
  98. };
  99.  
  100. /*
  101.  
  102. ** Rows Grid
  103. [ [ 5, 4, 6, 8, 9, 3, 2, 1, 7 ],
  104. [ 3, 9, 2, 1, 7, 4, 6, 8, 5 ],
  105. [ 8, 1, 7, 5, 2, 6, 9, 3, 4 ],
  106. [ 7, 6, 9, 3, 4, 1, 8, 5, 2 ],
  107. [ 2, 3, 4, 7, 8, 5, 1, 9, 6 ],
  108. [ 1, 8, 5, 2, 6, 9, 7, 4, 3 ],
  109. [ 4, 5, 8, 6, 1, 2, 3, 7, 9 ],
  110. [ 9, 2, 1, 4, 3, 7, 5, 6, 8 ],
  111. [ 6, 7, 3, 9, 5, 8, 4, 2, 1 ] ]
  112.  
  113. ** Columns Grid
  114. [ [ 5, 3, 8, 7, 2, 1, 4, 9, 6 ],
  115. [ 4, 9, 1, 6, 3, 8, 5, 2, 7 ],
  116. [ 6, 2, 7, 9, 4, 5, 8, 1, 3 ],
  117. [ 8, 1, 5, 3, 7, 2, 6, 4, 9 ],
  118. [ 9, 7, 2, 4, 8, 6, 1, 3, 5 ],
  119. [ 3, 4, 6, 1, 5, 9, 2, 7, 8 ],
  120. [ 2, 6, 9, 8, 1, 7, 3, 5, 4 ],
  121. [ 1, 8, 3, 5, 9, 4, 7, 6, 2 ],
  122. [ 7, 5, 4, 2, 6, 3, 9, 8, 1 ] ]
  123.  
  124. ** Blocks Grid
  125. [ [ 5, 4, 6, 3, 9, 2, 8, 1, 7 ],
  126. [ 8, 9, 3, 1, 7, 4, 5, 2, 6 ],
  127. [ 2, 1, 7, 6, 8, 5, 9, 3, 4 ],
  128. [ 7, 6, 9, 2, 3, 4, 1, 8, 5 ],
  129. [ 3, 4, 1, 7, 8, 5, 2, 6, 9 ],
  130. [ 8, 5, 2, 1, 9, 6, 7, 4, 3 ],
  131. [ 4, 5, 8, 9, 2, 1, 6, 7, 3 ],
  132. [ 6, 1, 2, 4, 3, 7, 9, 5, 8 ],
  133. [ 3, 7, 9, 5, 6, 8, 4, 2, 1 ] ]
  134.  
  135. */
  136.  
  137. // call the main function and print the output
  138. main(VALID_SUDOKU_GRID) ? console.log("Valid") : console.log("Invalid");
  139. // VALID
  140. main(INVALID_SUDOKU_GRID) ? console.log("Valid") : console.log("Invalid");
  141. // INVALID
Add Comment
Please, Sign In to add comment