aboutsummaryrefslogtreecommitdiff
path: root/2020/12/solve.c
blob: fb101416c2be0b82631574ce1016a9b932cd7414 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum dir { N, E, S, W };

// I <3 recursion
enum dir rotate(enum dir dir, int cnt, int r)
{
	if (!cnt)
		return dir;

	if (r) { // right
		if (dir == W)
			dir = N;
		else
			dir++;
	} else { // left
		if (dir == N)
			dir = W;
		else
			dir--;
	}
	return rotate(dir, cnt - 1, r);
}

// Quite inefficient but I like this approach.
int *rotate_wp(int dirs[], int cnt, int r)
{
	if (!cnt)
		return dirs;

	if (r) {
		int tmp = dirs[W];
		dirs[W] = dirs[S];
		dirs[S] = dirs[E];
		dirs[E] = dirs[N];
		dirs[N] = tmp;
	} else {
		int tmp = dirs[N];
		dirs[N] = dirs[E];
		dirs[E] = dirs[S];
		dirs[S] = dirs[W];
		dirs[W] = tmp;
	}

	return rotate_wp(dirs, cnt - 1, r);
}

int part_one(FILE *fp)
{
	char *line = NULL;
	size_t len = 0;
	int coords[4] = { 0 };
	enum dir cur = E;
	while (getline(&line, &len, fp) != -1) {
		char dir;
		int cnt;
		sscanf(line, "%c%d\n", &dir, &cnt);
		switch (dir) {
		case 'N':
			coords[N] += cnt;
			break;
		case 'E':
			coords[E] += cnt;
			break;
		case 'S':
			coords[S] += cnt;
			break;
		case 'W':
			coords[W] += cnt;
			break;
		case 'L':
			cur = rotate(cur, cnt / 90, 0);
			break;
		case 'R':
			cur = rotate(cur, cnt / 90, 1);
			break;
		case 'F':
			coords[cur] += cnt;
			break;
		default:
			break;
		}
	}

	return abs((coords[E] - coords[W]) + (coords[S] - coords[N]));
}

int part_two(FILE *fp)
{
	char *line = NULL;
	size_t len = 0;
	int wp[4] = { 1, 10, 0, 0 };
	int coords[4] = { 0 };
	enum dir cur = E;
	while (getline(&line, &len, fp) != -1) {
		char dir;
		int cnt;
		sscanf(line, "%c%d\n", &dir, &cnt);
		switch (dir) {
		case 'N':
			wp[N] += cnt;
			break;
		case 'E':
			wp[E] += cnt;
			break;
		case 'S':
			wp[S] += cnt;
			break;
		case 'W':
			wp[W] += cnt;
			break;
		case 'L':
			rotate_wp(wp, cnt / 90, 0);
			break;
		case 'R':
			rotate_wp(wp, cnt / 90, 1);
			break;
		case 'F':
			coords[N] += cnt * wp[N];
			coords[E] += cnt * wp[E];
			coords[S] += cnt * wp[S];
			coords[W] += cnt * wp[W];
			break;
		default:
			break;
		}
	}

	return abs((coords[E] - coords[W])) + abs((coords[S] - coords[N]));
}

int main(int argc, char *argv[])
{
	FILE *fp = fopen("input", "r");
	if (!fp)
		exit(EXIT_FAILURE);

	clock_t tic = clock();
	printf("%d\n", part_one(fp));
	rewind(fp);
	printf("%d\n", part_two(fp));
	clock_t toc = clock();
	printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);

	fclose(fp);
	return 0;
}