backtrex v0.1.2 Backtrex.Examples.Sudoku.Puzzle

Represents, updates, and verifies Sudoku puzzles.

Summary

Types

Valid Sudoku cell identifiers

Map of cell_ids to cell_values

Values allowed in Sudoku cells

Valid Sudoku column identifiers

Valid Sudoku row identifiers

t()

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

cell_id()
cell_id() :: {row_id, column_id}

Valid Sudoku cell identifiers.

cell_map()
cell_map() :: %{optional(cell_id) => cell_value}

Map of cell_ids to cell_values.

cell_value()
cell_value() :: 1..9 | :_

Values allowed in Sudoku cells.

column_id()
column_id() :: 0..8

Valid Sudoku column identifiers.

row_id()
row_id() :: 0..8

Valid Sudoku row identifiers.

t()
t() :: %Backtrex.Examples.Sudoku.Puzzle{cells: cell_map}

Sudoku puzzle which maps cell IDs to cell values.

Functions

__struct__()

Puzzle struct that maps cell IDs to cell values.

cell_ids()
cell_ids() :: Enum.t

IDs of all cells on a Sudoku grid.

cell_value(puzzle, cell_id)
cell_value(t, cell_id) :: cell_value

Get value of cell at cell_id in the puzzle.

Returns :_ (unoccupied) if no value associated with the cell_id.

empty_cells(puzzle)
empty_cells(t) :: Enum.t

Empty cells in the puzzle.

filled_in?(puzzle)
filled_in?(t) :: boolean

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
from_list(rows)
from_list([[cell_value]]) :: {:ok, t} | {:error, any}

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}}}
put_cell(puzzle, cell_id, cell_value)
put_cell(t, cell_id, cell_value) :: t

Copy of puzzle where cell_value is associated with cell_id.

regions()
regions() :: Enum.t

9-cell rows, columns, and sectors on the puzzle grid.

Each region must have no repeated cell values. See valid?.

solved?(puzzle)
solved?(t) :: boolean

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}
valid?(puzzle)
valid?(t) :: boolean

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