summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-12 00:33:00 -0500
committerJune McEnroe <june@causal.agency>2020-12-12 00:33:00 -0500
commit7c87d3d6ca0d31c6526169f7e3789a9ffa252f54 (patch)
treeeeca1a484cc547a87154da1ed1998316b0badd10 /2020
parentSolve day 12 part 1 (diff)
downloadaoc-7c87d3d6ca0d31c6526169f7e3789a9ffa252f54.tar.gz
aoc-7c87d3d6ca0d31c6526169f7e3789a9ffa252f54.zip
Solve day 12 part 2
In classic fashion, I typo'd an x as a y and failed to catch it until
reading over the code for the 5th time.
Diffstat (limited to '2020')
-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));
 }