summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2020/day12.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/2020/day12.c b/2020/day12.c
index 1d22820..a140d1b 100644
--- a/2020/day12.c
+++ b/2020/day12.c
@@ -8,28 +8,40 @@ static void swap(int *a, int *b) {
 int main(void) {
 	int x = 0, y = 0;
 	int dx = 1, dy = 0;
+	int sx = 0, sy = 0;
+	int wx = 10, wy = -1;
 	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 'N': y -= val; wy -= val;
+			break; case 'S': y += val; wy += val;
+			break; case 'E': x += val; wx += val;
+			break; case 'W': x -= val; wx -= val;
 			break; case 'L': {
 				for (int i = 0; i < val / 90; ++i) {
 					swap(&dx, &dy);
+					swap(&wx, &wy);
 					dy *= -1;
+					wy *= -1;
 				}
 			}
 			break; case 'R': {
 				for (int i = 0; i < val / 90; ++i) {
 					swap(&dx, &dy);
+					swap(&wx, &wy);
 					dx *= -1;
+					wx *= -1;
 				}
 			}
-			break; case 'F': x += val * dx; y += val * dy;
+			break; case 'F': {
+				x += val * dx;
+				y += val * dy;
+				sx += val * wx;
+				sy += val * wy;
+			}
 		}
 	}
 	printf("%d\n", abs(x) + abs(y));
+	printf("%d\n", abs(sx) + abs(sy));
 }