summaryrefslogtreecommitdiff
path: root/22.c
blob: 2a47a7972e50a8dd9e7c2d0ffb0d9662ca2bbe11 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#define _GNU_SOURCE

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


// Proper modulo operator
#define modulo(n, m) ((((n) % (m)) + (m)) % (m))


enum map_type {
	M_FLAT,
	M_NET,
};


enum direction {
	D_NORTH = 3,
	D_EAST = 0,
	D_SOUTH = 1,
	D_WEST = 2,
};


char const *
direction_name(enum direction dir)
{
	switch (dir) {
	case D_NORTH: return "North";
	case D_EAST: return "East";
	case D_SOUTH: return "South";
	case D_WEST: return "West";
	}
	return NULL;
}


struct elf {
	int x;
	int y;
	enum direction dir;
};


int
elf_turn(struct elf * elf, char turn)
{
#define direction_go(d, t) (1000 * (d) + (t))

	switch (direction_go(elf->dir, turn)) {
	case direction_go(D_NORTH, 'R'):
	case direction_go(D_SOUTH, 'L'):
		elf->dir = D_EAST;
		break;

	case direction_go(D_NORTH, 'L'):
	case direction_go(D_SOUTH, 'R'):
		elf->dir = D_WEST;
		break;

	case direction_go(D_EAST, 'L'):
	case direction_go(D_WEST, 'R'):
		elf->dir = D_NORTH;
		break;

	case direction_go(D_EAST, 'R'):
	case direction_go(D_WEST, 'L'):
		elf->dir = D_SOUTH;
		break;

	default:
		fprintf(stderr, "Unexpected elf_turn(%d, %c)\n", elf->dir, turn);
		return -1;
	}

	return 0;

#undef direction_go
}


int
elf_move(struct elf * elf, char const * map, int cols, int rows, enum map_type mtype, int distance)
{
	while (distance > 0) {
		int dx = 0, dy = 0, elem;
		struct elf newelf;

		switch (elf->dir) {
		case D_NORTH: dy = -1; break;
		case D_SOUTH: dy = +1; break;
		case D_EAST: dx = +1; break;
		case D_WEST: dx = -1; break;
		default:
			fprintf(stderr, "Unexpected direction %d\n", elf->dir);
			return -1;
		}

		newelf.x = modulo(elf->x + dx, cols);
		newelf.y = modulo(elf->y + dy, rows);
		newelf.dir = elf->dir;
		elem = map[newelf.x + newelf.y * cols];

		if (elem == ' ' || elem == '\n') {
			switch (mtype) {
			case M_FLAT:
				// Just keep going until we wrap around to a non-blank position
				while (elem == ' ' || elem == '\n') {
					newelf.x = modulo(newelf.x + dx, cols);
					newelf.y = modulo(newelf.y + dy, rows);
					elem = map[newelf.x + newelf.y * cols];
				}
				break;

			case M_NET:
				fprintf(stderr, "Map type not yet supported!\n");
				return -1;

			default:
				fprintf(stderr, "Unknown map type %d\n", mtype);
				return -1;
			}
		}

		switch (elem) {
		case '.':
			*elf = newelf;
			--distance;
			break;

		case '#':
			distance = 0;
			break;

		default:
			fprintf(stderr, "Unexpected map character %c\n", elem);
			return -1;
		}
	}

	return 0;
}


