private void ReduceRegion(int skip, SudokuRegion region, SudokuState state, ICollection <IEvent> events) { var quad = 0u; buffer.Clear(); foreach (var index in region.Skip(skip)) { var value = state[index]; if (SudokuCell.Count(value) < 4) { quad |= value; // Joined the represent more then 3 values. if (SudokuCell.Count(quad) > 4 || buffer.Count > 3) { return; } buffer.Add(index); } } if (buffer.Count == 4) { Fetch(quad, buffer, region, state, events); } }
/// <summary>Reduced a cell by excluding the options of the other.</summary> public ReduceResult AndMask(int index, uint mask) { unchecked { var val = m_Values[index]; var nw = val & mask; m_Values[index] = nw; if (nw == SudokuPuzzle.Invalid) { return(ReduceResult.Inconsistent); } if (SudokuCell.Count(nw) == 1 && SudokuCell.Count(val) != 1) { Unknowns--; if (IsSolved) { return(ReduceResult.Solved | ReduceResult.Reduced | ReduceResult.Found); } return(ReduceResult.Reduced | ReduceResult.Found); } return(val != nw ? ReduceResult.Reduced : ReduceResult.None); } }
private void ReduceRegion(SudokuRegion region, SudokuState state, ICollection <IEvent> events) { var nakedPair = 0u; var count = 0; foreach (var index in region) { var value = state[index]; // nothing found yet. if (nakedPair == default) { if (SudokuCell.Count(value) == 2) { nakedPair = value; count++; } } // Equal to the first (potential) naked pair. else if (value == nakedPair && count++ > 2) { throw new InvalidPuzzleException(); } } if (count > 1) { Fetch(nakedPair, region, state, events); } }
public ReduceHiddenPairs() { for (var f = 1; f <= 8; f++) { for (var s = f + 1; s <= 9; s++) { Pairs.Add(SudokuCell.Single(f) | SudokuCell.Single(s)); } } }
/// <summary>Reduced a cell by excluding the options of the other.</summary> public IEvent And <TSolver>(int index, uint mask) { unchecked { var val = m_Values[index]; var nw = val & mask; m_Values[index] = nw; if (nw == SudokuPuzzle.Invalid) { throw new InvalidPuzzleException(); } if (SudokuCell.Count(nw) == 1 && SudokuCell.Count(val) != 1) { return(ValueFound.Ctor <TSolver>(index, nw)); } return(val != nw ? ReducedOption.Instance : (IEvent)NoReduction.Instance); } }
/// <summary>Gets the number of optional values for a given index of the Sudoku state.</summary> public int Count(int index) => SudokuCell.Count(m_Values[index]);