-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.rb
217 lines (173 loc) · 4.01 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Takes a board as a string in the format
# you see in the puzzle file. Returns
# something representing a board after
# your solver has tried to solve it.
# How you represent your board is up to you!
def solve(board_string)
end
# Returns a boolean indicating whether
# or not the provided board is solved.
# The input board will be in whatever
# form `solve` returns.
def solved?(board)
end
# Takes in a board in some form and
# returns a _String_ that's well formatted
# for output to the screen. No `puts` here!
# The input board will be in whatever
# form `solve` returns.
def pretty_board(board)
end
def find_zero(board)
row = 0
column = 0
until row == 9 && column == 0
if board[row][column] == 0
return [row, column]
end
if column == 8
column = 0
row += 1
else
column += 1
end
end
false
end
def data_to_int(mixed_string)
mixed_string.chars.map!{ |item| item.to_i }
end
def create_board(array)
begin_idx = 0
end_idx = 8
board_array = []
until end_idx > 80
board_array << array[begin_idx..end_idx]
begin_idx += 9
end_idx += 9
end
board_array
end
def box_identifier(location_array)
row, column = location_array
box_row1 = (0..2)
box_row2 = (3..5)
box_row3 = (6..8)
box_column1 = (0..2)
box_column2 = (3..5)
box_column3 = (6..8)
if box_row1.include?(row) && box_column1.include?(column)
return 1
elsif box_row1.include?(row) && box_column2.include?(column)
return 2
elsif box_row1.include?(row) && box_column3.include?(column)
return 3
elsif box_row2.include?(row) && box_column1.include?(column)
return 4
elsif box_row2.include?(row) && box_column2.include?(column)
return 5
elsif box_row2.include?(row) && box_column3.include?(column)
return 6
elsif box_row3.include?(row) && box_column1.include?(column)
return 7
elsif box_row3.include?(row) && box_column2.include?(column)
return 8
elsif box_row3.include?(row) && box_column3.include?(column)
return 9
end
end
def box_start_location(boxnum)
case boxnum
when 1
return [0, 0]
when 2
return [0, 3]
when 3
return [0, 6]
when 4
return [3, 0]
when 5
return [3, 3]
when 6
return [3, 6]
when 7
return [6, 0]
when 8
return [6, 3]
when 9
return [6, 6]
end
end
def check_row(board, row_idx, num_search)
board[row_idx].include?(num_search)
end
def check_column(board, column_idx, num_search)
check_row(board.transpose, column_idx, num_search)
end
def check_box(board, cell_coordinates, num_search)
row, column = box_start_location(box_identifier(cell_coordinates))
stop_row = row + 2
stop_column = column + 2
until ow == stop_row && column == stop_column
if num_search == board[row][column]
return true
end
if column == stop_column
column = 0
row += 1
else
column += 1
end
end
false
end
def cell_check(board, start_array, num_search)
row_idx, column_idx = start_array
check_r = check_row(board, row_idx, num_search)
check_c = check_column(board, column_idx, num_search)
check_b = check_box(board, start_array, num_search)
if check_r || check_c || check_b
true
else
false
end
end
def cell_conflicts(board, start_location)
z = 1
cell = {}
until z == 10
conflict = cell_check(board, start_location, z)
cell[z] = conflict
z += 1
end
cell
end
def false_count(hash)
conflict_collection = []
hash.each_pair {|num, conflict| conflict_collection << conflict }
conflict_collection.count(false)
end
def loop_through_zeros(board)
cell_location = find_zero(board)
row, column = cell_location
until row == false
# p cell_location
hash = cell_conflicts(board, cell_location)
if false_count(hash) <= 1
num = hash.key(false)
board[row][column] = num
else
board[row][column] = ''
end
cell_location = find_zero(board)
row, column = cell_location
end
board
end
# def loop(board)
# new_board = []
# 4.times do |board|
# result = loop_through_zeros(board)
# end
# p result
# end