Feedback on my Sudoku solver using backtracking

I’ve been playing with backtracking in Elixir, would appreciate feedback for my code or logic.

The entire code is a bit long so please look at it here: https://gist.github.com/xlphs/ab08f52bab51f2b5922decacea177b0f

Here is the core backtracking logic, the trigger for backtracking is no more choices and no solution.

# entry point, start by generating a list of choices
def bt(problem, candidate) do
  {next_candidate, choices} = choose(problem, candidate)
  bt(problem, next_candidate, choices)
end
def bt(problem, candidate, [choice|rest]) do
  next_candidate = next(problem, candidate, choice)
  case bt(problem, next_candidate) do
    {:ok, solution} -> {:ok, solution}
    false -> bt(problem, candidate, rest)
  end
end
def bt(problem, candidate, []) do
  case accept(problem, candidate) do
    true -> {:ok, candidate}
    false -> false
  end
end
2 Likes