<commit_before>package sudoku

import (
	"math/rand"
	"sync"
)

//Solve searches for a solution to the puzzle as it currently exists without
//unfilling any cells. If one exists, it will fill in all cells to fit that
//solution and return true. If there are no solutions the grid will remain
//untouched and it will return false. If multiple solutions exist, Solve will pick one at random.
func (self *Grid) Solve() bool {

	//Special case; check if it's already solved.
	//TODO: removing this causes Solve, when called on an already solved grid, to sometimes fail. Why is that?
	if self.Solved() {
		return true
	}

	//TODO: Optimization: we only need one, so we can bail as soon as we find a single one.
	solutions := self.Solutions()
	if len(solutions) == 0 {
		return false
	}
	self.Load(solutions[0].DataString())
	return true
}

//NumSolutions returns the total number of solutions found in the grid when it is solved forward
//from this point. A valid Sudoku puzzle has only one solution. Does not mutate the grid.
func (self *Grid) NumSolutions() int {
	return len(self.Solutions())
}

//HasSolution returns true if the grid has at least one solution. Does not mutate the grid.
func (self *Grid) HasSolution() bool {
	//TODO: optimize this to bail as soon as we find a single solution.
	return len(self.nOrFewerSolutions(1)) > 0
}

//HasMultipleSolutions returns true if the grid has more than one solution. Does not mutate the grid.
func (self *Grid) HasMultipleSolutions() bool {
	return len(self.nOrFewerSolutions(2)) >= 2
}

//Solutions returns a slice of grids that represent possible solutions if you were to solve forward this grid. The current grid is not modified.
//If there are no solutions forward from this location it will return a slice with len() 0. Does not mutate the grid.
func (self *Grid) Solutions() (solutions []*Grid) {
	return self.nOrFewerSolutions(0)
}

//The actual workhorse of solutions generating. 0 means "as many as you can find". It might return more than you asked for, if it already had more results than requested sitting around.
func (self *Grid) nOrFewerSolutions(max int) []*Grid {

	self.cachedSolutionsLock.RLock()
	hasNoCachedSolutions := self.cachedSolutions == nil
	cachedSolutionsLen := self.cachedSolutionsRequestedLength
	self.cachedSolutionsLock.RUnlock()

	if hasNoCachedSolutions || (max > 0 && cachedSolutionsLen < max && cachedSolutionsLen > 0) {

		queueDone := make(chan bool, 1)

		queue := newSyncedFiniteQueue(0, DIM*DIM, queueDone)

		queue.In <- self.Copy()

		//In the past this wasn't buffered, but then when we finished early other items would try to go into it
		//and block, which prevented them from looping back up and getting the signal to shut down.
		//Since there's only a known number of threads, we'll make sure they all ahve a place to leave their work
		//without blocking so they can get the signal to shut down.
		incomingSolutions := make(chan *Grid, _NUM_SOLVER_THREADS)

		//Using a pattern for closing fan in style receivers from http://blog.golang.org/pipelines
		var wg sync.WaitGroup

		var solutions []*Grid

		//TODO: figure out a way to kill all of these threads when necessary.

		for i := 0; i < _NUM_SOLVER_THREADS; i++ {<commit_msg>Fixed it so ALL TESTS PASS. We now recalc solutions if we had previously asked for a specific number of solutions and are now asking for all solutions.<commit_after>package sudoku

import (
	"math/rand"
	"sync"
)

//Solve searches for a solution to the puzzle as it currently exists without
//unfilling any cells. If one exists, it will fill in all cells to fit that
//solution and return true. If there are no solutions the grid will remain
//untouched and it will return false. If multiple solutions exist, Solve will pick one at random.
func (self *Grid) Solve() bool {

	//Special case; check if it's already solved.
	//TODO: removing this causes Solve, when called on an already solved grid, to sometimes fail. Why is that?
	if self.Solved() {
		return true
	}

	//TODO: Optimization: we only need one, so we can bail as soon as we find a single one.
	solutions := self.Solutions()
	if len(solutions) == 0 {
		return false
	}
	self.Load(solutions[0].DataString())
	return true
}

//NumSolutions returns the total number of solutions found in the grid when it is solved forward
//from this point. A valid Sudoku puzzle has only one solution. Does not mutate the grid.
func (self *Grid) NumSolutions() int {
	return len(self.Solutions())
}

//HasSolution returns true if the grid has at least one solution. Does not mutate the grid.
func (self *Grid) HasSolution() bool {
	//TODO: optimize this to bail as soon as we find a single solution.
	return len(self.nOrFewerSolutions(1)) > 0
}

//HasMultipleSolutions returns true if the grid has more than one solution. Does not mutate the grid.
func (self *Grid) HasMultipleSolutions() bool {
	return len(self.nOrFewerSolutions(2)) >= 2
}

//Solutions returns a slice of grids that represent possible solutions if you were to solve forward this grid. The current grid is not modified.
//If there are no solutions forward from this location it will return a slice with len() 0. Does not mutate the grid.
func (self *Grid) Solutions() (solutions []*Grid) {
	return self.nOrFewerSolutions(0)
}

//The actual workhorse of solutions generating. 0 means "as many as you can find". It might return more than you asked for, if it already had more results than requested sitting around.
func (self *Grid) nOrFewerSolutions(max int) []*Grid {

	self.cachedSolutionsLock.RLock()
	hasNoCachedSolutions := self.cachedSolutions == nil
	cachedSolutionsLen := self.cachedSolutionsRequestedLength
	self.cachedSolutionsLock.RUnlock()

	if hasNoCachedSolutions || (max == 0 && cachedSolutionsLen != 0) || (max > 0 && cachedSolutionsLen < max && cachedSolutionsLen > 0) {

		queueDone := make(chan bool, 1)

		queue := newSyncedFiniteQueue(0, DIM*DIM, queueDone)

		queue.In <- self.Copy()

		//In the past this wasn't buffered, but then when we finished early other items would try to go into it
		//and block, which prevented them from looping back up and getting the signal to shut down.
		//Since there's only a known number of threads, we'll make sure they all ahve a place to leave their work
		//without blocking so they can get the signal to shut down.
		incomingSolutions := make(chan *Grid, _NUM_SOLVER_THREADS)

		//Using a pattern for closing fan in style receivers from http://blog.golang.org/pipelines
		var wg sync.WaitGroup

		var solutions []*Grid

		//TODO: figure out a way to kill all of these threads when necessary.

		for i := 0; i < _NUM_SOLVER_THREADS; i++ {
{'fraction_non_alphanumeric': 0.08161421472669779, 'fraction_numerical': 0.0045173919590423125, 'mean_word_length': 3.9864864864864864, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '270791', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1884, 'n_words': 927}