Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Forked from dbc-challenges/sample.unsolved.txt
Last active December 17, 2015 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjhea0/4ec197b18389a32a4f4c to your computer and use it in GitHub Desktop.
Save mjhea0/4ec197b18389a32a4f4c to your computer and use it in GitHub Desktop.
Sudoku
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
290500007700000400004738012902003064800050070500067200309004005000080700087005109
080020000040500320020309046600090004000640501134050700360004002407230600000700450
608730000200000460000064820080005701900618004031000080860200039050000100100456200
370000001000700005408061090000010000050090460086002030000000000694005203800149500
=begin
1) find cell
2) test if empty
3) if the cell is empty
*4) horizontal test (num % 9) and vertical test (num % 9): use the test array (which is an array with the numbers 1 - 9) and compare (subtract) that from the arrays in the suduko nested array. find horizontal and vertical and push to a new array.
*5) box test: (divide by 27 && [num % 9]/3)
6) if the cell is not empty move to the next cell (start back at 2)
7) continue loop until no values in array are 0
=end
#require "sample_unsolved.txt"
def quit(message = '')
puts message
exit
end
class Sudoku
attr_accessor :board_string
def initialize(board_string)
@board_array = string_to_array(board_string).map { |x| x.to_i }
end
def solve!
until @board_array.include?(0) == false
@board_array.each_with_index do |cell, i|
excluded_numbers = []
if cell == 0
excluded_numbers += grab_row(i)
excluded_numbers += grab_column(i)
excluded_numbers += grab_box(i)
excluded_numbers
excluded_numbers.uniq!
possible_numbers = (1..9).to_a-excluded_numbers
if possible_numbers.length == 1
@board_array[i]=possible_numbers[0]
end
end
end
end
end
def board
@board_array.join
end
def print_board
temp_array = @board_array.dup
print "#{temp_array[0..8].join(' ')}\n"
print "#{temp_array[9..17].join(' ')}\n"
print "#{temp_array[18..26].join(' ')}\n"
print "#{temp_array[27..35].join(' ')}\n"
print "#{temp_array[36..44].join(' ')}\n"
print "#{temp_array[45..53].join(' ')}\n"
print "#{temp_array[54..62].join(' ')}\n"
print "#{temp_array[63..71].join(' ')}\n"
print "#{temp_array[72..80].join(' ')}\n"
print "\n\n"
end
def grab_row(target_index)
return_array = []
target_value = target_index / 9
@board_array.each_with_index do |cell, index|
return_array << cell if (index / 9 == target_value and cell != 0)
end
return_array
end
def grab_column(target_index)
return_array = []
target_value = target_index % 9
@board_array.each_with_index do |cell, index|
return_array << cell if (index % 9 == target_value and cell != 0)
end
return_array
end
def grab_box(target_index)
return_array = []
target_column_num = target_index % 9 / 3
target_row_num = target_index / 27
@board_array.each_with_index do |cell, i|
return_array << cell if (i % 9 / 3 == target_column_num and i / 27 == target_row_num and cell != 0)
end
return_array
end
def string_to_array(string)
string.split('')
end
end
# The file has newlines at the end of each line, so we call
# String#chomp to remove them.
#board_string = File.readlines('sample.unsolved.txt').first.chomp
file = open("puzzles.txt", "r")
file.each do |puzzle_string|
# current_time = Time.now
new_game = Sudoku.new(puzzle_string.chomp)
new_game.print_board
new_game.solve!
new_game.print_board
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment