-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
185 lines (158 loc) · 4.19 KB
/
Copy pathmain.go
File metadata and controls
185 lines (158 loc) · 4.19 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Preferences project main.go
package main
import (
"errors"
"fmt"
"sort"
)
// Stores preferences for each Role for a given player
// Trapper, Medic, Support, Assault, & Monster
// 1 is the most preferred, 5 is the least preferred
type RolePreference struct {
Trapper, Medic, Support, Assault, Monster int
Name string
}
type RoleAssignment struct {
Name, Role string
}
// Assign roles to a group of players
func AssignRoles(prefs []RolePreference) ([]RoleAssignment, int, error) {
if len(prefs) != 5 {
return nil, 25, errors.New("Need 5 players to assign roles")
}
// We have 5 players, refer to them by their slice index
// p1 = prefs[0] etc...
bestWeight := 25 // worst case, everyone gets least favorite
bestSolutions := make([][]int, 1)
for current := range GeneratePermutations([]int{1, 2, 3, 4, 5}) {
currentWeight, _ := GetWeight(current, prefs)
if currentWeight <= bestWeight {
if currentWeight < bestWeight {
bestWeight = currentWeight
bestSolutions = nil
}
sliceCopy := make([]int, len(current))
copy(sliceCopy, current)
bestSolutions = append(bestSolutions, sliceCopy)
}
}
// for now, just take the first, best solution
if len(bestSolutions) == 0 {
return nil, 25, errors.New("Could not find any valid solutions")
}
bestSolution := bestSolutions[0]
assignments := make([]RoleAssignment, len(prefs))
for i, p := range prefs {
name, _ := GetRoleName(bestSolution[i])
assignments[i] = RoleAssignment{Name: p.Name, Role: name}
}
return assignments, bestWeight, nil
}
// Get the name associated with a role ID
func GetRoleName(i int) (string, error) {
// 1 - Trapper
// 2 - Medic
// 3 - Support
// 4 - Assault
// 5 - Monster
switch i {
case 1:
return "Trapper", nil
case 2:
return "Medic", nil
case 3:
return "Support", nil
case 4:
return "Assault", nil
case 5:
return "Monster", nil
}
return "", errors.New("Role not found")
}
// Get the preference weight for this role permutation
func GetWeight(current []int, prefs []RolePreference) (int, error) {
sum := 0
for i := 0; i < 5; i++ {
if w, e := GetRoleWeight(current[i], prefs[i]); e == nil {
sum += w
} else {
return 25, e
}
}
return sum, nil
}
// Get an individual player's preference for his given role
func GetRoleWeight(role int, playerPreferences RolePreference) (int, error) {
// 1 - Trapper
// 2 - Medic
// 3 - Support
// 4 - Assault
// 5 - Monster
switch role {
case 1:
return playerPreferences.Trapper, nil
case 2:
return playerPreferences.Medic, nil
case 3:
return playerPreferences.Support, nil
case 4:
return playerPreferences.Assault, nil
case 5:
return playerPreferences.Monster, nil
}
return -1, errors.New("Role not found")
}
// Return every permutation of integers in a slice
func GeneratePermutations(values []int) <-chan []int {
c := make(chan []int)
// Make sure our slice of integers is sorted
sort.Ints(values)
go func(c chan []int) {
defer close(c)
done := false
for !done {
// Return the next lexicographical permutation
retSlice := make([]int, len(values))
copy(retSlice, values)
c <- retSlice
// Find the rightmost value smaller than the one after it
i := len(values) - 2
for ; i >= 0; i-- {
if values[i] < values[i+1] {
break
}
}
// If we managed to get to -1, values are reverse sorted and we
// have returned all permutations
if i == -1 {
done = true
} else {
// find the smallest number greater than values[i] but still
// after it in the slice (ceil)
j, ceil := i+1, i+1
for ; j < len(values); j++ {
if values[j] > values[i] && values[j] < values[ceil] {
ceil = j
}
}
// and swap them
values[i], values[ceil] = values[ceil], values[i]
// then sort the new subslice
sort.Ints(values[i+1 : len(values)])
}
}
}(c)
return c
}
func main() {
sample := []RolePreference{
RolePreference{1, 2, 3, 4, 5, "Vincent"},
RolePreference{5, 4, 3, 2, 1, "Edgar"},
RolePreference{1, 5, 2, 4, 3, "Lenn0s"},
RolePreference{1, 2, 3, 4, 5, "Perlmonger42"},
RolePreference{1, 4, 2, 3, 5, "Walrus"},
}
result, weight, _ := AssignRoles(sample)
fmt.Println(result)
fmt.Printf("Weight: %d\n", weight)
}