summaryrefslogtreecommitdiff
path: root/24.c
blob: 8e2f498403c03ee1df81009d2b967e4d9d0a185e (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define C_EMPTY	0x00

#define C_BLZ_N	0x01
#define C_BLZ_E	0x02
#define C_BLZ_S 0x04
#define C_BLZ_W	0x08

#define C_BLZ	(C_BLZ_N | C_BLZ_E | C_BLZ_S | C_BLZ_W)

#define C_WALL  0x10
#define C_ELF	0x20


unsigned char *
pos(unsigned char * buf, int cols, int rows, int i, int j)
{
	i = ((i % cols) + cols) % cols;
	j = ((j % rows) + rows) % rows;

	return buf + i + j * cols;
}


void
clear_mask(unsigned char * buf, int cols, int rows, unsigned mask)
{
	int i, j;

	for (j = 0; j < rows; ++j) {
		for (i = 0; i < cols; ++i) {
			*pos(buf, cols, rows, i, j) &= ~mask;
		}
	}
}


int
find_path(unsigned char * buf, int cols, int rows,
		int start_col, int start_row, int exit_col, int exit_row)
{
	unsigned char * realnext, * next;
	int i, j, round;

	// Create the "next" buffer, and place the walls in it.
	if ((next = realnext = calloc(rows, cols)) == NULL) {
		fprintf(stderr, "Bad next alloc(%d)\n", cols * rows);
		return -1;
	}
	for (j = 0; j < rows; ++j) {
		for (i = 0; i < cols; ++i) {
			if (*pos(buf, cols, rows, i, j) == C_WALL)
				*pos(next, cols, rows, i, j) = C_WALL;
		}
	}

	// Place the start elf
	*pos(buf, cols, rows, start_col, start_row) = C_ELF;

	for (round = 0; (*pos(buf, cols, rows, exit_col, exit_row) & C_ELF) == 0; ++round) {
		int elves = 0;
		unsigned char * psrc, * pdest, * tmp;

		// Work out the possible positions of elves and blizzards in `next`
		for (j = 0; j < rows; ++j) {
			for (i = 0; i < cols; ++i) {
				int o;

				psrc = pos(buf, cols, rows, i, j);

				if (*psrc & C_ELF) {
					*pos(next, cols, rows, i, j) |= C_ELF;
					if ((*(pdest = pos(next, cols, rows, i, j - 1)) & C_WALL) == 0)
						*pdest |= C_ELF;
					if ((*(pdest = pos(next, cols, rows, i + 1, j)) & C_WALL) == 0)
						*pdest |= C_ELF;
					if ((*(pdest = pos(next, cols, rows, i, j + 1)) & C_WALL) == 0)
						*pdest |= C_ELF;
					if ((*(pdest = pos(next, cols, rows, i - 1, j)) & C_WALL) == 0)
						*pdest |= C_ELF;
					++elves;
				}
				if (*psrc & C_BLZ_N) {
					for (o = 1; (*(pdest = pos(next, cols, rows, i, j - o)) & C_WALL) != 0; ++o)
						;
					*pdest |= C_BLZ_N;
				}
				if (*psrc & C_BLZ_E) {
					for (o = 1; (*(pdest = pos(next, cols, rows, i + o, j)) & C_WALL) != 0; ++o)
						;
					*pdest |= C_BLZ_E;
				}
				if (*psrc & C_BLZ_S) {
					for (o = 1; (*(pdest = pos(next, cols, rows, i, j + o)) & C_WALL) != 0; ++o)
						;
					*pdest |= C_BLZ_S;
				}
				if (*psrc & C_BLZ_W) {
					for (o = 1; (*(pdest = pos(next, cols, rows, i - o, j)) & C_WALL) != 0; ++o)
						;
					*pdest |= C_BLZ_W;
				}
			}
		}
		if (elves == 0) {
			fprintf(stderr, "Warning, ran out of elves after %d rounds!\n", round);
			free(realnext);
			return -1;
		}

		// Whereever there is a blizzard and and elf together, remove the elf
		for (j = 0; j < rows; ++j) {
			for (i = 0; i < cols; ++i) {
				if ((*(pdest = pos(next, cols, rows, i, j)) & C_ELF) != 0
						&& (*pdest & C_BLZ) != 0)
				{
					*pdest &= ~C_ELF;
				}
			}
		}

		// Swap next and buf, and clear `next` for next round
		tmp = buf;
		buf = next;
		next = tmp;
		clear_mask(next, rows, cols, C_ELF | C_BLZ);
	}

	if (next != realnext) {
		// Our `next` and `buf` variables are flipped.
		// Copy `buf` into `next` so that the caller has an up-to-date
		// copy of the map.
		memcpy(next, buf, cols * rows);
	}

	// Tidy and exit
	free(realnext);

	return round;
}


int
main(int argc, char ** argv)
{
	unsigned char * buf = NULL;
	int part = 1, bufsiz = 0, buflen = 0;
	int cols = -1, rows = -1, start_col = -1, start_row = -1, exit_col = -1, exit_row = -1;
	int i, round;

	// Parse options
	while ((i = getopt(argc, argv, "p:")) != -1) {
		switch (i) {
		case 'p':
			part = atoi(optarg);
			break;

		default:
			return -1;
		}
	}

	// Read input
	while (1) {
		if (bufsiz - buflen < BUFSIZ) {
			void * p;

			bufsiz += BUFSIZ;
			if ((p = realloc(buf, bufsiz)) == NULL) {
				fprintf(stderr, "Bad realloc(%d)\n", bufsiz);
				free(buf);
				return -1;
			}
			buf = p;
		}

		if ((i = fgetc(stdin)) == EOF)
			break;
		switch (i) {
		case '#':
			buf[buflen++] = C_WALL;
			break;

		case '.':
			buf[buflen++] = C_EMPTY;
			break;

		case '^':
			buf[buflen++] = C_BLZ_N;
			break;

		case '>':
			buf[buflen++] = C_BLZ_E;
			break;

		case 'v':
			buf[buflen++] = C_BLZ_S;
			break;

		case '<':
			buf[buflen++] = C_BLZ_W;
			break;

		case '\n':
			if (cols == -1)
				cols = buflen;
			break;

		default:
			fprintf(stderr, "Unexpected input char %c\n", i);
			return -1;
		}
	}
	if (cols == -1 || buflen % cols != 0) {
		fprintf(stderr, "Input size %d is not a multiple of row length %d\n",
				buflen, cols);
		free(buf);
		return -1;
	}
	rows = buflen / cols;

	// Find the start/exit positions
	start_row = 0;
	exit_row = rows - 1;
	for (i = 0; i < cols; ++i) {
		if (*pos(buf, cols, rows, i, start_row) == C_EMPTY)
			start_col = i;
		if (*pos(buf, cols, rows, i, exit_row) == C_EMPTY)
			exit_col = i;
	}
	if (exit_col == -1) {
		fprintf(stderr, "No exit position found!\n");
		free(buf);
		return -1;
	}

	switch (part) {
	case 1:
		round = find_path(buf, cols, rows, start_col, start_row, exit_col, exit_row);
		break;

	case 2:
		round = find_path(buf, cols, rows, start_col, start_row, exit_col, exit_row);
		clear_mask(buf, cols, rows, C_ELF);
		round += find_path(buf, cols, rows, exit_col, exit_row, start_col, start_row);
		clear_mask(buf, cols, rows, C_ELF);
		round += find_path(buf, cols, rows, start_col, start_row, exit_col, exit_row);
		break;

	default:
		fprintf(stderr, "Unexpected part %d\n", part);
		free(buf);
		return -1;
	}

	printf("Found exit after %d rounds\n", round);

	// Tidy and exit
	free(buf);

	return 0;
}