-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.rb
156 lines (133 loc) · 4.29 KB
/
sudoku.rb
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
require 'pry'
# hardest known sudoku
sudoku_input = [
[8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 6, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 9, 0, 2, 0, 0],
[0, 5, 0, 0, 0, 7, 0, 0, 0],
[0, 0, 0, 0, 4, 5, 7, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3, 0],
[0, 0, 1, 0, 0, 0, 0, 6, 8],
[0, 0, 8, 5, 0, 0, 0, 1, 0],
[0, 9, 0, 0, 0, 0, 4, 0, 0]
]
# # medium sudoku
# sudoku_input = [
# [0, 0, 5, 3, 0, 0, 0, 0, 0],
# [8, 0, 0, 0, 0, 0, 0, 2, 0],
# [0, 7, 0, 0, 1, 0, 5, 0, 0],
# [4, 0, 0, 0, 0, 5, 3, 0, 0],
# [0, 1, 0, 0, 7, 0, 0, 0, 6],
# [0, 0, 3, 2, 0, 0, 0, 8, 0],
# [0, 6, 0, 5, 0, 0, 0, 0, 9],
# [0, 0, 4, 0, 0, 0, 0, 3, 0],
# [0, 0, 0, 0, 0, 9, 7, 0, 0]
# ]
# # easy sudoku
# sudoku_input = [
# [1, 0, 0, 0, 0, 7, 0, 9, 0],
# [0, 3, 0, 0, 2, 0, 0, 0, 8],
# [0, 0, 9, 6, 0, 0, 5, 0, 0],
# [0, 0, 5, 3, 0, 0, 9, 0, 0],
# [0, 1, 0, 0, 8, 0, 0, 0, 2],
# [6, 0, 0, 0, 0, 4, 0, 0, 0],
# [3, 0, 0, 0, 0, 0, 0, 1, 0],
# [0, 4, 0, 0, 0, 0, 0, 0, 7],
# [0, 0, 7, 0, 0, 0, 3, 0, 0]
# ]
@backtracking_count = 0
def print_sudoku(sudoku)
print "\n"
width = sudoku.flatten.max.to_s.size+2
sudoku.each_with_index do |i, i_index|
i_third_element = ((i_index + 1) % 3).zero?
print "\n" unless i_index.zero?
i.each_with_index do |j, j_index|
j_third_element = ((j_index + 1) % 3).zero?
j_nonzero=(j.to_s.rjust(width) + (j_third_element ? ' ' : ''))
unless j.zero?
print "\e[37m#{j_nonzero}\e[0m"
else
print "\e[31m#{j_nonzero}\e[0m"
end
end
print (i_third_element ? "\n" : "")
end
print "\n"
end
# first identity the next empty position
# then validate the sudoku with the next element
# if valid move to next else backtrack using recursion
def solve_sudoku(sudoku)
position = find_position(sudoku)
return true unless position
(1..9).to_a.each do |element|
if valid_position(sudoku, position, element)
# print_sudoku(sudoku)
# puts "position[0]=#{position[0]}, position[1]=#{position[1]}, sudoku[position[0]][position[1]]=#{sudoku[position[0]][position[1]]}, element=#{element}"
# binding.pry
sudoku[position[0]][position[1]] = element
# using recursion for backtracking
return true if solve_sudoku(sudoku)
# set the element to zero for the position when the element is not fit (backtrack)
# move to the next element in 1-9
sudoku[position[0]][position[1]] = 0
@backtracking_count += 1
end
end
false
end
# return position if the value is zero
def find_position(sudoku)
(0..8).to_a.each_with_index do |i|
(0..8).to_a.each_with_index do |j|
return [i,j] if sudoku[i][j] == 0
end
end
false
end
def valid_position(sudoku, position, element)
# return 'false' if there is an element greater then zero in the position
return false if sudoku[position[0]][position[1]] > 0
# return 'false' if existing element in the row or column of the position
(0..8).to_a.each do |i|
return false if (sudoku[position[0]][i] == element) || (sudoku[i][position[1]] == element)
end
# return 'false' if the existing element is in the same 3x3 block
((position[0]/3 * 3)..((position[0]/3 * 3) + 2)).to_a.each do |i|
((position[1]/3 * 3)..((position[1]/3 * 3) + 2)).to_a.each do |j|
return false if sudoku[i][j] == element
end
end
true
end
def valid_sudoku_input(sudoku)
(0..8).to_a.each_with_index do |i|
(0..8).to_a.each_with_index do |j|
return false if (sudoku[i][j] > 9 || sudoku[i][j] < 0)
if sudoku[i][j] > 0
(0..8).to_a.each do |k|
next if i == k || j == k
return false if (sudoku[i][k] == sudoku[i][j]) || (sudoku[k][j] == sudoku[i][j])
end
((i/3 * 3)..((i/3 * 3) + 2)).to_a.each do |l|
((j/3 * 3)..((j/3 * 3) + 2)).to_a.each do |m|
next if (i == l && j == m)
return false if sudoku[l][m] == sudoku[i][j]
end
end
end
end
end
true
end
print_sudoku(sudoku_input)
if !valid_sudoku_input(sudoku_input)
print "\e[31mInvalid Sudoku\e[0m"
return
end
solve_sudoku(sudoku_input)
print_sudoku(sudoku_input)
puts "Backtrack iterations: #{@backtracking_count}"
# TIME COMPLEXITY (K^N)
# time complexity is K^N, where K is number of times the function calls itself