int
main(int argc, char ** argv)
{
	char * buf = NULL, * beg, * end;
	enum map_type mtype = M_FLAT;
	long bufsiz = 0, buflen = 0, maxline = 0, lines = 0;
	long pos;
	struct elf elf;
	char distance[8], c;
	int dlen = 0;

	while ((pos = getopt(argc, argv, "p:m:")) != -1) {
		switch (pos) {
		case 'p':
			switch (atoi(optarg)) {
			case 1:
				mtype = M_FLAT;
				break;

			case 2:
				mtype = M_NET;
				break;

			default:
				fprintf(stderr, "Unexpected puzzle part %s\n", optarg);
				return -1;
			}
			break;

		case 'm':
			if (strcmp(optarg, "flat") == 0) {
				mtype = M_FLAT;
			}
			else if (strcmp(optarg, "net") == 0) {
				mtype = M_NET;
			}
			else {
				fprintf(stderr, "Unexpected map type %s\n", optarg);
				return -1;
			}
			break;

		default:
			return -1;
		}
	}

	// Read map data
	// Don't try and figure out lines yet, in case we get a line longer than
	// our read buffer, which could make things awkward.
	while (1) {
		if (bufsiz - buflen < BUFSIZ / 2) {
			void * p;
			bufsiz += BUFSIZ;
			if ((p = realloc(buf, bufsiz)) == NULL) {
				fprintf(stderr, "Bad realloc(%ld)\n", bufsiz);
				free(buf);
				return -1;
			}
			buf = p;
		}

		if (!fgets(buf + buflen, bufsiz - buflen, stdin))
			// End of file!
			break;

		if (buflen > 0 && buf[buflen] == '\n' && buf[buflen - 1] == '\n')
			// End of map input.
			break;

		buflen += strlen(buf + buflen);
	}

	// Work out max lines and longest line.
	for (beg = buf, lines = 0; beg < buf + buflen; beg = end + 1, ++lines) {
		end = memchr(beg, '\n', buf + buflen - beg);
		if (end - beg > maxline)
			maxline = end - beg;
	}
	if (maxline == 0 || lines == 0) {
		fprintf(stderr, "Unexpected map size (%ld,%ld)\n", maxline, lines);
		free(buf);
		return -1;
	}
	++maxline; // Also count the newline on the end of each line.

	// Resize the map data buffer to hold a full grid of data
	if ((beg = realloc(buf, lines * maxline)) == NULL) {
		fprintf(stderr, "Bad realloc(%ld)\n", bufsiz);
		free(buf);
		return -1;
	}
	buf = beg;
	memset(buf + buflen, ' ', lines * maxline - buflen);

	// Move the map data so that it fits the grid properly
	for (end = buf + buflen, pos = lines - 1; pos >= 0; end = beg, --pos) {
		beg = memrchr(buf, '\n', (end - buf) - 1);
		beg = beg ? beg + 1 : buf;
		memmove(buf + pos * maxline, beg, (end - beg));
		memset(buf + (pos * maxline) + (end - beg) - 1, ' ', maxline - (end - beg) + 1);
		buf[pos * maxline + maxline - 1] = '\n';
	}

	// Set initial position
	elf.x = 0;
	elf.y = 0;
	elf.dir = D_EAST;
	while (elf.x < maxline && buf[elf.x] != '.')
		++elf.x;

	// Read and follow the movement instructions.
	while (elf.x >= 0 && (c = fgetc(stdin)) != EOF) {
		if (isdigit(c)) {
			distance[dlen++] = c;
		}
		else if (c == 'L' || c == 'R') {
			distance[dlen] = '\0';

			if (elf_move(&elf, buf, maxline, lines, mtype, atoi(distance)) != 0)
				elf.x = -1;
			if (elf_turn(&elf, c) != 0)
				elf.x = -1;

			dlen = 0;
		}
		else if (c == '\n') {
			// Cover last bit of distance, if any
			distance[dlen] = '\0';
			if (elf_move(&elf, buf, maxline, lines, mtype, atoi(distance)) != 0)
				elf.x = -1;
			break;
		}
		else {
			fprintf(stderr, "Unexpected movement instruction (%c)\n", c);
			free(buf);
			return -1;
		}
	}
	if (elf.x < 0) {
		free(buf);
		return -1;
	}

	// Done.
	printf("Password is %d (%d,%d,%d)\n",
			1000 * (1 + elf.y) + 4 * (1 + elf.x) + elf.dir,
			1 + elf.x, 1 + elf.y, elf.dir);

	// Tidy and exit.
	free(buf);

	return 0;
}