aboutsummaryrefslogtreecommitdiff
path: root/2017/05/puzzles.c
diff options
context:
space:
mode:
Diffstat (limited to '2017/05/puzzles.c')
-rw-r--r--2017/05/puzzles.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/2017/05/puzzles.c b/2017/05/puzzles.c
new file mode 100644
index 0000000..1f78e14
--- /dev/null
+++ b/2017/05/puzzles.c
@@ -0,0 +1,38 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main(void)
+{
+ int n, s;
+ int *jumps;
+ FILE *fp;
+
+ if (!(jumps = calloc(PROGLEN, sizeof(int))))
+ err(EXIT_FAILURE, "calloc");
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+ for (int i = 0; fscanf(fp, "%d", &n) != EOF; i++)
+ jumps[i] = n;
+ fclose(fp);
+
+ s = 0;
+ for (int i = 0; i >= 0 && i < PROGLEN; s++) {
+#ifdef PART2
+ if (jumps[i] >= 3) {
+ jumps[i]--;
+ i += jumps[i] + 1;
+ } else {
+ jumps[i]++;
+ i += jumps[i] - 1;
+ }
+#else
+ jumps[i]++;
+ i += jumps[i] - 1;
+#endif
+ }
+
+ printf("%d\n", s);
+ return EXIT_SUCCESS;
+}