summary refs log tree commit diff homepage
path: root/2020/day12.c
blob: a140d1b36e59c1fbd5248498a467a11142a86aae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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;
	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; 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;
				sx += val * wx;
				sy += val * wy;
			}
		}
	}
	printf("%d\n", abs(x) + abs(y));
	printf("%d\n", abs(sx) + abs(sy));
}