summaryrefslogtreecommitdiff
path: root/24.c
blob: e1f159d10b165fa15a69eed7c0c6cd95700581f4 (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
#include <stdio.h>
#include <stdlib.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;
}


int
main()
{
	unsigned char * buf = NULL, * next = NULL;
	int bufsiz = 0, buflen = 0;
	int cols = -1, rows = -1, exit_col = -1, exit_row = -1;
	int i, j, round;

	// 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 '.':
			if (cols == -1)
				buf[buflen++] = C_ELF;
			else
				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 exit position
	exit_row = rows - 1;
	for (i = 0; i < cols; ++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;
	}

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

	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(next);
			free(buf);
			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;
		for (j = 0; j < rows; ++j) {
			for (i = 0; i < cols; ++i) {
				if ((*(pdest = pos(next, cols, rows, i, j)) & C_WALL) == 0)
					*pdest = C_EMPTY;
			}
		}
	}

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

	// Tidy and exit
	free(next);
	free(buf);

	return 0;
}