diff options
-rw-r--r-- | 2020/day12.c | 35 |
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)); +} |