-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
47 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
SudokuSolver.Tests/Solvers/BacktrackSolvers/Pruners/HiddenTripplePrunerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using SudokuSolver.Models; | ||
using SudokuSolver.Solvers.BacktrackSolvers.Pruners; | ||
using SudokuSolver.Solvers.Preprocessors; | ||
|
||
namespace SudokuSolver.Tests.Solvers.BacktrackSolvers.Pruners | ||
{ | ||
[TestClass] | ||
public class HiddenTripplePrunerTests | ||
{ | ||
[TestMethod] | ||
[DataRow("000001030231090000065003100678924300103050006000136700009360570006019843300000000", 15)] | ||
public void Can_PruneCorrectly(string board, int expectedChange) | ||
{ | ||
// ARRANGE | ||
var values = new List<byte>(); | ||
foreach (var c in board) | ||
values.Add(byte.Parse($"{c}")); | ||
var context = Preprocessor.Preprocess(new SudokuBoard(values.ToArray())); | ||
IPruner pruner1 = new HiddenTripplePruner(); | ||
var preCount = context.Cardinalities.Sum(x => x.Possibilities); | ||
|
||
// ACT | ||
while (pruner1.Prune(context)) { } | ||
context.Cardinalities = Preprocessor.GenerateCardinalities(context.Board, context.Candidates); | ||
|
||
// ASSERT | ||
Assert.AreEqual(expectedChange, preCount - context.Cardinalities.Sum(x => x.Possibilities)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
SudokuSolver/Solvers/BacktrackSolvers/Pruners/HiddenTripplePruner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using SudokuSolver.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SudokuSolver.Solvers.BacktrackSolvers.Pruners | ||
{ | ||
public class HiddenTripplePruner : BasePruner | ||
{ | ||
public override bool Prune(SearchContext context) | ||
{ | ||
var pruned = 0; | ||
|
||
for (byte row = 0; row < SudokuBoard.BoardSize; row++) | ||
{ | ||
var candidates = GetAssignmentsFromRow(context, row); | ||
var removed = 1; | ||
while (removed > 0) | ||
{ | ||
removed = 0; | ||
for (int i = 1; i <= SudokuBoard.BoardSize; i++) | ||
if (candidates.Where(x => x.Value == i).Count() == 1 || candidates.Where(x => x.Value == i).Count() > 3) | ||
removed += candidates.RemoveAll(x => x.Value == i); | ||
for (int i = 1; i <= SudokuBoard.BoardSize; i++) | ||
if (candidates.Where(x => x.Value == i).Any(y => !candidates.Any(z => z != y && z.X == y.X))) | ||
removed += candidates.RemoveAll(x => x.Value == i); | ||
} | ||
|
||
if (candidates.DistinctBy(x => x.Value).Count() == 3 && candidates.DistinctBy(x => x.X).Count() == 3) | ||
foreach (var candidate in candidates) | ||
pruned += context.Candidates[candidate.X, candidate.Y].RemoveAll(x => !candidates.Contains(x)); | ||
} | ||
|
||
for (byte column = 0; column < SudokuBoard.BoardSize; column++) | ||
{ | ||
var candidates = GetAssignmentsFromColumn(context, column); | ||
var removed = 1; | ||
while(removed > 0) | ||
{ | ||
removed = 0; | ||
for (int i = 1; i <= SudokuBoard.BoardSize; i++) | ||
if (candidates.Where(x => x.Value == i).Count() == 1 || candidates.Where(x => x.Value == i).Count() > 3) | ||
removed += candidates.RemoveAll(x => x.Value == i); | ||
for (int i = 1; i <= SudokuBoard.BoardSize; i++) | ||
if (candidates.Where(x => x.Value == i).Any(y => !candidates.Any(z => z != y && z.Y == y.Y))) | ||
removed += candidates.RemoveAll(x => x.Value == i); | ||
} | ||
|
||
if (candidates.DistinctBy(x => x.Value).Count() == 3 && candidates.DistinctBy(x => x.Y).Count() == 3) | ||
foreach (var candidate in candidates) | ||
pruned += context.Candidates[candidate.X, candidate.Y].RemoveAll(x => !candidates.Contains(x)); | ||
} | ||
|
||
if (pruned > 0) | ||
Console.WriteLine($"Removed {pruned} candidates because of hidden tripples"); | ||
return pruned > 0; | ||
} | ||
|
||
private List<CellAssignment> LegalTripple(List<CellAssignment> assignments) | ||
{ | ||
if (assignments.DistinctBy(x => x.Value).Count() != 3) | ||
return new List<CellAssignment>(); | ||
|
||
for(int i = 1; i <= SudokuBoard.BoardSize; i++) | ||
{ | ||
if (assignments.Any(x => x.Value == i) && assignments.Where(x => x.Value == i).Count() < 2) | ||
return new List<CellAssignment>(); | ||
} | ||
|
||
return assignments; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters