aboutsummaryrefslogtreecommitdiff
path: root/2017/05/puzzles.c
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-06 21:53:31 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-06 21:53:31 +0100
commitac0ae55b0c2cec33a18b9a4e1ebe3558f3467b57 (patch)
tree890045b786921670da123531bf2cba3a928a2e6b /2017/05/puzzles.c
parent7b8acb52f25bda9a533276067a0e94826f8e14df (diff)
Add day 5 solutions
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;
+}