-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
119 lines (94 loc) · 2.41 KB
/
Copy pathstate.go
File metadata and controls
119 lines (94 loc) · 2.41 KB
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
package main
import (
"math/rand"
"time"
)
type State struct {
avail_people_id []int
matched_pairs []*Pair
total_people int
}
func (s *State) valid_pair(pair *Pair) bool {
person_A := pair.first
person_B := pair.second
if person_A.Dropped || person_B.Dropped {
return false
}
if _, ok := person_A.Seen[person_B.Id]; ok {
return false
}
if _, ok := person_B.Seen[person_A.Id]; ok {
return false
}
return true
}
func (s *State) valid_triple(pair *Pair, person_C *Person) bool {
person_A := pair.first
person_B := pair.second
if person_C.Dropped {
return false
}
if _, ok := person_A.Seen[person_C.Id]; ok {
return false
}
if _, ok := person_B.Seen[person_C.Id]; ok {
return false
}
return true
}
func (s *State) get_successors(people *People) ([]*State, error) {
childrenState := []*State{}
// Handle odd numder of people case
if len(s.avail_people_id) == 1 {
person_C := people.People[s.avail_people_id[0]]
for _, pair := range s.matched_pairs {
if !s.valid_triple(pair, person_C) {
continue
}
// Add triple into pair
pair.third = person_C
pair.triple = true
newState := &State{
avail_people_id: []int{},
matched_pairs: s.matched_pairs,
total_people: s.total_people,
}
return append(childrenState, newState), nil
}
return childrenState, nil
}
// Shuffle the order of available people
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(s.avail_people_id), func(i, j int) {
s.avail_people_id[i], s.avail_people_id[j] = s.avail_people_id[j], s.avail_people_id[i]
})
for i := 0; i < len(s.avail_people_id); i++ {
for j := i + 1; j < len(s.avail_people_id); j++ {
person_A := people.People[s.avail_people_id[i]]
person_B := people.People[s.avail_people_id[j]]
pair := &Pair{
first: person_A,
second: person_B,
}
// Verify pair has not seen each other before
if !s.valid_pair(pair) {
continue
}
// Create new state
var remainder_people_id []int = []int{}
for _, id := range s.avail_people_id {
if id != person_A.Id && id != person_B.Id {
remainder_people_id = append(remainder_people_id, id)
}
}
newState := &State{
avail_people_id: remainder_people_id,
matched_pairs: append(s.matched_pairs, pair),
total_people: s.total_people,
}
// Append to possible childrenState
childrenState = append(childrenState, newState)
}
}
return childrenState, nil
}