aboutsummaryrefslogtreecommitdiff
path: root/2020/12/puzzle-1.c
diff options
context:
space:
mode:
Diffstat (limited to '2020/12/puzzle-1.c')
-rw-r--r--2020/12/puzzle-1.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/2020/12/puzzle-1.c b/2020/12/puzzle-1.c
new file mode 100644
index 0000000..1a4c7ab
--- /dev/null
+++ b/2020/12/puzzle-1.c
@@ -0,0 +1,73 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(void)
+{
+ FILE *fp;
+ char cl[5];
+ int north, east;
+ int direction = 0;
+
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+
+ north = east = 0;
+ while (fgets(cl, sizeof(cl), fp) != NULL) {
+ switch (cl[0]) {
+ case 'N':
+ north += atoi(&cl[1]);
+ break;
+ case 'E':
+ east += atoi(&cl[1]);
+ break;
+ case 'S':
+ north -= atoi(&cl[1]);
+ break;
+ case 'W':
+ east -= atoi(&cl[1]);
+ break;
+ case 'L':
+ direction -= atoi(&cl[1]);
+ goto wrap;
+ case 'R':
+ direction += atoi(&cl[1]);
+
+wrap:
+ /* 0 <= direction < 360 */
+ if (direction >= 360)
+ direction -= 360;
+ else if (direction < 0)
+ direction += 360;
+ break;
+ case 'F':
+ switch (direction) {
+ case 0:
+ east += atoi(&cl[1]);
+ break;
+ case 90:
+ north -= atoi(&cl[1]);
+ break;
+ case 180:
+ east -= atoi(&cl[1]);
+ break;
+ case 270:
+ north += atoi(&cl[1]);
+ break;
+ }
+ break;
+ }
+ }
+ fclose(fp);
+
+ /* Absolute value */
+ if (north < 0)
+ north = -north;
+ if (east < 0)
+ east = -east;
+
+ printf("%d\n", north + east);
+ return EXIT_SUCCESS;
+}