aboutsummaryrefslogtreecommitdiff
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
parentc2744420f190800dac8bc9c17a058e3998291cc9 (diff)
Day 14
l---------day14/Makefile1
-rw-r--r--day14/day14.c66
-rw-r--r--day14/day14.input9
-rw-r--r--day14/day14.readme28
4 files changed, 104 insertions, 0 deletions
diff --git a/day14/Makefile b/day14/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/day14/Makefile
@@ -0,0 +1 @@
+../Makefile \ No newline at end of file
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;
+}
diff --git a/day14/day14.input b/day14/day14.input
new file mode 100644
index 0000000..6cf5489
--- /dev/null
+++ b/day14/day14.input
@@ -0,0 +1,9 @@
+Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds.
+Cupid can fly 22 km/s for 2 seconds, but then must rest for 41 seconds.
+Rudolph can fly 11 km/s for 5 seconds, but then must rest for 48 seconds.
+Donner can fly 28 km/s for 5 seconds, but then must rest for 134 seconds.
+Dasher can fly 4 km/s for 16 seconds, but then must rest for 55 seconds.
+Blitzen can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
+Prancer can fly 3 km/s for 21 seconds, but then must rest for 40 seconds.
+Comet can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
+Vixen can fly 18 km/s for 5 seconds, but then must rest for 84 seconds.
diff --git a/day14/day14.readme b/day14/day14.readme
new file mode 100644
index 0000000..1830f41
--- /dev/null
+++ b/day14/day14.readme
@@ -0,0 +1,28 @@
+--- Day 14: Reindeer Olympics ---
+
+This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race.
+
+Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state.
+
+For example, suppose you have the following Reindeer:
+
+ - Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.
+ - Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.
+
+After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds.
+
+In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds).
+
+Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled?
+
+--- Part Two ---
+
+Seeing how reindeer move in bursts, Santa decides he's not pleased with the old scoring system.
+
+Instead, at the end of each second, he awards one point to the reindeer currently in the lead. (If there are multiple reindeer tied for the lead, they each get one point.) He keeps the traditional 2503 second time limit, of course, as doing otherwise would be entirely ridiculous.
+
+Given the example reindeer from above, after the first second, Dancer is in the lead and gets one point. He stays in the lead until several seconds into Comet's second burst: after the 140th second, Comet pulls into the lead and gets his first point. Of course, since Dancer had been in the lead for the 139 seconds before that, he has accumulated 139 points by the 140th second.
+
+After the 1000th second, Dancer has accumulated 689 points, while poor Comet, our old champion, only has 312. So, with the new scoring system, Dancer would win (if the race ended at 1000 seconds).
+
+Again given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, how many points does the winning reindeer have?