From b1843541b053131c3f1c55ba69bb9b8f37063c9d Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 12 Dec 2020 00:15:47 -0500 Subject: Solve day 12 part 1 --- 2020/day12.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2020/day12.c 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 +#include +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)); +} -- cgit 1.4.1