summary refs log tree commit diff homepage
path: root/2020/day12.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-12 00:15:47 -0500
committerJune McEnroe <june@causal.agency>2020-12-12 00:15:47 -0500
commitb1843541b053131c3f1c55ba69bb9b8f37063c9d (patch)
tree1b43bbe4fe6def112a68e028d98004991f08ea13 /2020/day12.c
parentSolve day 11 part 2 (diff)
downloadaoc-b1843541b053131c3f1c55ba69bb9b8f37063c9d.tar.gz
aoc-b1843541b053131c3f1c55ba69bb9b8f37063c9d.zip
Solve day 12 part 1
Diffstat (limited to '')
-rw-r--r--2020/day12.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/2020/day12.c b/2020/day12.c
new file mode 100644
index 0000000..1d22820
--- /dev/null
+++ b/2020/day12.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
+static void swap(int *a, int *b) {
+	int x = *a;
+	*a = *b;
+	*b = x;
+}
+int main(void) {
+	int x = 0, y = 0;
+	int dx = 1, dy = 0;
+	char act;
+	int val;
+	while (0 < scanf("%c%d\n", &act, &val)) {
+		switch (act) {
+			break; case 'N': y -= val;
+			break; case 'S': y += val;
+			break; case 'E': x += val;
+			break; case 'W': x -= val;
+			break; case 'L': {
+				for (int i = 0; i < val / 90; ++i) {
+					swap(&dx, &dy);
+					dy *= -1;
+				}
+			}
+			break; case 'R': {
+				for (int i = 0; i < val / 90; ++i) {
+					swap(&dx, &dy);
+					dx *= -1;
+				}
+			}
+			break; case 'F': x += val * dx; y += val * dy;
+		}
+	}
+	printf("%d\n", abs(x) + abs(y));
+}