-
Notifications
You must be signed in to change notification settings - Fork 0
/
wing.go
386 lines (354 loc) · 9.37 KB
/
wing.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package solver
import (
"fmt"
"github.com/RoaringBitmap/roaring"
)
type XYWing struct {
Pivot *Cell
Wings []*Cell
}
func (xy *XYWing) Union() *roaring.Bitmap {
return ParUnionCells(xy.Triplet())
}
func (xy *XYWing) Eliminate(b *Board) error {
union := xy.Union()
union.AndNot(xy.Pivot.Marks)
marks := union.Clone()
for _, cell := range xy.WingsIntersect(b) {
cell.Marks.AndNot(marks)
if cell.Marks.IsEmpty() {
return fmt.Errorf("Invalid Board XY: Empty marks: Cell: %+v\n", cell)
}
}
return nil
}
func (xy *XYWing) Triplet() []*Cell {
triplet := make([]*Cell, 0, 3)
triplet = append(triplet, xy.Pivot)
for _, wing := range xy.Wings {
triplet = append(triplet, wing)
}
return triplet
}
func (xy *XYWing) WingsIntersect(b *Board) []*Cell {
intersect := make([]*Cell, 0)
aUnit := xy.Wings[0].CellUnits(b)
bUnit := xy.Wings[1].CellUnits(b)
for _, unit := range aUnit {
for _, c := range unit {
if IsCellInCollections(c, bUnit) && c.ID != xy.Wings[0].ID && c.ID != xy.Wings[1].ID && xy.Pivot.ID != c.ID {
intersect = append(intersect, c)
}
}
}
return UnSolvedCells(intersect)
}
func (xy *XYWing) Print() {
fmt.Printf("\nXY Wing\n")
fmt.Printf("Pivot: %+v\n", xy.Pivot)
fmt.Printf("Wings\n")
for _, wing := range xy.Wings {
fmt.Printf("Wing: %+v\n", wing)
}
}
func EliminateXYWings(unsolved []*Cell, b *Board) error {
xyWings := make([]*XYWing, 0)
combinations := TripletCombinations(unsolved)
for _, triplet := range combinations {
if IsTripletHasSameCardinality(triplet, 2) && IsTripletUnionHasCardinality(triplet, 3) {
yes, xyWing := IsTripletXYWingCandidate(triplet, b)
if yes {
xyWings = append(xyWings, xyWing)
}
}
}
for _, xyWing := range xyWings {
if eliminateErr := xyWing.Eliminate(b); eliminateErr != nil {
return eliminateErr
}
}
return nil
}
type XYZWing struct {
Pivot *Cell
Wings []*Cell
}
func (xyz *XYZWing) Intersect() *roaring.Bitmap {
return ParIntersectCells(xyz.Triplet())
}
func (xyz *XYZWing) Triplet() []*Cell {
triplet := make([]*Cell, 0, 3)
triplet = append(triplet, xyz.Pivot)
for _, wing := range xyz.Wings {
triplet = append(triplet, wing)
}
return triplet
}
func (xyz *XYZWing) Eliminate(b *Board) error {
for _, cell := range xyz.XYZIntersect(b) {
cell.Marks.AndNot(xyz.Intersect())
if cell.Marks.IsEmpty() {
return fmt.Errorf("Invalid Board XYZ: Empty marks: Cell: %+v\n", cell)
}
}
return nil
}
func (xyz *XYZWing) XYZIntersect(b *Board) []*Cell {
intersect := make([]*Cell, 0)
aUnit := xyz.Wings[0].CellUnits(b)
bUnit := xyz.Wings[1].CellUnits(b)
cUnit := xyz.Pivot.CellUnits(b)
for _, unit := range aUnit {
for _, c := range unit {
if IsCellInCollections(c, bUnit) && IsCellInCollections(c, cUnit) && (c.ID != xyz.Wings[0].ID && c.ID != xyz.Wings[1].ID && c.ID != xyz.Pivot.ID) {
intersect = append(intersect, c)
}
}
}
return UnSolvedCells(intersect)
}
func (xyz *XYZWing) Print() {
fmt.Printf("XYZ Wing\n")
fmt.Printf("Pivot: %+v\n", xyz.Pivot)
fmt.Printf("Wings\n")
for _, wing := range xyz.Wings {
fmt.Printf("Wing: %+v\n", wing)
}
}
func EliminateXYZWings(unsolved []*Cell, b *Board) error {
xyzWings := make([]*XYZWing, 0)
combinations := TripletCombinations(unsolved)
for _, triplet := range combinations {
if IsTripletXYZWingBasedOnCardinality(triplet) && IsTripletUnionHasCardinality(triplet, 3) {
yes, xyzWing := IsTripletXYZWingCandidate(triplet, b)
if yes {
xyzWings = append(xyzWings, xyzWing)
}
}
}
for _, xyzWing := range xyzWings {
if eliminateErr := xyzWing.Eliminate(b); eliminateErr != nil {
return eliminateErr
}
}
return nil
}
func IsTripletXYZWingCandidate(triplet []*Cell, b *Board) (bool, *XYZWing) {
var xyzWing XYZWing
related := 0
unRelated := 0
pairs := PairCombinations(triplet)
for _, pair := range pairs {
if !IsPairRelated(pair, b) {
unRelated++
xyzWing.Wings = pair
} else {
related++
}
}
if related == 2 && unRelated == 1 {
for _, cell := range triplet {
if !IsCellInCollection(cell, xyzWing.Wings) {
xyzWing.Pivot = cell
}
}
// Pivot should have 3 elements and the intersection should be 1
if xyzWing.Pivot.Marks.GetCardinality() == 3 && xyzWing.Intersect().GetCardinality() == 1 {
return true, &xyzWing
}
}
return false, nil
}
func IsTripletXYWingCandidate(triplet []*Cell, b *Board) (bool, *XYWing) {
var xyWing XYWing
related := 0
unRelated := 0
pairs := PairCombinations(triplet)
for _, pair := range pairs {
if !IsPairRelated(pair, b) {
unRelated++
xyWing.Wings = pair
} else {
related++
}
}
if related == 2 && unRelated == 1 {
for _, cell := range triplet {
if !IsCellInCollection(cell, xyWing.Wings) {
xyWing.Pivot = cell
}
}
// Pivot - wings intersect should be 1 element set
aIntersect := ParIntersect(xyWing.Pivot.Marks, xyWing.Wings[0].Marks)
bIntersect := ParIntersect(xyWing.Pivot.Marks, xyWing.Wings[1].Marks)
if aIntersect.GetCardinality() == 1 && bIntersect.GetCardinality() == 1 {
return true, &xyWing
}
}
return false, nil
}
func IsPairRelated(pair []*Cell, board *Board) bool {
aUnits := pair[0].CellUnits(board)
for _, unit := range aUnits {
if IsCellInCollection(pair[1], unit) {
return true
}
}
return false
}
func IsTripletHasSameCardinality(triplet []*Cell, cardinality int) bool {
for _, cell := range triplet {
if cell.Marks.GetCardinality() != uint64(cardinality) {
return false
}
}
return true
}
func IsTripletXYZWingBasedOnCardinality(triplet []*Cell) bool {
// Two cells (wings) might have 2 cardinality while one of them has 3
cardinality2 := 0
cardinality3 := 0
for _, cell := range triplet {
if cell.Marks.GetCardinality() == 2 {
cardinality2++
}
if cell.Marks.GetCardinality() == 3 {
cardinality3++
}
}
if cardinality2 == 2 && cardinality3 == 1 {
return true
}
return false
}
func IsTripletUnionHasCardinality(triplet []*Cell, cardinality int) bool {
union := ParUnionCells(triplet)
return union.GetCardinality() == uint64(cardinality)
}
type XWing struct {
Up []*Cell
Down []*Cell
Mark *roaring.Bitmap
}
func (x *XWing) LeftCol(b *Board) ([]*Cell, error) {
if x.Up[0].Col != x.Down[0].Col {
return nil, fmt.Errorf("Inconsistency on LEFT col indexes: %+v\n", x)
}
return b.col(x.Up[0].Col), nil
}
func (x *XWing) RightCol(b *Board) ([]*Cell, error) {
if x.Up[1].Col != x.Down[1].Col {
return nil, fmt.Errorf("Inconsistency on RIGHT col indexes: %+v\n", x)
}
return b.col(x.Up[1].Col), nil
}
func (x *XWing) Eliminate(b *Board) error {
leftColumn, leftColErr := x.LeftCol(b)
if leftColErr != nil {
return leftColErr
}
for _, cell := range leftColumn {
if !cell.IsSolved() && cell.ID != x.Up[0].ID && cell.ID != x.Down[0].ID {
cell.Marks.AndNot(x.Mark)
if cell.Marks.IsEmpty() {
return fmt.Errorf("Invalid Board X (LEFT): Empty marks: Cell: %+v\n", cell)
}
}
}
rightColumn, rightColErr := x.RightCol(b)
if rightColErr != nil {
return rightColErr
}
for _, cell := range rightColumn {
if !cell.IsSolved() && cell.ID != x.Up[1].ID && cell.ID != x.Down[1].ID {
cell.Marks.AndNot(x.Mark)
if cell.Marks.IsEmpty() {
return fmt.Errorf("Invalid Board X (RIGHT): Empty marks: Cell: %+v\n", cell)
}
}
}
return nil
}
func (x *XWing) Print() {
fmt.Printf("\nX Wing\n")
fmt.Printf("Mark: %s\n", x.Mark.String())
fmt.Printf("Up Part\n")
for _, cell := range x.Up {
fmt.Printf("Cell [%d][%d] Marks: %s\n", cell.Row, cell.Col, cell.Marks.String())
}
fmt.Printf("Down Part\n")
for _, cell := range x.Down {
fmt.Printf("Cell [%d][%d] Marks: %s\n", cell.Row, cell.Col, cell.Marks.String())
}
}
func EliminateXWings(b *Board) error {
xWings := make([]*XWing, 0)
for i := 0; i < BoardSize; i++ {
row := b.data[i]
yes, cells, mark := HasXCandidates(row[:])
if yes {
y, xWing := SearchDownPart(cells, mark, i, b)
if y {
xWings = append(xWings, xWing)
}
}
}
for _, xWing := range xWings {
if eliminateErr := xWing.Eliminate(b); eliminateErr != nil {
return eliminateErr
}
}
return nil
}
func HasXCandidates(row []*Cell) (bool, []*Cell, *roaring.Bitmap) {
union := ParUnionCells(UnSolvedCells(row))
marks := BitmapSingles(union.ToArray())
for _, mark := range marks {
yes, cells := IsMarkAppearsTwiceInRow(mark, row)
if yes {
return true, cells, mark
}
}
return false, nil, nil
}
func IsMarkAppearsTwiceInRow(mark *roaring.Bitmap, row []*Cell) (bool, []*Cell) {
cells := make([]*Cell, 0)
for _, cell := range row {
if !cell.IsSolved() {
intersect := ParIntersect(mark, cell.Marks)
if intersect.GetCardinality() == 1 {
cells = append(cells, cell)
}
}
}
if len(cells) == 2 {
// Given mark appears only twice within the row
return true, cells
}
return false, nil
}
func SearchDownPart(upCells []*Cell, mark *roaring.Bitmap, rowIndex int, b *Board) (bool, *XWing) {
if rowIndex+1 == BoardSize-1 {
return false, nil
}
for i := rowIndex + 1; i < BoardSize; i++ {
row := b.data[i]
yes, downCells := IsMarkAppearsTwiceInRow(mark, row[:])
if yes {
// Well we found that same mark also only appears twice in this row.
// Let's also check whether the column indexes also match using sets
upIndexes := IndexesBitmap(upCells)
downIndexes := IndexesBitmap(downCells)
intersect := ParIntersect(upIndexes, downIndexes)
if intersect.GetCardinality() == 2 {
// Indexes also match perfectly
return true, &XWing{
Up: upCells,
Down: downCells,
Mark: mark,
}
}
}
}
return false, nil
}