-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeople.go
More file actions
162 lines (131 loc) · 2.97 KB
/
Copy pathpeople.go
File metadata and controls
162 lines (131 loc) · 2.97 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
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
package main
type People struct {
People map[int]*Person `json:"people"` // (id -> Person)
MaxId int `json:"max_id"`
}
type Person struct {
Id int `json:"id"`
Name string `json:"name"`
Seen map[int]bool `json:"seen"`
Dropped bool `json:"dropped"`
}
type Pair struct {
first *Person
second *Person
triple bool
third *Person // Only valid if total is odd number
}
func (p *People) update_roster(roster []string) error {
new_people := []*Person{}
// Identify all the new people
for _, name := range roster {
seen := false
for _, person := range p.People {
if person.Name == name {
seen = true
break
}
}
// New person in the roster
if !seen {
new_people = append(new_people, &Person{
Id: p.MaxId + 1,
Name: name,
Seen: map[int]bool{},
})
p.MaxId++
}
}
// Identify all dropped individual
for _, person := range p.People {
seen := false
for _, name := range roster {
if person.Name == name {
seen = true
break
}
}
if !seen {
p.People[person.Id].Dropped = true
}
}
// Add all new people into list
for _, person := range new_people {
p.People[person.Id] = person
}
return nil
}
func (p *People) mark_family(families [][]string) error {
// Convert names into family of ids
familiesIds := [][]int{}
for _, family := range families {
familyId := []int{}
for _, name := range family {
if name == "" {
continue
}
// Find the corresponding name
for _, person := range p.People {
if person.Name == name {
familyId = append(familyId, person.Id)
break
}
}
}
familiesIds = append(familiesIds, familyId)
}
// Mark everyone in each family as seen
for _, family := range familiesIds {
for _, id := range family {
person := p.People[id]
for _, new_id := range family {
if id != new_id {
person.Seen[new_id] = true
}
}
}
}
return nil
}
func (p *People) create_starting_state() (*State, error) {
people_id := []int{}
person_addr := make(map[int](*Person))
for _, person := range p.People {
if person.Dropped {
continue
}
people_id = append(people_id, person.Id)
person_addr[person.Id] = person
}
return &State{
avail_people_id: people_id,
matched_pairs: []*Pair{},
total_people: len(p.People),
}, nil
}
func (p *People) updateWithState(pairs []*Pair) (*People, error) {
// Iterate over all pairs
for _, pair := range pairs {
// Add each pair to seen
person_A := p.People[pair.first.Id]
person_B := p.People[pair.second.Id]
person_A.Seen[person_B.Id] = true
person_B.Seen[person_A.Id] = true
// Hand odd case
if pair.triple {
person_C := p.People[pair.third.Id]
person_A.Seen[person_C.Id] = true
person_B.Seen[person_C.Id] = true
person_C.Seen[person_A.Id] = true
person_C.Seen[person_B.Id] = true
}
}
ret := make(map[int]*Person)
for id, person := range p.People {
ret[id] = person
}
return &People{
People: ret,
MaxId: p.MaxId,
}, nil
}