Skip to content

Instantly share code, notes, and snippets.

@Ingelheim
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 1 You must be signed in to fork a gist
  • Save Ingelheim/7e65d9993ec7c0eb47fd to your computer and use it in GitHub Desktop.
Save Ingelheim/7e65d9993ec7c0eb47fd to your computer and use it in GitHub Desktop.
096040001100060004504810390007950043030080000405023018010630059059070830003590007
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
096040001100060004504810390007950043030080000405023018010630059059070830003590007
105802000090076405200400819019007306762083090000061050007600030430020501600308900
005030081902850060600004050007402830349760005008300490150087002090000600026049503
290500007700000400004738012902003064800050070500067200309004005000080700087005109
080020000040500320020309046600090004000640501134050700360004002407230600000700450
608730000200000460000064820080005701900618004031000080860200039050000100100456200
370000001000700005408061090000010000050090460086002030000000000694005203800149500
000689100800000029150000008403000050200005000090240801084700910500000060060410000
030500804504200010008009000790806103000005400050000007800000702000704600610300500
000075400000000008080190000300001060000000034000068170204000603900000020530200000
300000000050703008000028070700000043000000000003904105400300800100040000968000200
302609005500730000000000900000940000000000109000057060008500006000000003019082040
# require 'debugger'
class Sudoku
attr_reader :board, :clone_board
# --- INITIALIZE THE GAME ---
def initialize(string)
@string = string
@new_string = ""
@board = []
@clone_board = []
start_game
total_solve
end
# --- GAMEFLOW ---
def start_game
change_to_X
clone_board
# initialize_empty_board
end
# --- SETTING UP THE BOARD ---
def change_to_X
@new_string = @string.gsub!("0", "X")
@board = create_board(@new_string)
# p @board
end
def create_board(string)
temp_board = []
start_board = string.split("")
start_board.each_slice(9) {|array| temp_board << array}
temp_board
end
def clone_board
@clone_board = create_board(@new_string)
end
def view_board
9.times do |x|
puts "="*100 if x % 3 == 0
9.times do |y|
print "||".ljust(5) if y % 3 == 0
print @board[x][y].to_s.ljust(10)
end
puts "\n"
end
end
def view_clone_board
9.times do |x|
puts "="*100 if x % 3 == 0
9.times do |y|
print "||".ljust(5) if y % 3 == 0
print @clone_board[x][y].to_s.ljust(20)
end
puts "\n"
end
end
# --- TOOLBELT ---
# This method updates the cell value by replacing it on the grid
def update_cell!(x_coordinate, y_coordinate, replace_with)
new_board[x_coordinate][y_coordinate] = replace_with
end
# This method deletes the number from the possibilities of the array if called
def deleter!(x_coordinate, y_coordinate, number)
end
# Checks if board is solved
def solved?
end
# --- SETTING POSSIBILITIES ---
# This method will check if the current grid item is either a number or an X. Returns boolean
# If true loop to next
# Else: call method to fill with start-array
def total_solve
counter = 0
until check_for_Xs?
check_possibilities
counter += 1
end
puts "It took #{counter} loops to figure out!"
view_board
end
def check_for_Xs?
9.times do |x|
9.times do |y|
return false if @board[x][y].match(/^X/)
end
end
true
end
def check_possibilities
(0...9).each do |x|
(0...9).each do |y|
if @board[x][y] == "X"
possible_nums = check_possible_nums(x,y, @board)
@clone_board[x][y] = possible_nums
flatten(x,y) if possible_nums.size == 1
end
end
end
(0...9).each do |x|
(0...9).each do |y|
check_square_single_outs(x,y)
end
end
end
def flatten(x, y)
@board[x][y] = @clone_board[x][y][0].to_s
end
def check_possible_nums(row,col,board)
good_nums = (1..9).to_a
good_nums -= col_poss(board, col)
good_nums -= row_poss(board, row)
good_nums -= square_poss(board, row, col)
good_nums
end
def row_poss(board, row)
bad_nums = []
9.times do |y|
bad_nums << board[row][y].to_i if board[row][y] != "X"
end
bad_nums
end
def col_poss(board, col)
bad_nums = []
9.times do |x|
bad_nums << board[x][col].to_i if board[x][col] != "X"
end
bad_nums
end
def row_col_loop(board, x_offset, y_offset)
bad_nums = []
3.times do |x|
3.times do |y|
bad_nums << board[x + x_offset][y + y_offset].to_i if board[x+x_offset][y+y_offset] != "X"
end
end
bad_nums
end
def square_poss(board, row, col)
if row.between?(0,2)
return row_col_loop(board, 0, 0) if col.between?(0, 2)
return row_col_loop(board, 0, 3) if col.between?(3, 5)
return row_col_loop(board, 0, 6) if col.between?(6, 8)
elsif row.between?(3,5)
return row_col_loop(board, 3, 0) if col.between?(0, 2)
return row_col_loop(board, 3, 3) if col.between?(3, 5)
return row_col_loop(board, 3, 6) if col.between?(6, 8)
else
return row_col_loop(board, 6, 0) if col.between?(0, 2)
return row_col_loop(board, 6, 3) if col.between?(3, 5)
return row_col_loop(board, 6, 6) if col.between?(6, 8)
end
end
def check_square_single_looper(board, x_offset, y_offset)
bad_nums = []
# build bad nums array
3.times do |x|
3.times do |y|
if board[x + x_offset][y + y_offset].kind_of?(Array)
board[x + x_offset][y + y_offset].each {|elem| bad_nums << elem}
end
end
end
# check against bad nums
3.times do |x|
3.times do |y|
if board[x + x_offset][y + y_offset].kind_of?(Array)
board[x + x_offset][y + y_offset].each_with_index do |three_deep_elem, index|
if bad_nums.count(board[x+ x_offset][y+y_offset][index]) == 1
@clone_board[x + x_offset][y + y_offset] = board[x + x_offset][y + y_offset][index].to_s
@board[x + x_offset][y + y_offset] = @clone_board[x + x_offset][y + y_offset]
end
end
end
end
end
end
def check_square_single_outs(row, col, board = @clone_board)
if row.between?(0,2)
return check_square_single_looper(board, 0, 0) if col.between?(0, 2)
return check_square_single_looper(board, 0, 3) if col.between?(3, 5)
return check_square_single_looper(board, 0, 6) if col.between?(6, 8)
elsif row.between?(3,5)
return check_square_single_looper(board, 3, 0) if col.between?(0, 2)
return check_square_single_looper(board, 3, 3) if col.between?(3, 5)
return check_square_single_looper(board, 3, 6) if col.between?(6, 8)
else
return check_square_single_looper(board, 6, 0) if col.between?(0, 2)
return check_square_single_looper(board, 6, 3) if col.between?(3, 5)
return check_square_single_looper(board, 6, 6) if col.between?(6, 8)
end
end
end
sudoku2 = Sudoku.new("096040001100060004504810390007950043030080000405023018010630059059070830003590007")
sudoku = Sudoku.new("105802000090076405200400819019007306762083090000061050007600030430020501600308900")
sudoku3 = Sudoku.new("005030081902850060600004050007402830349760005008300490150087002090000600026049503")
sudoku4 = Sudoku.new("096040001100060004504810390007950043030080000405023018010630059059070830003590007")
sudoku5 = Sudoku.new("105802000090076405200400819019007306762083090000061050007600030430020501600308900")
sudoku6 = Sudoku.new("005030081902850060600004050007402830349760005008300490150087002090000600026049503")
sudoku7 = Sudoku.new("290500007700000400004738012902003064800050070500067200309004005000080700087005109")
sudoku8 = Sudoku.new("080020000040500320020309046600090004000640501134050700360004002407230600000700450")
sudoku9 = Sudoku.new("608730000200000460000064820080005701900618004031000080860200039050000100100456200")
sudoku10 = Sudoku.new("370000001000700005408061090000010000050090460086002030000000000694005203800149500")
sudoku11 = Sudoku.new("000689100800000029150000008403000050200005000090240801084700910500000060060410000")
sudoku12 = Sudoku.new("030500804504200010008009000790806103000005400050000007800000702000704600610300500")
sudoku14 = Sudoku.new("300000000050703008000028070700000043000000000003904105400300800100040000968000200")
# sudoku15 = Sudoku.new("302609005500730000000000900000940000000000109000057060008500006000000003019082040") this one sucks too
# sudoku13 = Sudoku.nw("000075400000000008080190000300001060000000034000068170204000603900000020530200000") this one sucks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment