Example #1
0
func main() {
	numSolutions = 0
	// create the grid.
	grid := *datatypes.InitGrid()
	count := 0
	inputValues := make(map[int]bool)
	// read input into the grid.
	for i := 0; i < max; i++ {
		count += readRow(&grid, i, inputValues)
	}
	// At least 17 values, and 8 distinct values are required for a unique solution. (Necessary condition, but not sufficient).
	if count < 17 || len(inputValues) < 8 {
		handleError("Too few input values given. At least 17 values, and 8 distinct values must be given.", nil)
	}
	// solve using given inputs without making any guess.
	positions := solve(&grid, count)
	// make a guess for a position and start solving; backtrack if there is any conflict.
	solveByGuessing(&grid, positions, 0)
	fmt.Println("Total solutions:", numSolutions)
	// Set difficulty as easy if solved without making any guess, medium if number of guesses is less than 9, and hard otherwise.
	difficultyLevel := easy
	if len(positions) > 0 {
		if len(positions) < max {
			difficultyLevel = medium
		} else {
			difficultyLevel = hard
		}
	}
	fmt.Println("Difficulty level:", difficultyLevel)
}
Example #2
0
// TestSolve initializes and solves the grid.
func TestSolve(t *testing.T) {
	grid := *datatypes.InitGrid()
	count := setInput(&grid)
	positions := solve(&grid, count)
	if len(positions) < 37 {
		t.Error("Expected at least 37, got ", len(positions))
	}
	solveByGuessing(&grid, positions, 0)
}
Example #3
0
func BenchmarkSolve(b *testing.B) {
	for n := 0; n < b.N; n++ {
		grid := *datatypes.InitGrid()
		count := setInput(&grid)
		positions := solve(&grid, count)
		if len(positions) != 37 {
			b.Error("Expected 37, got ", len(positions))
		}
		solveByGuessing(&grid, positions, 0)
	}
}