aboutsummaryrefslogtreecommitdiffstats
path: root/teams.c
blob: 269096bde52441e740f37c91fd02bbc6414f0f88 (plain) (blame)
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
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

typedef struct Person {
  char *name;
  struct Person *opponent;
} Person;

static void
die(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

  if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
    fputc(' ', stderr);
    perror(NULL);
  } else
    fputc('\n', stderr);
  exit(1);
}

/**
 * @brief get number of substr in str
 *
 * @param haystack str to search
 * @param needle substr
 * @return number of substr in str
 */
static inline int
numsubstr(const char *haystack, const char *needle)
{
  int i = 0;
  const char *tmp = haystack;

  while((tmp = strstr(tmp, needle))) {
    i++;
    tmp++;
  }

  return i;
}

/**
 * @brief cleanup people ptrs
 *
 * @param p pointer to person
 * @param len number of people in p
 */
void
destroypeopleptr(Person *p, size_t num)
{
  int i;

  for (i = 0; i < num; i++)
    if (p[i].name && p[i].name == NULL)
      free(p[i].name);
  free(p);
}

int
main(int argc, char *argv[])
{
  int j, i, l, t, c, a, r, min, numteams, numpeople, peopleperteam;
  Person *people, **teams;

  /* seed random number generator */
  srand(time(NULL));

  /* get number of people declared and allocate enough space for them */
  for (i = numpeople = 0; i < argc; i++)
    numpeople += numsubstr(argv[i], "-p");

  /* allocate space for all people */
  people = calloc(numpeople, sizeof(Person));

  /* collect people and team numbers */
  i = 0;
  while ((c = getopt(argc, argv, "p:t:")) != -1) {
    switch (c) {
      case 'p':
        people[i++] = (Person){
          .name = optarg,
          .opponent = NULL
        };
        break;
      case 't':
          numteams = atoi(optarg);
        break;
    }
  }

  /* exit if there aren't enough people for the number of teams */
  if (floor((double)numpeople / (double)numteams) < 1)
    die("%d people is not enough for %d teams", numpeople, numteams);

  /* allocate space for teams */
  teams = calloc(numteams, sizeof(Person *));

  /* calculate maximum number of people per team */
  peopleperteam = floor(((double)numpeople / (double)numteams) + 0.5);

  /* allocate space for people in each team */
  for (i = 0; i < numteams; i++)
    teams[i] = calloc(peopleperteam, sizeof(Person));

  /* randomize array */
  for (i = 0; i < numpeople; i++) {
    r = rand() / (RAND_MAX / numpeople);
    Person tmp = people[r];
    people[r] = people[i];
    people[i] = tmp;
  }

  /* sort people into teams */
  for (i = j = l = min = 0; i < numpeople; i++) {
    if (l == peopleperteam) {
      if (j < numteams - 1) {
        j++;
        l = min;
      } else {
        /* resize arrays */
        for (t = 0; t < numteams; t++) {
          Person *b = calloc(peopleperteam + 1, sizeof(Person));

          for (a = 0; a < peopleperteam; a++) {
            b[a].name = teams[t][a].name;
            b[a].opponent = teams[t][a].opponent;
          }

          destroypeopleptr(teams[t], numpeople);
          teams[t] = b;
          b = NULL;
        }

        j = 0;
        l = min = peopleperteam++;
      }
    }

    /* assign person to team */
    teams[j][l++] = people[i];
  }

  /* print out teams */
  for (i = 0; i < numteams; i++) {
    printf("team #%d:\n", i + 1);
    for (j = 0; j < peopleperteam; j++)
      if (teams[i][j].name)
        puts(teams[i][j].name);
  }

  /* cleanup */
  destroypeopleptr(people, numpeople);

  for (i = 0; i < numteams; i++)
    free(teams[i]);
  free(teams);

  return 0;
}