-
Notifications
You must be signed in to change notification settings - Fork 0
/
zebra_constraint_next_to.go
65 lines (55 loc) · 2 KB
/
zebra_constraint_next_to.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
package zebra
import (
"math"
)
type nextToConstraint struct {
a, b domainer
}
type nextToConstraintData struct {
aIdx, bIdx, aHouseNumIdx, bHouseNumIdx int
indexesFound bool
}
func (ntc nextToConstraint) dataFrom(assignment ...uint8) nextToConstraintData {
aIdx, aErr := indexOf(ntc.a, assignment...)
bIdx, bErr := indexOf(ntc.b, assignment...)
indexesFound := aErr == nil && bErr == nil
nIdx, mIdx := otherIdx(int(aIdx), ntc.a, number(0)), otherIdx(int(bIdx), ntc.b, number(0))
return nextToConstraintData{aIdx: int(aIdx), bIdx: int(bIdx), indexesFound: indexesFound, aHouseNumIdx: nIdx, bHouseNumIdx: mIdx}
}
func (ntc nextToConstraint) forApplicability() constraint {
return func(assignment ...uint8) bool {
data := ntc.dataFrom(assignment...)
return data.indexesFound && assignment[data.aHouseNumIdx] != 0 && assignment[data.bHouseNumIdx] != 0
}
}
func (ntc nextToConstraint) forSatisfaction() constraint {
return func(assignment ...uint8) bool {
data := ntc.dataFrom(assignment...)
return math.Abs(float64(assignment[data.aHouseNumIdx])-float64(assignment[data.bHouseNumIdx])) == 1 &&
(assignment[data.aIdx]%5 != assignment[data.bIdx]%5)
}
}
func (ntc nextToConstraint) forMissingIndex(assignment []uint8) (uint8, bool) {
data := ntc.dataFrom(assignment...)
switch {
case !data.indexesFound:
return 0, false
case assignment[data.aHouseNumIdx] == 0:
return uint8(data.aHouseNumIdx), true
case assignment[data.bHouseNumIdx] == 0:
return uint8(data.bHouseNumIdx), true
default:
return 0, false
}
}
type toTheRightOfConstraint nextToConstraint
func (ttroc toTheRightOfConstraint) forApplicability() constraint {
return nextToConstraint(ttroc).forApplicability()
}
func (ttroc toTheRightOfConstraint) forSatisfaction() constraint {
return func(assignment ...uint8) bool {
data := nextToConstraint(ttroc).dataFrom(assignment...)
return (assignment[data.aHouseNumIdx]-assignment[data.bHouseNumIdx] == 1) &&
(assignment[data.aIdx]%5 != assignment[data.bIdx]%5)
}
}