From 7c87d3d6ca0d31c6526169f7e3789a9ffa252f54 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 12 Dec 2020 00:33:00 -0500 Subject: 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. --- 2020/day12.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to '2020/day12.c') 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)); } -- cgit 1.4.1