-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwogame.cpp
280 lines (239 loc) · 7.48 KB
/
twogame.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include "twogame.hpp"
#include <map>
#include <math.h>
using namespace std;
long fac(long n) {
long result = n;
for (n--; n > 1; n--) {
result *= n;
}
return result;
}
long bico(long n, long k) {
long result = k == 1 ? n : fac(n)/(fac(k)*(fac(n-k)));
return result;
}
Player::Player(string n, const char *t, double s, int c) {
this->name = n;
this->team = t;
this->score = s;
this->cost = c;
}
const int POS_QB = 0;
const int POS_RB = 1;
const int POS_WR = 2;
const int POS_TE = 3;
const int POS_K = 4;
const int POS_D = 5;
Team::Team(int size) {
this->size = size;
this->players = (Player **)malloc(sizeof(Player *) * size);
}
double Team::score() {
double score = 0;
for (int ii = 0; ii < this->size; ii++) {
score += this->players[ii]->score;
}
return score;
}
int Team::cost() {
int cost = 0;
for (int ii = 0; ii < this->size; ii++) {
cost += this->players[ii]->cost;
}
return cost;
}
bool Team::hasPlayer(Player *p) {
for (int ii = 0; ii < this->size; ii++) {
if (p == this->players[ii]) {
return true;
}
}
return false;
}
bool Team::isValid(int budget, int maxperteam) {
if (this->cost() > budget) {
return false;
}
// this is a really slow bottle neck, but it happens rare enough
map<const char *, int> teamCounts;
for (int ii = 0; ii < this->size; ii++) {
Player *p = this->players[ii];
if (teamCounts.find(p->team) == teamCounts.end()) {
teamCounts[p->team] = 1;
} else {
teamCounts[p->team]++;
}
}
return true;
}
Team *Team::dup() {
Team *t = new Team(this->size);
for (int ii = 0; ii < this->size; ii++) {
t->players[ii] = this->players[ii];
}
return t;
}
bool Team::isEqual(Team *team) {
if (team->size != this->size) {
return false;
}
// so lazy... should probably sort or something first
for (int ii = 0; ii < team->size; ii++) {
bool tf = false;
for (int jj = 0; jj < this->size; jj++) {
if (team->players[ii] == this->players[jj]) {
tf = true;
break;
}
}
if (!tf) {
return false;
}
}
return true;
}
TeamMaker::TeamMaker(int *positions, int numpos, int budget, int maxperteam, vector<string> *playingTeams) {
this->positions = positions;
this->numpos = numpos;
this->budget = budget;
this->maxperteam = maxperteam;
this->maxTeams = new vector<Team *>();
this->playingTeams = playingTeams;
this->players = new vector<vector<Player *> >(6);
this->filterPosition(&qb, POS_QB);
this->filterPosition(&rb, POS_RB);
this->filterPosition(&wr, POS_WR);
this->filterPosition(&te, POS_TE);
this->filterPosition(&k, POS_K);
this->filterPosition(&d, POS_D);
this->printConsiderations();
}
void TeamMaker::filterPosition(vector<Player *> *posplayers, int position) {
for (int pp = 0; pp < posplayers->size(); pp++) {
Player *p = posplayers->at(pp);
if (find(this->playingTeams->begin(), this->playingTeams->end(), p->team) != this->playingTeams->end()) {
vector<Player *> *curplayers = &this->players->at(position);
curplayers->push_back(posplayers->at(pp));
}
}
}
void TeamMaker::printConsiderations() {
int posCount[6] = {0,0,0,0,0,0};
long total = 1;
for (int pp = 0; pp < numpos; pp++) {
int pos = this->positions[pp];
posCount[pos]++;
}
for (int pos = 0; pos < 6; pos++) {
long count = posCount[pos];
long size = (&this->players->at(pos))->size();
if (count) {
total *= fac(size)/fac(size-count);
}
}
printf("TeamMaker Considerations\n");
printf(" -- Teams: %lu\n", this->playingTeams->size());
printf(" -- Positions: %d\n", this->numpos);
printf(" -- Combinations: %lu\n", total);
printf(" -- Quarterbacks: %lu\n", (&this->players->at(POS_QB))->size());
printf(" -- Running Backs: %lu\n", (&this->players->at(POS_RB))->size());
printf(" -- Wide Receivers: %lu\n", (&this->players->at(POS_WR))->size());
printf(" -- Tight Ends: %lu\n", (&this->players->at(POS_TE))->size());
printf(" -- Kickers: %lu\n", (&this->players->at(POS_K))->size());
printf(" -- Defenses: %lu\n", (&this->players->at(POS_D))->size());
printf("\n\n");
}
pair<double, vector<Team *>* > TeamMaker::findTeams() {
Team *t = new Team(this->numpos);
this->findMaxTeams(0, t);
printf("Counted %d teams\n", this->tc);
this->dedup();
return make_pair(this->max, this->maxTeams);
}
void TeamMaker::dedup() {
// again, really lazy here
Team *t1, *t2;
for (int ii = 0; ii < this->maxTeams->size()-1; ii++) {
t1 = this->maxTeams->at(ii);
for (int jj = 1; jj < this->maxTeams->size(); jj++) {
t2 = this->maxTeams->at(jj);
if (t1->isEqual(t2)) {
this->maxTeams->erase(this->maxTeams->begin()+jj);
jj--;
}
}
}
}
void TeamMaker::findMaxTeams(int posIndex, Team *team) {
int position = this->positions[posIndex];
vector<Player *> *posPlayers = &this->players->at(position);
int posSize = posPlayers->size();
for (int pidx = 0; pidx < posSize; ++pidx) {
Player *nextPlayer = posPlayers->at(pidx);
if (team->hasPlayer(nextPlayer)) {
continue;
}
team->players[posIndex] = nextPlayer;
if (posIndex == this->numpos-1) {
this->tc++;
if (this->tc % 10000000 == 0) {
printf("Counted %d teams\n", this->tc);
}
double score = team->score();
if (score >= this->max) {
if (!team->isValid(this->budget, this->maxperteam)) {
team->players[posIndex] = NULL;
continue;
}
if (score > this->max) {
this->maxTeams->clear();
this->max = score;
}
this->maxTeams->push_back(team->dup());
}
} else {
this->findMaxTeams(posIndex+1, team);
}
team->players[posIndex] = NULL;
}
}
int main(int argc, char **argv) {
int numpos = 9;
int *pos = (int *)malloc(sizeof(int) * numpos);
pos[0] = POS_QB;
pos[1] = POS_RB;
pos[2] = POS_RB;
pos[3] = POS_WR;
pos[4] = POS_WR;
pos[5] = POS_WR;
pos[6] = POS_TE;
pos[7] = POS_K;
pos[8] = POS_D;
vector<string> *playingTeams = new vector<string>();
playingTeams->push_back("NYG");
playingTeams->push_back("PHI");
playingTeams->push_back("SF");
playingTeams->push_back("STL");
TeamMaker *tm = new TeamMaker(pos, numpos, 60000, 4, playingTeams);
pair<double, vector<Team *> *> res = tm->findTeams();
double curmax = res.first;
vector<Team *> *teams = res.second;
for (int tt = 0; tt < teams->size(); tt++) {
Team *team = teams->at(tt);
printf("Team %d -- Expected Score: %.2f\n", tt+1, team->score());
for (int pp = 0; pp < team->size; pp++) {
Player *player = team->players[pp];
cout << " -- " << player->name
<< " (" << player->team << ") "
<< player->score << " "
<< "$" << player->cost
<< endl;
}
printf("\n");
}
return 0;
}