Solve your sudoku puzzles with Elixir

Introduction

Why this project ?

I started learning Elixir few months ago and after finishing the Elixir channel on Exercism.io, I was looking for a bigger project. I had this Euler project in my head for a long time and the timing was excellent to start it.

Why Sudoku ?

Sudoku is maybe not the most exciting thing but everybody knows what it is and probably already tried once to solve one. So people will be more likely to understand me if I write an article or show the project for job interviews.

Get started

First things first, You need to install Elixir on your computer. Then you should see something like this:

Part-1: Create the board and apply values

At the end of this part you will be able to generate an empty board, apply initial values on it and generate this:

Introduction

  • There are 9 rows from 0 to 8 and this one is the first one (be careful this is 0 based)
    [{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1}]
  • There are 9 columns from 0 to 8 and this one is the first one
    [{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}]
  • There are 9 boxes and this one is the number zero
    [{0,0} {1,0} {2,0} {0,1} {1,1} {2,1} {0,2} {1,2} {2,2}]
├── _build
...
├── config
│ └── config.exs
├── index.js
├── lib
│ ├── sudoku
│ │ ├── board.ex <--
│ │ ├── data_structure_utils.ex <--
│ │ ├── strategies
│ │ │ ├── apply_values.ex <--
│ └── sudoku.ex
├── mix.exs
├── package.json
├── README.md
├── stuff.js
└── test
├── sudoku_board_test.exs <--
├── sudoku_test.exs
└── test_helper.exs

Board

source code
test file

Generating a row:

Generating rows

We have to do the same from 0 to 8.

Applying values

source code

input_str = 003020600900305001001806400008102900700000008006708200002609500800203009005010300

Bonus: The Display module

source code

├── _build
...
├── config
│ └── config.exs
├── index.js
├── lib
│ ├── sudoku
│ │ ├── board.ex
│ │ ├── display.ex <--
│ │ ├── strategies
│ │ │ ├── apply_values.ex
│ └── sudoku.ex
├── mix.exs
├── package.json
├── README.md
├── stuff.js
└── test
├── sudoku_board_test.exs
├── sudoku_test.exs
└── test_helper.exs
  • drawing a line of items
  • drawing a line of separation