I'm not Spar - but I do like his lua challenge and it inspired me to do something similar. I'm going to (if I remember) post a weekly little challenge to maybe spark a bit of fun/discussion, talk about various methods - and hopefully give some new developers an idea for something to help them learn.
The 'Challenge': Sudoku Solver
Write a Lua function to solve a given Sudoku puzzle. The function should take a [9][9] table, where empty zells are represented by zeros. The function should fill the grid (change the zeros) to it's corresponding number based on the traditional rules of Sudoku and then print the solved grid
Rules/Requirements:
- Your function should be called solveSudoku and take only a single argument, the board table.
solveSudoku(board) - No outside libraries should be used
- The puzzle should return
trueif the puzzle is solved, otherwisefalseif not. - You can use glua syntax/functions if you want
Bonus/show-off points:
- Solve in the least amount of iterations to solve possible
- Generate a sudoku table based on a difficulty (you chose how many)
- Nice output (honestly go wild, I'm prepared to see some cool ASCII grids)
- Handle multiple solutions
- Handle impossible boards
The base code:
local board = {
{0, 3, 2, 6, 0, 5, 4, 7, 0},
{5, 0, 0, 0, 3, 0, 0, 0, 0},
{7, 0, 0, 0, 0, 0, 0, 0, 8},
{2, 5, 0, 0, 0, 0, 0, 0, 0},
{0, 4, 0, 0, 0, 0, 0, 0, 6},
{8, 0, 0, 9, 2, 0, 0, 0, 5},
{0, 2, 0, 5, 0, 0, 0, 1, 3},
{0, 0, 0, 3, 0, 0, 6, 0, 7},
{0, 8, 0, 0, 9, 7, 0, 5, 4}
}
function solveSudoku(board)
-- TODO: implement
return false
end
solveSudoku(board)
p.s the example board is 100% solvable, I did it myself.. manually..
