sudokuconv

package module
v0.0.0-...-24f3e65 Latest Latest
Warning

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

Go to latest
Published: May 31, 2018 License: MIT Imports: 4 Imported by: 0

README

sudokuconv

GoDoc code coverage

Package sudokuconv contains helpers to convert solved 9x9 sudokus to compact byte slices.

It will be used by https://sudokoin.com soon to add meta information to transactions.

Documentation

Overview

Package sudokuconv contains helpers to convert solved 9x9 sudokus to compact byte slices.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/jraedisch/sudokuconv"
)

func main() {
	board := [9][9]int{
		{9, 8, 7, 6, 5, 4, 3, 2, 1},
		{6, 5, 4, 3, 2, 1, 9, 8, 7},
		{3, 2, 1, 9, 8, 7, 6, 5, 4},
		{8, 9, 6, 7, 4, 5, 2, 1, 3},
		{7, 4, 5, 2, 1, 3, 8, 9, 6},
		{2, 1, 3, 8, 9, 6, 7, 4, 5},
		{5, 7, 9, 4, 6, 8, 1, 3, 2},
		{4, 6, 8, 1, 3, 2, 5, 7, 9},
		{1, 3, 2, 5, 7, 9, 4, 6, 8},
	}

	bytes, err := sudokuconv.ToBytes(board)

	if err != nil {
		// board is not solved correctly
	}

	board2, err := sudokuconv.FromBytes(bytes)

	if err != nil {
		// bytes are corrupted
	}

	fmt.Println(reflect.DeepEqual(board, board2))
}
Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromBytes

func FromBytes(bytes []byte) ([9][9]int, error)

FromBytes converts bytes (see ToBytes) back to board. An error is returned iff the provided bytes are malformed.

func ToBytes

func ToBytes(board [9][9]int) ([]byte, error)

ToBytes converts a 9x9 sudoku board into a compact bit representation. Size is 23 or 24 bytes depending on where the 9s are. The returned byte slice contains 4 bits for the row where the 9 is in the last column. Then follow 3 bits for each of the other eight columns containing 9s. Then the other symbols are converted and appended as 3 bits each. For this, 1-8 are converted to 0-7. The last row and column are left out since they can trivially be computed. An error is returned iff the provided board is not correctly solved.

Types

This section is empty.

Jump to

Keyboard shortcuts

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