-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuValidator.js
170 lines (141 loc) · 4.79 KB
/
sudokuValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Sudoku Background
Sudoku is a game played on a 9x9 grid. The goal of the game is to fill all cells of the grid with digits from 1 to 9, so that each column, each row, and each of the nine 3x3 sub-grids (also known as blocks) contain all of the digits from 1 to 9.
(More info at: http://en.wikipedia.org/wiki/Sudoku)
Sudoku Solution Validator
Write a function validSolution that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions.
Examples:
validSolution([[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]])
//Example 1 should return true
validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 0, 3, 4, 8],
[1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 0, 1, 5, 3, 7, 2, 1, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 0, 0, 4, 8, 1, 1, 7, 9]])
//Example 2 should returns false
http://www.codewars.com/kata/529bf0e9bdf7657179000008
*/
"use strict"
function validSolution(board){
console.log(board);
let mapper = {
1 : [],
2 : [],
3 : [],
4 : [],
5 : [],
6 : [],
7 : [],
8 : [],
9 : []
}
let filler = function(){
let tempArr = []
let tempArr2 = []
for (var i = 0; i < 9 ; i++) {
board.forEach(function(item){
mapper[i+1].push(item[i])
})
mapper[i+10] = board[i]
}
return mapper
}
var quad = function(map){
var eachQuad = function(x, y, mapper){
var i = x
var line = y
var mapper = mapper
var counterMapper = 0
var counterx= i
map[mapper] = []
for (var j = 0; j<3 ; j++) {
map[mapper][j] = map[line][counterx]
counterx++
}
counterx = i
for (var j = 3; j<6 ; j++) {
map[mapper][j] = map[line+1][counterx]
counterx++
}
counterx = i
for (var j = 6; j<9 ; j++) {
map[mapper][j] = map[line+2][counterx]
counterx++
}
}
eachQuad(0, 1, 19)
eachQuad(3, 1, 20)
eachQuad(6, 1, 21)
eachQuad(0, 4, 22)
eachQuad(3, 4, 23)
eachQuad(6, 4, 24)
eachQuad(0, 7, 25)
eachQuad(3, 7, 26)
eachQuad(6, 7, 27)
return mapper
}
let checker = function(map){
let tempArray = []
let shouldBreak = false
let lastItem = -1
for (var i = 1; i < 28; i++) {
console.log("i = "+i);
console.log(map[i]);
tempArray = map[i].sort()
map[i] = tempArray
console.log("==========");
console.log(map[i]);
}
for (var i = 1; i < 28; i++) {
map[i].forEach(function(item){
if (item == lastItem || !item) {
shouldBreak = true
}
lastItem = item
})
}
console.log(mapper);
return !shouldBreak
}
return checker(quad(filler(board)))
}
// console.log(validSolution(([[5, 3, 4, 6, 7, 8, 9, 1, 2],
// [6, 7, 2, 1, 9, 0, 3, 4, 8],
// [1, 0, 0, 3, 4, 2, 5, 6, 0],
// [8, 5, 9, 7, 6, 1, 0, 2, 0],
// [4, 2, 6, 8, 5, 3, 7, 9, 1],
// [7, 1, 3, 9, 2, 4, 8, 5, 6],
// [9, 0, 1, 5, 3, 7, 2, 1, 4],
// [2, 8, 7, 4, 1, 9, 6, 3, 5],
// [3, 0, 0, 4, 8, 1, 1, 7, 9]])));
//
// console.log(validSolution([[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(validSolution([ [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 2, 3, 1, 5, 6, 4, 8, 9, 7 ],
[ 3, 1, 2, 6, 4, 5, 9, 7, 8 ],
[ 4, 5, 6, 7, 8, 9, 1, 2, 3 ],
[ 5, 6, 4, 8, 9, 7, 2, 3, 1 ],
[ 6, 4, 5, 9, 7, 8, 3, 1, 2 ],
[ 7, 8, 9, 1, 2, 3, 4, 5, 6 ],
[ 8, 9, 7, 2, 3, 1, 5, 6, 4 ],
[ 9, 7, 8, 3, 1, 2, 6, 4, 5 ]]));