sudoku

package module
v0.0.0-...-aa7c405 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README


Sudoku Solvers and Simplifiers

This project aims to implement different techniques for simplifying and solving sudokus.

Beware of breaking changes! The API is not stable at all!

For good descriptions of some techniques check out Sudoku of the Day (no affiliation)!

You can see the code in action at sudokoin.com.

All help is greatly appreciated!

Documentation

Overview

Package sudoku contains sudoku solvers following different strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnnotatedBoard

type AnnotatedBoard struct {
	Board
	Candidates [][]Candidates
}

AnnotatedBoard contains "penciled" candidates.

func CandidateLines

func CandidateLines(ab AnnotatedBoard) (ab2 AnnotatedBoard, succeeded bool)

CandidateLines tries to simplify by finding candidates of the same kind on the same row or column within a block, so that they can be safely removed from other fields of that line not within that block.

func NewAnnotatedBoard

func NewAnnotatedBoard(bo Board) (ab AnnotatedBoard, err error)

NewAnnotatedBoard returns an annotated version of provided board.

func (AnnotatedBoard) Annotate

func (ab AnnotatedBoard) Annotate() (AnnotatedBoard, error)

Annotate (naively) annotates a board with possible candidates for each field. All data except board will be overwritten.

func (AnnotatedBoard) Copy

func (ab AnnotatedBoard) Copy() (ab2 AnnotatedBoard)

Copy returns a copy of the annotated board. Helpful to stay as immutible as possible for now.

func (AnnotatedBoard) Solved

func (ab AnnotatedBoard) Solved() bool

Solved returns true if board is solved correctly.

type Board

type Board [][]int

Board contains all fields of a simple, unannotated sudoku.

func Backtrack

func Backtrack(ab AnnotatedBoard, maxSolutions int) (solved bool, solutions []Board)

Backtrack implements a simple backtracking solver. It is not performant but guaranteed to finish.

func GenerateSimple

func GenerateSimple(random Board, minimum int) (unsolved Board)

GenerateSimple generates a board that is solvable with only single candidates strategy. Minimum param sets the number of minimum numbers that should remain on sudoku. minimum <= 0 will be ignored (hardest) and the higher the easier it gets. minimum >= size² makes no sense.

func NewEmptyBoard

func NewEmptyBoard(size int) Board

NewEmptyBoard builds an empty board with provided size.

func NewFromShort

func NewFromShort(s string, size int) (bo Board, err error)

NewFromShort tries to parse provided short notation into board with provided size.

func NewRandomBoard

func NewRandomBoard(size int) Board

NewRandomBoard returns a solved, pseudo random board of provided size.

func SingleCandidate

func SingleCandidate(ab AnnotatedBoard, maxSolutions int) (bool, []Board)

SingleCandidate will look through annotated board for fields with just one candidate, fill it, recalculate candidates for all fields, and repeat, until board is solved or no longer solvable. It will not return more than one solution.

func (Board) Copy

func (bo Board) Copy() (bo2 Board)

Copy returns a copy of the board. Helpful to stay as immutible as possible for now.

func (Board) FirstEmpty

func (bo Board) FirstEmpty() (y, x int, found bool)

FirstEmpty returns coordinates of first empty field and whether one was found.

func (Board) Full

func (bo Board) Full() bool

Full returns if there are no empty fields left. It does not check correctness.

func (Board) Short

func (bo Board) Short() (s string, err error)

Short returns simple sudoku string representation, e.g. "a18b52".

func (Board) Size

func (bo Board) Size() int

Size returns the size of the sides of the board

func (Board) UltraShort

func (bo Board) UltraShort() (s string, err error)

UltraShort returns an even shorter identifier only if board is full. It does so by returning only values and removing the last column and row. A 9x9 sudoku will thus result in 64 chars.

type Candidates

type Candidates int

Candidates contains all "penciled" numbers that may occupy a field.

func (Candidates) Add

func (c Candidates) Add(v int) Candidates

Add adds provided number to candidates (if not exists).

func (Candidates) Complement

func (c Candidates) Complement(size int) Candidates

Complement returns the candidates not represented assuming a sudoku of provided size.

func (Candidates) Contains

func (c Candidates) Contains(v int) bool

Contains checks whether candidates contain provided number.

func (Candidates) Decimals

func (c Candidates) Decimals() (dcs []int)

Decimals returns candidates as a decimal array.

func (Candidates) Remove

func (c Candidates) Remove(v int) Candidates

Remove provided number from candidates (if exists).

func (Candidates) Single

func (c Candidates) Single() bool

Single returns whether candidates contain a single candidate.

type Parser

type Parser func(unparsed string) (bo Board, err error)

A Parser is a function that parses a certain string representation of a sudoku into a board.

type Simplifier

type Simplifier func(ab AnnotatedBoard) (ab2 AnnotatedBoard, succeeded bool)

A Simplifier does not solve a sudoku but tries to remove candidates.

type Solver

type Solver func(ab AnnotatedBoard, maxSolutions int) (solved bool, solutions []Board)

A Solver is a function that tries to solve an annotated board following a certain strategy. An integer should be provided to limit the amount of calculated solutions. -1 means, as many solutions as possible will be calculated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL