backtrex v0.1.2 Backtrex.Examples.Sudoku.Puzzle
Represents, updates, and verifies Sudoku puzzles.
Summary
Types
Valid Sudoku cell identifiers
Map
of cell_id
s to cell_value
s
Values allowed in Sudoku cells
Valid Sudoku column identifiers
Valid Sudoku row identifiers
Sudoku puzzle which maps cell IDs to cell values
Functions
Puzzle
struct that maps cell IDs to cell values
IDs of all cells on a Sudoku grid
Get value of cell at cell_id
in the puzzle
Empty cells in the puzzle
Returns whether puzzle
has a numerical value in every cell
Create Puzzle
from list of lists of cell values
Copy of puzzle
where cell_value
is associated with cell_id
9-cell rows, columns, and sectors on the puzzle grid
Returns whether puzzle
is in a solved state
Returns whether puzzle
is valid
Types
Map
of cell_id
s to cell_value
s.
Sudoku puzzle which maps cell IDs to cell values.
Functions
IDs of all cells on a Sudoku grid.
Get value of cell at cell_id
in the puzzle
.
Returns :_
(unoccupied) if no value associated with the cell_id
.
Returns whether puzzle
has a numerical value in every cell.
Examples
iex> %Puzzle{cells: %{{0, 0} => 1}}
...> |> Puzzle.filled_in?
false
iex> %Puzzle{
...> cells: Puzzle.cell_ids
...> |> Enum.map(&({&1, 9}))
...> |> Enum.into(%{})
...> } |> Puzzle.filled_in?
true
Create Puzzle
from list of lists of cell values.
Examples
A puzzle copied from Wikipedia.
iex> Puzzle.from_list([
...> [5, 3, :_, :_, 7, :_, :_, :_, :_],
...> [6, :_, :_, 1, 9, 5, :_, :_, :_],
...> [:_, 9, 8, :_, :_, :_, :_, 6, :_],
...> [8, :_, :_, :_, 6, :_, :_, :_, 3],
...> [4, :_, :_, 8, :_, 3, :_, :_, 1],
...> [7, :_, :_, :_, 2, :_, :_, :_, 6],
...> [:_, 6, :_, :_, :_, :_, 2, 8, :_],
...> [:_, :_, :_, 4, 1, 9, :_, :_, 5],
...> [:_, :_, :_, :_, 8, :_, :_, 7, 9],
...> ])
{:ok, %Puzzle{
cells: %{
{0, 0} => 5, {0, 1} => 3, {0, 4} => 7,
{1, 0} => 6, {1, 3} => 1, {1, 4} => 9, {1, 5} => 5,
{2, 1} => 9, {2, 2} => 8, {2, 7} => 6,
{3, 0} => 8, {3, 4} => 6, {3, 8} => 3,
{4, 0} => 4, {4, 3} => 8, {4, 5} => 3, {4, 8} => 1,
{5, 0} => 7, {5, 4} => 2, {5, 8} => 6,
{6, 1} => 6, {6, 6} => 2, {6, 7} => 8,
{7, 3} => 4, {7, 4} => 1, {7, 5} => 9, {7, 8} => 5,
{8, 4} => 8, {8, 7} => 7, {8, 8} => 9}}}
Copy of puzzle
where cell_value
is associated with cell_id
.
9-cell rows, columns, and sectors on the puzzle grid.
Each region must have no repeated cell values. See valid?
.
Returns whether puzzle
is in a solved state.
A Sudoku puzzle is said to be solved if it is both filled_in?
and
valid?
.
Examples
iex> {:ok, puzzle} = Puzzle.from_list([
...> [5, 3, 4, 6, 7, 8, 9, 1, 2],
...> [6, 7, 2, 1, 9, 5, 3, 4, 8],
...> [1, 9, 8, 3, 4, 2, 5, 6, 7],
...> [8, 5, 9, 7, 6, 1, 4, 2, 3],
...> [4, 2, 6, 8, 5, 3, 7, 9, 1],
...> [7, 1, 3, 9, 2, 4, 8, 5, 6],
...> [9, 6, 1, 5, 3, 7, 2, 8, 4],
...> [2, 8, 7, 4, 1, 9, 6, 3, 5],
...> [3, 4, 5, 2, 8, 6, 1, 7, 9]])
iex> {puzzle |> Puzzle.solved?,
...> puzzle |> Puzzle.put_cell({0, 0}, 1) |> Puzzle.solved?}
{true, false}
Returns whether puzzle
is valid.
Each of puzzle
’s 81 cells must be occupied by a number from 1-9,
inclusive, or the :_
atom which means “unoccupied”. Cells with no
value are implictly :_
.
Additionally, no individual row, column, nor sector may have repeated
cell number values. Sectors are the puzzle
’s nine 3x3 sub-grids.
Examples
iex> %Puzzle{cells: %{{0, 0} => 1}}
...> |> Puzzle.valid?
true
iex> %Puzzle{cells: %{{0, 0} => 1, {0, 7} => 1}}
...> |> Puzzle.valid?
false