Class: SudokuGenius::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/sudoku_genius/solver.rb

Instance Method Summary collapse

Constructor Details

#initializeSolver

Returns a new instance of Solver.



5
6
7
8
9
10
11
12
# File 'lib/sudoku_genius/solver.rb', line 5

def initialize
  @starting_dash_count = 0
  @ending_dash_count = 0
  @starting_board = nil
  @final_board = []
  @solved = nil
  @start_time = Time.now
end

Instance Method Details

#fill_dashes(data_hashes, rows) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sudoku_genius/solver.rb', line 171

def fill_dashes(data_hashes, rows)
  lowest_option_count_hash = get_lowest_option_count_hash(data_hashes)

  if !lowest_option_count_hash.nil?
    lowest_option_count = lowest_option_count_hash[:options].count

    if lowest_option_count == 1 ## fill dashes if only one option available.
      data_hashes.each do |data_hash|
        r_i = data_hash[:coords][0]
        c_i = data_hash[:coords][1]
        rows[r_i][c_i] = data_hash[:options].first if data_hash[:options].count == 1
      end
    end

    if lowest_option_count > 1
      r_i = lowest_option_count_hash[:coords][0]
      c_i = lowest_option_count_hash[:coords][1]
      rows[r_i][c_i] = lowest_option_count_hash[:options].sample
    end

  end
  rows
end

#find_box(rows) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sudoku_genius/solver.rb', line 100

def find_box(rows)
  boxes = [[]] * 9
  boxes[0] = rows[0][0..2]+rows[1][0..2]+rows[2][0..2]
  boxes[1] = rows[0][3..5]+rows[1][3..5]+rows[2][3..5]
  boxes[2] = rows[0][6..8]+rows[1][6..8]+rows[2][6..8]
  boxes[3] = rows[3][0..2]+rows[4][0..2]+rows[5][0..2]
  boxes[4] = rows[3][3..5]+rows[4][3..5]+rows[5][3..5]
  boxes[5] = rows[3][6..8]+rows[4][6..8]+rows[5][6..8]
  boxes[6] = rows[6][0..2]+rows[7][0..2]+rows[8][0..2]
  boxes[7] = rows[6][3..5]+rows[7][3..5]+rows[8][3..5]
  boxes[8] = rows[6][6..8]+rows[7][6..8]+rows[8][6..8]
  boxes
end

#find_column(rows) ⇒ Object



84
85
86
# File 'lib/sudoku_genius/solver.rb', line 84

def find_column(rows)
  rows[0..-1].transpose.to_a
end

#find_dash_coords(rows) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sudoku_genius/solver.rb', line 88

def find_dash_coords(rows)
  coords = []
  rows.each do |row|
    if row.include?("-")
      for col in 0...row.length
        coords << [rows.index(row), col] if row[col] == '-'
      end
    end
  end
  coords
end

#find_dash_options(data_hashes) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/sudoku_genius/solver.rb', line 151

def find_dash_options(data_hashes)
  all_numbers = (1..9).to_a
  data_hashes.each do |data_hash|
    numbers_played = data_hash[:row] + data_hash[:col] + data_hash[:box]
    numbers_played.uniq!.delete("-")
    options = all_numbers - numbers_played.map!(&:to_i)
    data_hash[:options] = options
  end
end

#format_puzzle_string(puzzle_string) ⇒ Object



57
58
59
# File 'lib/sudoku_genius/solver.rb', line 57

def format_puzzle_string(puzzle_string)
  rows = puzzle_string.chomp.chars.each_slice(9).to_a
end

#get_dash_count_totals(rows) ⇒ Object



69
70
71
# File 'lib/sudoku_genius/solver.rb', line 69

def get_dash_count_totals(rows)
  total_dash_count = rows.flatten.join.count "-"
end

#get_lowest_option_count_hash(data_hashes) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/sudoku_genius/solver.rb', line 161

def get_lowest_option_count_hash(data_hashes)
  counter = 0
  lowest_option_count_hash = nil
  while !lowest_option_count_hash && counter < 10
    counter += 1
    lowest_option_count_hash = data_hashes.find { |hash| hash[:options].count == counter }
  end
  lowest_option_count_hash
end

#grab_artoObject



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/sudoku_genius/solver.rb', line 231

def grab_arto
  # p = %w[8--------
  # --36-----
  # -7--9-2--
  # -5---7---
  # ----457--
  # ---1---3-
  # --1----68
  # --85---1-
  # -9----4--]

  p = "8----------36------7--9-2---5---7-------457-----1---3---1----68--85---1--9----4--"
end

#grab_puzzlesObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/sudoku_genius/solver.rb', line 210

def grab_puzzles
  %w[
    1-58-2----9--764-52--4--819-19--73-6762-83-9-----61-5---76---3-43--2-5-16--3-89--
    --5-3--819-285--6-6----4-5---74-283-34976---5--83--49-15--87--2-9----6---26-495-3
    29-5----77-----4----4738-129-2--3-648---5--7-5---672--3-9--4--5----8-7---87--51-9
    -8--2-----4-5--32--2-3-9-466---9---4---64-5-1134-5-7--36---4--24-723-6-----7--45-
    6-873----2-----46-----6482--8---57-19--618--4-31----8-86-2---39-5----1--1--4562--
    ---6891--8------2915------84-3----5-2----5----9-24-8-1-847--91-5------6--6-41----
    -3-5--8-45-42---1---8--9---79-8-61-3-----54---5------78-----7-2---7-46--61-3--5--
    -96-4---11---6---45-481-39---795--43-3--8----4-5-23-18-1-63--59-59-7-83---359---7
    ----754----------8-8-19----3----1-6--------34----6817-2-4---6-39------2-53-2-----
    3---------5-7-3--8----28-7-7------43-----------39-41-54--3--8--1---4----968---2--
    3-26-9--55--73----------9-----94----------1-9----57-6---85----6--------3-19-82-4-
    -2-5----48-5--------48-9-2------5-73-9-----6-25-9------3-6-18--------4-71----4-9-
    ----------2-65-------18--4--9----6-4-3---57-------------------73------9----------
    8----------36------7--9-2---5---7-------457-----1---3---1----68--85---1--9----4--
    ---------------------------------------------------------------------------------
  ]
