-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoxUniquenessRule.cs
62 lines (55 loc) · 2.1 KB
/
BoxUniquenessRule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Diagnostics;
namespace SudokuSpice.RuleBased.Rules
{
/// <summary>
/// Restricts that each 'box' contains all unique values.
/// </summary>
public class BoxUniquenessRule : UniquenessRule, IMissingBoxValuesTracker
{
private int _boxSize;
private IReadOnlyPuzzle? _puzzle;
public BoxUniquenessRule() : base() { }
private BoxUniquenessRule(BoxUniquenessRule existing, IReadOnlyPuzzle? puzzle)
: base(existing)
{
_boxSize = existing._boxSize;
_puzzle = puzzle;
}
/// <inheritdoc/>
public override IRule CopyWithNewReference(IReadOnlyPuzzle? puzzle)
{
return new BoxUniquenessRule(this, puzzle);
}
/// <inheritdoc/>
public override bool TryInit(IReadOnlyPuzzle puzzle, BitVector uniquePossibleValues)
{
_boxSize = Boxes.IntSquareRoot(puzzle.Size);
_puzzle = puzzle;
if (!base.TryInit(puzzle, uniquePossibleValues))
{
_puzzle = null;
return false;
}
return true;
}
/// <inheritdoc/>
public BitVector GetMissingValuesForBox(int box) => GetPossibleValues(box);
/// <inheritdoc/>
protected override int GetNumDimensions(IReadOnlyPuzzle puzzle) => puzzle.Size;
/// <inheritdoc/>
protected override int GetDimension(in Coordinate c) => Boxes.CalculateBoxIndex(in c, _boxSize);
/// <inheritdoc/>
protected override void TrackUnsetCoordinatesOnSameDimension(int dimension, in Coordinate source, CoordinateTracker tracker)
{
Debug.Assert(_puzzle is not null, $"Can't call {nameof(Update)} with a null puzzle.");
foreach (Coordinate unsetCoord in Boxes.YieldUnsetCoordsForBox(dimension, _boxSize, _puzzle!))
{
if (source.Column == unsetCoord.Column && source.Row == unsetCoord.Row)
{
continue;
}
tracker.AddOrTrackIfUntracked(unsetCoord);
}
}
}
}