Solve your sudoku puzzles with Elixir — Part-2


In Part-1, we parsed an input_str, create an input_map and apply this map to the initial board. To be ready to follow Part-2, you need this to work.

We will now operate on this representation, which is our map.
If you use my repository then switch to part-2 branch ;)

Project structure

In this part we will create 2 files:

├── _build
├── config
│ └── config.exs
├── index.js
├── lib
│ ├── sudoku
│ │ ├── board.ex
│ │ ├── backtracking.ex <--
│ │ ├── data_structure_utils.ex
│ │ ├── strategies
│ │ │ ├── apply_values.ex
│ │ └── validation.ex <--
│ └── sudoku.ex
├── mix.exs
├── package.json
├── stuff.js
└── test
├── sudoku_board_test.exs
├── sudoku_test.exs
└── test_helper.exs

Using backtracking : the dumb way

source code
test file

Here is the backtracking algorithm on wikipedia but basically the idea is trying values until it fails, go back, change the values and continue like that until it works.

I tried to explain with words what is going to happend here but it’s always better with a graph.

Data structure

We will work on a list of two-element tuple. Notice that even if we have only one value left, the value is still a list.

stack = [  {{0,0}, [1,2,3,4,5,6,7,8,9]},  {{1,0}, [4]}, ...]

Isolate coordinates that need actions

We will use adding, changing and droping a lot in backtracking. Input values (input_str and input_map) will never change. So let’s put them aside and create a list of coordinates that will change.


Named functions support multiple clauses. I tend to use this concept a lot so if you are not familiar with anonymous/named functions or multiple clauses (which is pattern matching for functions) you can go here.
You can implement this part to fully understand what’s going on if you want to.




Validating our solutions

source code
test file

Here is the validation code for a row

Logic is the same for columns and boxes, here is your turn ! You can implement the functions for columns, boxes and the general is_valid? which validate the whole board or grab the source file.

Updating our map of possibilities with the stack

The stack is going to be manipulate a lot of time and if we want to have a valid map of possibilities we need to keep it up to date ! This is the goal of update_map which under the hood use our ApplyValues module from

Let’s compute !

We will define our run function that will initialize and prepare our data then we will define the do_run that will do the real job

If you want you can try to implement the do_run it’s nothing more than reading the graph.

Do you think it’s working now ?? well let’s test in iex !

Hey ! Hey ! Look at this ! Our first sudoku solved ! Let’s try these:

OMG… the last one was reeaaaaally long what’s going on ? Well we only apply values and start computing by trying all the combinations so it’s not really optimize.
We have 81 elements and 9 possibilities by element and we apply 22 values (81–22) * 9 = 707! which is …


possibilities… I’m not kidding. So that’s why it’s a bit long.

I will keep my ‘score’ here in order to compare later:

input_str_1 --> ~400ms
input_str_2 --> ~330ms
input_str_3 --> out of memory on my computer
input_str_4 --> out of memory on my computer
input_str_5 --> ~15s
The euler project n96 --> out of memory on my computer


We finally solve our first puzzles successfully but the satisfaction was short-lived, Our algorithm is slow…
In PART-3 we will find a way to reduce the number of possibilities before using backtracking.