end

#looper(puzzle_string, updated_rows) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/sudoku_genius/solver.rb', line 73

def looper(puzzle_string, updated_rows)
  starting_dash_count = get_dash_count_totals(updated_rows)
  return solved?(updated_rows) if starting_dash_count == 0
  data_hashes = find_dash_options(make_data_hash(updated_rows))
  updated_rows = fill_dashes(data_hashes, updated_rows)
  ending_dash_count = get_dash_count_totals(updated_rows)
  return solved?(updated_rows) if ending_dash_count == 0

  (ending_dash_count != starting_dash_count) ? looper(puzzle_string, updated_rows) : looper(puzzle_string, res = format_puzzle_string(puzzle_string))
end

#make_data_hash(rows) ⇒ Object



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
# File 'lib/sudoku_genius/solver.rb', line 114

def make_data_hash(rows)
  cols = find_column(rows)
  all_dash_coords = find_dash_coords(rows)
  boxes = find_box(rows)

  data = []

  all_dash_coords.each do |coords|
    r_i = coords[0]
    c_i = coords[1]

    data_hash = {coords: coords, row: rows[r_i], col: cols[c_i]  }

    if (0..2) === r_i && (0..2) === c_i
      data_hash[:box] = boxes[0]
    elsif (0..2) === r_i && (3..5) === c_i
      data_hash[:box] = boxes[1]
    elsif (0..2) === r_i && (6..8) === c_i
      data_hash[:box] = boxes[2]
    elsif (3..5) === r_i && (0..2) === c_i
      data_hash[:box] = boxes[3]
    elsif (3..5) === r_i && (3..5) === c_i
      data_hash[:box] = boxes[4]
    elsif (3..5) === r_i && (6..8) === c_i
      data_hash[:box] = boxes[5]
    elsif (6..8) === r_i && (0..2) === c_i
      data_hash[:box] = boxes[6]
    elsif (6..8) === r_i && (3..5) === c_i
      data_hash[:box] = boxes[7]
    elsif (6..8) === r_i && (6..8) === c_i
      data_hash[:box] = boxes[8]
    end
    data << data_hash
  end
  data
end

#number_or_nil(string) ⇒ Object



52
53
54
55
# File 'lib/sudoku_genius/solver.rb', line 52

def number_or_nil(string)
  num = string.to_i
  num.to_s == string ? num : string
end

#play(args = {}) ⇒ Object

AlgoService.new.play



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
# File 'lib/sudoku_genius/solver.rb', line 19

def play(args={})
  sudoku_scores = []
  puzzles = args.fetch(:puzzles, grab_puzzles)
  total_rounds = puzzles.count

  totals = puzzles.enum_for(:each_with_index).map do |puzzle,i|
    refresh_values
    starting_board = format_puzzle_string(puzzle)
    starting_dash_count = get_dash_count_totals(starting_board)
    start_puzzle(puzzle)

    unless @solved == nil
      sudoku_scores << { round: i+1,
              rounds: total_rounds,
              solved: @solved,
              starting_board: remove_quotes(starting_board),
              final_board: remove_quotes(@final_board),
              duration: Time.now - @start_time,
              starting_dash_count: starting_dash_count,
              ending_dash_count: ending_dash_count = get_dash_count_totals(@final_board)
            }

    end
  end
  return sudoku_scores.reverse
end

#pretty_board(board) ⇒ Object



204
205
206
207
# File 'lib/sudoku_genius/solver.rb', line 204

def pretty_board(board)
  pretty_rows = board.map { |row| row.join('  ') }
  pretty_rows.map! { |row| row.insert(-1, "\n") }
end

#refresh_valuesObject



14
15
16
# File 'lib/sudoku_genius/solver.rb', line 14

def refresh_values
  initialize
end

#remove_quotes(rows) ⇒ Object



46
47
48
49
50
# File 'lib/sudoku_genius/solver.rb', line 46

def remove_quotes(rows)
  rows.map! do |row|
    row.map { |el| number_or_nil(el) }
  end
end

#solved?(board) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
199
200
201
# File 'lib/sudoku_genius/solver.rb', line 196

def solved?(board)
  puts "\n\n#{"="*25}\nThe board was solved!\n\n"
  puts pretty_board(board)
  @solved = true
  return @solved
end

#start_puzzle(puzzle_string) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/sudoku_genius/solver.rb', line 61

def start_puzzle(puzzle_string)
  rows = format_puzzle_string(puzzle_string)
  data_hashes = find_dash_options(make_data_hash(rows))
  updated_rows = fill_dashes(data_hashes, rows)
  @final_board = updated_rows if !@final_board.any?
  res = looper(puzzle_string, updated_rows)
end