-
Notifications
You must be signed in to change notification settings - Fork 1
/
StandardRules.cs
170 lines (157 loc) · 6.46 KB
/
StandardRules.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Diagnostics;
namespace SudokuSpice.RuleBased.Rules
{
/// <summary>
/// Combines the standard rules (row, column, and box uniqueness) for efficiency and
/// convenience.
/// </summary>
public class StandardRules : IRule, IMissingRowValuesTracker, IMissingColumnValuesTracker, IMissingBoxValuesTracker
{
private int _boxSize;
private BitVector[]? _unsetRowValues;
private BitVector[]? _unsetColValues;
private BitVector[]? _unsetBoxValues;
private IReadOnlyPuzzle? _puzzle;
/// <summary>
/// Constructs a single rule that enforces all the standard rules. This instance can be
/// reused to solve multiple puzzles with the same <paramref name="size"/> and
/// <paramref name="allUniqueValues"/>.
/// </summary>
/// <param name="allUniqueValues">
/// The unique values to satisfy for each row, column, and box (eg. 1-9 for a standard
/// puzzle).
/// </param>
///
public StandardRules() { }
private StandardRules(StandardRules existing, IReadOnlyPuzzle? puzzle)
{
_boxSize = existing._boxSize;
_unsetRowValues = existing._unsetRowValues?.AsSpan().ToArray();
_unsetColValues = existing._unsetColValues?.AsSpan().ToArray();
_unsetBoxValues = existing._unsetBoxValues?.AsSpan().ToArray();
_puzzle = puzzle;
}
/// <inheritdoc/>
public IRule CopyWithNewReference(IReadOnlyPuzzle? puzzle)
{
return new StandardRules(this, puzzle);
}
/// <inheritdoc/>
public bool TryInit(IReadOnlyPuzzle puzzle, BitVector uniquePossibleValues)
{
int size = puzzle.Size;
_boxSize = Boxes.IntSquareRoot(size);
if (size != _unsetRowValues?.Length)
{
_unsetRowValues = new BitVector[size];
_unsetRowValues.AsSpan().Fill(uniquePossibleValues);
_unsetColValues = _unsetRowValues.AsSpan().ToArray();
_unsetBoxValues = _unsetRowValues.AsSpan().ToArray();
} else
{
_unsetRowValues.AsSpan().Fill(uniquePossibleValues);
_unsetColValues.AsSpan().Fill(uniquePossibleValues);
_unsetBoxValues.AsSpan().Fill(uniquePossibleValues);
}
int boxIdx = 0;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
if (col == 0)
{
boxIdx = (row / _boxSize) * _boxSize;
} else if (col % _boxSize == 0)
{
boxIdx++;
}
int? val = puzzle[row, col];
if (!val.HasValue)
{
continue;
}
if (!_unsetRowValues[row].IsBitSet(val.Value)
|| !_unsetColValues![col].IsBitSet(val.Value)
|| !_unsetBoxValues![boxIdx].IsBitSet(val.Value))
{
return false;
}
_unsetRowValues[row].UnsetBit(val.Value);
_unsetColValues[col].UnsetBit(val.Value);
_unsetBoxValues[boxIdx].UnsetBit(val.Value);
}
}
_puzzle = puzzle;
return true;
}
/// <inheritdoc/>
public BitVector GetMissingValuesForRow(int row) => _unsetRowValues![row];
/// <inheritdoc/>
public BitVector GetMissingValuesForColumn(int column) => _unsetColValues![column];
/// <inheritdoc/>
public BitVector GetMissingValuesForBox(int box) => _unsetBoxValues![box];
/// <inheritdoc/>
public BitVector GetPossibleValues(in Coordinate c)
{
Debug.Assert(_puzzle is not null, $"Can't call {nameof(GetPossibleValues)} with a null puzzle.");
return BitVector.FindIntersect(
_unsetRowValues![c.Row],
BitVector.FindIntersect(
_unsetColValues![c.Column],
_unsetBoxValues![Boxes.CalculateBoxIndex(in c, _boxSize)]));
}
/// <inheritdoc/>
public void Revert(in Coordinate c, int val)
{
_unsetRowValues![c.Row].SetBit(val);
_unsetColValues![c.Column].SetBit(val);
_unsetBoxValues![Boxes.CalculateBoxIndex(in c, _boxSize)].SetBit(val);
}
/// <inheritdoc/>
public void Revert(in Coordinate c, int val, CoordinateTracker coordTracker)
{
_unsetRowValues![c.Row].SetBit(val);
_unsetColValues![c.Column].SetBit(val);
int boxIdx = Boxes.CalculateBoxIndex(in c, _boxSize);
_unsetBoxValues![boxIdx].SetBit(val);
_AddUnset(in c, boxIdx, coordTracker);
}
/// <inheritdoc/>
public void Update(in Coordinate c, int val, CoordinateTracker coordTracker)
{
_unsetRowValues![c.Row].UnsetBit(val);
_unsetColValues![c.Column].UnsetBit(val);
int boxIdx = Boxes.CalculateBoxIndex(in c, _boxSize);
_unsetBoxValues![boxIdx].UnsetBit(val);
_AddUnset(in c, boxIdx, coordTracker);
}
private void _AddUnset(in Coordinate c, int boxIdx, CoordinateTracker coordTracker)
{
Debug.Assert(_puzzle is not null, $"Can't call {nameof(_AddUnset)} with a null puzzle.");
int size = _puzzle.Size;
for (int col = 0; col < size; col++)
{
if (col != c.Column && !_puzzle[c.Row, col].HasValue)
{
coordTracker.AddOrTrackIfUntracked(new Coordinate(c.Row, col));
}
}
for (int row = 0; row < size; row++)
{
if (row != c.Row && !_puzzle[row, c.Column].HasValue)
{
coordTracker.AddOrTrackIfUntracked(new Coordinate(row, c.Column));
}
}
foreach (Coordinate inBoxCoord in Boxes.YieldUnsetCoordsForBox(boxIdx, _boxSize, _puzzle))
{
if (inBoxCoord.Row == c.Row || inBoxCoord.Column == c.Column)
{
continue;
}
coordTracker.AddOrTrackIfUntracked(inBoxCoord);
}
}
}
}