-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
74 lines (69 loc) · 1.47 KB
/
game.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
package main
func permutations(state []uint8, rank int, candidates []candidate) []candidate {
if rank < 0 {
candidates = append(candidates, candidate{state[3], state[2], state[1], state[0]})
} else {
var digit uint8 = 0
if rank == len(state)-1 {
digit = 1
}
for digit < 10 {
var shouldSkip = false
for i := len(state) - 1; i > rank; i-- {
if state[i] == digit {
shouldSkip = true
break
}
}
if !shouldSkip {
state[rank] = digit
candidates = permutations(state, rank-1, candidates)
}
digit += 1
}
}
return candidates
}
func filterNonMatchingCandidates(candidates []candidate, query candidate, answer answer) []candidate {
newCandidates := make([]candidate, 0, cap(candidates))
for _, c := range candidates {
if c.Compare(query) == answer {
newCandidates = append(newCandidates, c)
}
}
return newCandidates
}
func (self candidate) IsValid() bool {
if self[0] == 0 {
return false
}
for i := 0; i < len(self)-1; i++ {
for j := i + 1; j < len(self); j++ {
if self[i] == self[j] {
return false
}
}
}
return true
}
func (self candidate) Compare(other candidate) answer {
answer := answer{}
for i, selfDigit := range self {
for j, otherDigit := range other {
if selfDigit == otherDigit {
if i == j {
answer.bulls++
} else {
answer.cows++
}
}
}
}
return answer
}
func (self candidate) Show() string {
for i := 0; i < len(self); i++ {
self[i] += 0x30
}
return string(self[:])
}