Struct sudoku::Sudoku[][src]

pub struct Sudoku(_);

The main structure exposing all the functionality of the library

Sudokus can generated, constructed from arrays or parsed from &strs in either the line or block format.

Methods

impl Sudoku
[src]

Generate a random, solved sudoku

Generate a random, uniquely solvable sudoku The puzzles are minimal in that no cell can be removed without losing uniquess of solution Most puzzles generated by this are easy

Generate a random, uniqely solvable sudoku that has the same solution as the given sudoku by removing the contents of some of its cells. The puzzles are minimal in that no cell can be removed without losing uniquess of solution. Most puzzles generated by this from solved sudokus are easy.

If the source sudoku is invalid or has multiple solutions, it will be returned as is.

Creates a sudoku from a byte slice. All numbers must be below 10. Empty cells are denoted by 0, clues by the numbers 1-9. The slice must be of length 81.

Creates a sudoku from a byte array. All numbers must be below 10. Empty cells are denoted by 0, clues by the numbers 1-9.

Reads a sudoku in the line format.

This is a concatenation of the digits in each cell, line by line from top to bottom. Digits must be in range of 1-9. '_', '.' and '0' are accepted interchangeably as empty cells

An optional comment is allowed after the sudoku, separated by ASCII whitespace, commas or semicolons, that is, any of ' ', '\t', '\n', '\r', ',', ';'

Example:

..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3.. optional comment

Stops parsing after the first sudoku

Reads a sudoku in the block format with or without field delimiters

Digits must be in range of 1-9. '_', '.' and '0' are accepted interchangeably as empty cells

Optional comments are accepted after each line. They must be delimited by ' ' or '\t', i.e. a space or a tab character.

__3_2_6__ optional comment
9__3_5__1 another comment
__18_64__
__81_29__
7_______8
__67_82__
__26_95__
8__2_3__9
__5_1_3__

alternatively also with field delimiters

__3|_2_|6__ optional comment
9__|3_5|__1 another comment
__1|8_6|4__
---+---+--- comment: "-----------", i.e. '-' 11 times is also allowed
__8|1_2|9__          delimiters have to be consistent across the entire
7__|___|__8          grid
__6|7_8|2__
---+---+---
__2|6_9|5__
8__|2_3|__9
__5|_1_|3__

Stops parsing after the first sudoku

Reads a sudoku in a variety of block formats with very few constraints.

'_', '.' and '0' are treated as empty cells. '1' to '9' as clues. Each line needs to have 9 valid cells. Lines that don't contain 9 valid entries are ignored.

Stops parsing after the first sudoku.

Due to the lax format rules, the only failure that can occur is that there are not enough rows.

Find a solution to the sudoku. If multiple solutions exist, it will not find them and just stop at the first. Return None if no solution exists.

Solve sudoku and return solution if solution is unique.

Counts number of solutions to sudoku up to limit This solves the sudoku but does not return the solutions which allows for slightly faster execution.

Checks whether sudoku has one and only one solution. This solves the sudoku but does not return the solution which allows for slightly faster execution.

Solve sudoku and return the first limit solutions it finds. If less solutions exist, return only those. Return None if no solution exists. No specific ordering of solutions is promised. It can change across versions.

Counts number of solutions to sudoku up to limit and writes any solution found into target up to its capacity. Additional solutions will be counted but not saved. No specific ordering of solutions is promised. It can change across versions. This is primarily meant for C FFI.

Check whether the sudoku is solved.

Returns number of filled cells

Perform various transformations that create a different but equivalent sudoku. The transformations preserve the sudoku's validity and the amount of solutions as well a the applicability of solution strategies. Shuffling can be used to quickly generate sudokus of the same difficulty as a given sudoku.

Transformations that are applied:

  • Relabel numbers, e.g. swap all 1s and all 3s (9! permutations)
  • Permute rows within their band and columns within their stack (3!3 * 2 permutations)
  • Permute stacks and bands (3!2 permutations)
  • Transpose the board, i.e. mirror it along the diagonal (2 permutations) The remaining rotations as well as mirrorings can be produced by a combination with the other transformations

This results in a total of up to 2 * 9! * 3!8 = 1,218,998,108,160 permutations Less permutations exists if the sudoku is symmetrical in respect to some combinations of the transformations The vast majority of sudokus do not have any such symmetries (automorphisms). The highest number of automorphisms a sudoku can have is 648 and ~99.99% of all non-equivalent sudokus have only 1, the identity transformation.

Returns an Iterator over sudoku, going from left to right, top to bottom

Returns a byte array for the sudoku. Empty cells are denoted by 0, clues by the numbers 1-9.

Returns a representation of the sudoku in line format that can be printed and which derefs into a &str

use sudoku::Sudoku;

let mut grid = [0; 81];
grid[3] = 5;
let sudoku = Sudoku::from_bytes(grid).unwrap();
let line = sudoku.to_str_line(); // :SudokuLine
println!("{}", line);

let line_str: &str = &line;
assert_eq!(
    "...5.............................................................................",
    line_str
);

Returns a value that, prints a block representation of the sudoku when formatted via the Display trait.

use sudoku::Sudoku;

let mut grid = [0; 81];
grid[3] = 5;
grid[36..45].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9]);
let sudoku = Sudoku::from_bytes(grid).unwrap();
let block = sudoku.display_block(); // :SudokuBlock

let block_string = format!("{}", block);
assert_eq!(
    &block_string,
"
___ 5__ ___
___ ___ ___
___ ___ ___

___ ___ ___
123 456 789
___ ___ ___

___ ___ ___
___ ___ ___
___ ___ ___"
);

Trait Implementations

impl Copy for Sudoku
[src]

impl Clone for Sudoku
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for Sudoku
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd for Sudoku
[src]

The ordering is lexicographical in the cells of the sudoku going from left to right, top to bottom

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Ord for Sudoku
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl Hash for Sudoku
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl Eq for Sudoku
[src]

impl Debug for Sudoku
[src]

Formats the value using the given formatter. Read more

impl Display for Sudoku
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Sudoku

impl Sync for Sudoku