Skip to content

A simple golang sudoku solver using Peter Norvig algorithm

License

Notifications You must be signed in to change notification settings

laurentlp/sudoku-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sudoku-solver

wercker status Coverage Status Go Report Card

A simple golang sudoku solver using Peter Norvig algorithm

Installation

$ go get github.com/laurentlp/sudoku-solver

To install the dependencies (Tests + API)

Install glide (Package Management for Golang)

$ curl https://glide.sh/get | sh

Or via Homebrew on MacOS

$ brew install glide

Then to install the dependencies the dependencies

$ glide i

Running the tests

Test the solver

$ cd $GOPATH/src/github.com/laurentlp/sudoku-solver
$ go test -v ./solver

Test everything

Simply run the test.sh file like so

$ cd $GOPATH/src/github.com/laurentlp/sudoku-solver
$ ./test.sh

*Before, make sure the file is executable

$ chmod 777 test.sh

API

To use the API, simply run the main.go file in the command line. Then enter your sudoku in the body of the request as shown bellow :

Make a POST request to http://localhost:8080/sudoku

with the body (JSON) :

{
    "sudoku" : "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......"
}

Result :

solved.jpg from the examples folder

Basic usage of the solver

First create a separate go project in which you will need a main.go file

package main

import (
    "fmt"

    "github.com/laurentlp/sudoku-solver/solver"
)

func main() {
    // Directly write a sudoku grid or use a file as shown in the examples folder
    grid := "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......"
    resolved, err := solver.Solve(grid)

    if err != nil {
        fmt.Print(err)
    }

    solver.Display(resolved)
}

Now to run the file just hit go run main.go in the command line at the root of your project.

You should see an output looking like this :

$ go run main.go

4 1 7 | 3 6 9 | 8 2 5
6 3 2 | 1 5 8 | 9 4 7
9 5 8 | 7 2 4 | 3 1 6
------+-------+-------
8 2 5 | 4 3 7 | 1 6 9
7 9 1 | 5 8 6 | 4 3 2
3 4 6 | 9 1 2 | 7 5 8
------+-------+-------
2 8 9 | 6 4 3 | 5 7 1
5 7 3 | 2 9 1 | 6 8 4
1 6 4 | 8 7 5 | 2 9 3

About

A simple golang sudoku solver using Peter Norvig algorithm

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published