Sudoku: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: syntax coloured, made p2js compatible |
Sudoku Solver Implementation in Crystal |
||
Line 2,561: | Line 2,561: | ||
(7 5 9 2 3 8 1 4 6) |
(7 5 9 2 3 8 1 4 6) |
||
(8 2 6 4 1 9 7 3 5))</pre> |
(8 2 6 4 1 9 7 3 5))</pre> |
||
=={{header|Crystal}}== |
|||
Based on the Java implementation presented in the video "[https://www.youtube.com/watch?v=mcXc8Mva2bA Create a Sudoku Solver In Java...]". |
|||
<lang ruby>GRID_SIZE = 9 |
|||
def isNumberInRow(board, number, row) |
|||
board[row].includes?(number) |
|||
end |
|||
def isNumberInColumn(board, number, column) |
|||
board.any?{|row| row[column] == number } |
|||
end |
|||
def isNumberInBox(board, number, row, column) |
|||
localBoxRow = row - row % 3 |
|||
localBoxColumn = column - column % 3 |
|||
(localBoxRow...(localBoxRow+3)).each do |i| |
|||
(localBoxColumn...(localBoxColumn+3)).each do |j| |
|||
return true if board[i][j] == number |
|||
end |
|||
end |
|||
false |
|||
end |
|||
def isValidPlacement(board, number, row, column) |
|||
return !isNumberInRow(board, number, row) && |
|||
!isNumberInColumn(board, number, column) && |
|||
!isNumberInBox(board, number, row, column) |
|||
end |
|||
def solveBoard(board) |
|||
board.each_with_index do |row, i| |
|||
row.each_with_index do |cell, j| |
|||
if(cell == 0) |
|||
(1..GRID_SIZE).each do |n| |
|||
if(isValidPlacement(board,n,i,j)) |
|||
board[i][j]=n |
|||
if(solveBoard(board)) |
|||
return true |
|||
else |
|||
board[i][j]=0 |
|||
end |
|||
end |
|||
end |
|||
return false |
|||
end |
|||
end |
|||
end |
|||
return true |
|||
end |
|||
def printBoard(board) |
|||
board.each_with_index do |row, i| |
|||
row.each_with_index do |cell, j| |
|||
print cell |
|||
print '|' if j == 2 || j == 5 |
|||
print '\n' if j == 8 |
|||
end |
|||
print "-"*11 + '\n' if i == 2 || i == 5 |
|||
end |
|||
print '\n' |
|||
end |
|||
board = [ |
|||
[7, 0, 2, 0, 5, 0, 6, 0, 0], |
|||
[0, 0, 0, 0, 0, 3, 0, 0, 0], |
|||
[1, 0, 0, 0, 0, 9, 5, 0, 0], |
|||
[8, 0, 0, 0, 0, 0, 0, 9, 0], |
|||
[0, 4, 3, 0, 0, 0, 7, 5, 0], |
|||
[0, 9, 0, 0, 0, 0, 0, 0, 8], |
|||
[0, 0, 9, 7, 0, 0, 0, 0, 5], |
|||
[0, 0, 0, 2, 0, 0, 0, 0, 0], |
|||
[0, 0, 7, 0, 4, 0, 2, 0, 3]] |
|||
printBoard(board) |
|||
if(solveBoard(board)) |
|||
printBoard(board) |
|||
end |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
702|050|600 |
|||
000|003|000 |
|||
100|009|500 |
|||
----------- |
|||
800|000|090 |
|||
043|000|750 |
|||
090|000|008 |
|||
----------- |
|||
009|700|005 |
|||
000|200|000 |
|||
007|040|203 |
|||
732|458|619 |
|||
956|173|824 |
|||
184|629|537 |
|||
----------- |
|||
871|564|392 |
|||
643|892|751 |
|||
295|317|468 |
|||
----------- |
|||
329|786|145 |
|||
418|235|976 |
|||
567|941|283 |
|||
</pre> |
|||
=={{header|Curry}}== |
=={{header|Curry}}== |
||
Copied from [http://www.informatik.uni-kiel.de/~curry/examples/ Curry: Example Programs]. |
Copied from [http://www.informatik.uni-kiel.de/~curry/examples/ Curry: Example Programs]. |