aboutsummaryrefslogtreecommitdiff
path: root/day14/day14.c
diff options
context:
space:
mode:
authorNæþ'n Lasseter <Næþ'n Lasseter nathan@bytemark.co.uk>2015-12-14 17:42:16 +0000
committerNæþ'n Lasseter <Næþ'n Lasseter nathan@bytemark.co.uk>2015-12-14 17:42:16 +0000
commit598e6dea91e17b952af4f31263274fdc05ceee64 (patch)
tree77a65a7426b805efcb904d06d4e046e759ec6be0 /day14/day14.c
parentc2744420f190800dac8bc9c17a058e3998291cc9 (diff)
Day 14
Diffstat (limited to 'day14/day14.c')
-rw-r--r--day14/day14.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/day14/day14.c b/day14/day14.c
new file mode 100644
index 0000000..fbb81ef
--- /dev/null
+++ b/day14/day14.c
@@ -0,0 +1,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;
+}