aboutsummaryrefslogtreecommitdiff
path: root/day14/day14.c
blob: fbb81ef9f1ca5aed428a4de7e3dcd6f008c49c77 (plain)
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
#include <stdio.h>

#define MAXTIME 2503

int calcdist(int speed, int fly, int rest, int time) {
  int second = 0, dist = 0, run;
  while(1) {
    run = 0;
    while(second < time && run < fly) {
      dist += speed;
      second++;
      run++;
    }

    run = 0;
    while(second < time && run < rest) {
      second++;
      run++;
    }

    if(second == time) break;
  }
  return dist;
}

int maxpoints(int* speed, int* fly, int* rest, int num, int time) {
  int i, second = 1, dist[20], maxdist, points[20], maxpoints = 0;

  for(i = 0; i < num; i++) points[i] = 0;

  while(second < time) {
    maxdist = 0;

    for(i = 0; i < num; i++) {
      dist[i] = calcdist(*(speed+i), *(fly+i), *(rest+i), second);
      if(dist[i] > maxdist) maxdist = dist[i];
    }

    for(i = 0; i < num; i++)
      if(dist[i] == maxdist) points[i]++;

    second++;
  }

  for(i = 0; i < num; i++)
    if(points[i] > maxpoints)
      maxpoints = points[i];

  return maxpoints;
}

int main() {
  char name[20];
  int speed[20], fly[20], rest[20], idx = 0, dist, maxdist = 0;

  while(scanf("%s can fly %d km/s for %d seconds, but then must rest for %d seconds.", name, speed+idx, fly+idx, rest+idx) != EOF) {
    dist = calcdist(*(speed+idx), *(fly+idx), *(rest+idx), MAXTIME);
    if(dist > maxdist) maxdist = dist;
    idx++;
  }

  printf("Max distance travelled: %d km\n", maxdist);
  printf("Max points earned: %d\n", maxpoints(speed, fly, rest, idx, MAXTIME));

  return 0;
}