-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy path5kyu_DidIFinishMySudoku.js
103 lines (85 loc) · 3.29 KB
/
5kyu_DidIFinishMySudoku.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
// // 5 kyu - Did I Finish my Sudoku?
// Description:
// Write a function done_or_not passing a board (list[list_lines]) as parameter. If the
// board is valid return 'Finished!', otherwise return 'Try again!'
// Sudoku rules:
// Complete the Sudoku puzzle so that each and every row, column, and region contains the
// numbers one through nine only once.
// Rows:
// 5 3 8 6 9 7 4 1 2
// There are 9 rows in a traditional Sudoku puzzle. Every row must contain the numbers
// 1, 2, 3, 4, 5, 6, 7, 8, and 9. There may not be any duplicate numbers in any row. In
// other words, there can not be any rows that are identical.
// In the illustration the numbers 5, 3, 1, and 2 are the "givens". They can not be
// changed. The remaining numbers in black are the numbers that you fill in to complete
// the row.
// Columns:
// 7
// 4
// 8
// 5
// 9
// 2
// 1
// 3
// 6
// There are 9 columns in a traditional Sudoku puzzle. Like the Sudoku rule for rows,
// every column must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Again,
// there may not be any duplicate numbers in any column. Each column will be unique as
// a result.
// In the illustration the numbers 7, 2, and 6 are the "givens". They can not be changed.
// You fill in the remaining numbers as shown in black to complete the column.
// Regions
// 4 5 1
// 6 9 7
// 3 2 8
// A region is a 3x3 box like the one shown to the left. There are 9 regions in a
// traditional Sudoku puzzle.
// Like the Sudoku requirements for rows and columns, every region must also contain
// the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Duplicate numbers are not permitted in
// any region. Each region will differ from the other regions.
// In the illustration the numbers 1, 2, and 8 are the "givens". They can not be
// changed. Fill in the remaining numbers as shown in black to complete the region.
// Valid board example:
// [[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]]
// For those who don't know the game, here are some information about rules and how
// to play Sudoku:
function checkVector(arr) {
for(let i = 1; i < 10; i++)
if(arr.indexOf(i) < 0)
return false;
return true;
}
function doneOrNot(board) {
let [region1, region2, region3, valid] = [[], [], [], true];
for(let i = 0; i < board.length; i++) {
if(!checkVector(board[i]))
valid = false;
}
for(let i = 0; i < 9; i++) {
let arr = []
for(let j = 0; j < 9; j++)
arr = arr.concat(board[j][i]);
if(!checkVector(arr))
valid = false;
}
for(let i = 0; i < 9; i++) {
[region1, region2, region3] = [region1.concat(board[i].slice(0,3)),
region2.concat(board[i].slice(3,6)),
region3.concat(board[i].slice(6,9))]
if((i+1)%3 === 0) {
if(!checkVector(region1) || !checkVector(region2) || !checkVector(region3))
valid = false;
[region1, region2, region3] = [[], [], []]
}
}
return valid ? "Finished!" : "Try again!";
}