aboutsummaryrefslogtreecommitdiffstats
path: root/src/15/main.c
blob: 0598503e9f7a03f518c8d0827b9e6049cf157207 (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "aoc.h"
#include "dvec_s.h"
#include "allocator.h"
#include "util.h"
#include "iccmp.h"
#include "hmap.h"
#include "vec.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

enum {
	MOVE_NORTH = 1,
	MOVE_SOUTH,
	MOVE_WEST,
	MOVE_EAST
};

enum {
	BLOCK_WALL = 0,
	BLOCK_OPEN,
	BLOCK_GOAL
};

struct state {
	int back;
	int next;
};

const char block_lut[] = {
	'#',
	'.',
	'O'
};

const struct vec2i dirs[] = {
	{ 0 },
	{ 0, -1 },
	{ 0, 1 },
	{ -1, 0 },
	{ 1, 0 },
};

const int rev[] = {
	0, 2, 1, 4, 3
};

uint32_t
vec_hmap_hash(struct hmap_key key)
{
	const struct vec2i *vec = key.p;

	return (uint32_t) (vec->x << 16) ^ (uint32_t) vec->y;
}

bool
vec_hmap_keycmp(struct hmap_key k1, struct hmap_key k2)
{
	const struct vec2i *v1 = k1.p, *v2 = k2.p;

	return v1->x == v2->x && v1->y == v2->y;
}

int
robot_move(struct icc *icc, int dir)
{
	while (icc->state != ICC_HALT) {
		icc_step_inst(icc);
		switch (icc->state) {
		case ICC_INPUT:
			mi_setv(&icc->in, (mi_ul) dir, MI_POS);
			continue;
		case ICC_OUTPUT:
			return (int) mi_cast_ul(&icc->out);
		case ICC_HALT:
		case ICC_RUN:
			continue;
		}
	}

	return -1;
}

void
print_map(struct hmap *map, struct vec2i droid)
{
	struct hmap_link *link;
	struct hmap_iter iter;
	struct vec2i min = { 0 };
	struct vec2i max = { 0 };
	struct vec2i pos;
	const struct vec2i *vec;

	for (HMAP_ITER(map, iter)) {
		vec = iter.link->key.p;
		min.x = MIN(min.x, vec->x);
		min.y = MIN(min.y, vec->y);
		max.x = MAX(max.x, vec->x);
		max.y = MAX(max.y, vec->y);
	}

	fprintf(stderr, "+");
	for (pos.x = min.x; pos.x <= max.x; pos.x++)
		fprintf(stderr, "-");
	fprintf(stderr, "+\n");

	for (pos.y = min.y; pos.y <= max.y; pos.y++) {
		fprintf(stderr, "|");
		for (pos.x = min.x; pos.x <= max.x; pos.x++) {
			if (pos.x == droid.x && pos.y == droid.y) {
				fprintf(stderr, "D");
			} else {
				link = hmap_get(map,
					(struct hmap_key) {.p = &pos});
				if (link) {
					fprintf(stderr, "%c", link->value.c);
				} else {
					fprintf(stderr, " ");
				}
			}
		}
		fprintf(stderr, "|\n");
	}

	fprintf(stderr, "+");
	for (pos.x = min.x; pos.x <= max.x; pos.x++)
		fprintf(stderr, "-");
	fprintf(stderr, "+\n");
}

size_t
dijkstra_dist(struct hmap *map, struct vec2i start, const struct vec2i *end)
{
	struct hmap_link *link, **linkp;
	struct hmap_iter iter;
	struct hmap seen;
	struct dvec outer;
	struct vec2i *vec;
	struct vec2i pos, npos;
	size_t dist, maxdist;
	void *key;
	int dir;

	hmap_init(&seen, 32, vec_hmap_hash, vec_hmap_keycmp,
		&stdlib_strict_heap_allocator);
	dvec_init(&outer, sizeof(struct vec2i),
		10, &stdlib_strict_heap_allocator);

	vec = dvec_add_slot(&outer);
	vec2i_set(vec, &start);

	key = memdup(vec, sizeof(struct vec2i));
	hmap_add(&seen, (struct hmap_key) {.p = key},
		(struct hmap_val) {.s = 0});

	maxdist = 0;
	while (!dvec_empty(&outer)) {
		vec = dvec_back(&outer);
		vec2i_set(&pos, vec);
		link = hmap_get(&seen, (struct hmap_key) {.p = &pos});
		assert(link);
		dist = link->value.s + 1;
		dvec_rm_slot(&outer, vec);

		for (dir = 1; dir <= 4; dir++) {
			vec2i_add(&npos, &pos, &dirs[dir]);
			linkp = hmap_link_pos(&seen,
				(struct hmap_key) {.p = &npos});
			if (*linkp) continue;

			if (end && !memcmp(&npos, end, sizeof(struct vec2i)))
				goto exit;

			link = hmap_get(map, (struct hmap_key) {.p = &npos});
			assert(link);
			if (link->value.c != '.') continue;

			if (!maxdist || dist > maxdist)
				maxdist = dist;

			vec = dvec_add_slot(&outer);
			vec2i_set(vec, &npos);
			key = memdup(&npos, sizeof(struct vec2i));
			*linkp = hmap_link_alloc(&seen,
				(struct hmap_key) {.p = key},
				(struct hmap_val) {.s = dist}, NULL);
		}
	}

exit:
	for (HMAP_ITER(&seen, iter))
		free(iter.link->key._p);
	dvec_deinit(&outer);
	hmap_deinit(&seen);

	return end ? dist : maxdist;
}

void
explore_map(struct icc *icc, struct hmap *map,
	struct vec2i start, struct vec2i *tank)
{
	struct hmap_link **linkp;
	struct hmap_link *link;
	struct dvec states;
	struct vec2i pos, npos;
	struct state *state;
	int move_result;
	int move_dir;
	void *key;

	dvec_init(&states, sizeof(struct state),
		10, &stdlib_strict_heap_allocator);

	state = dvec_add_slot(&states);
	state->back = -1;
	state->next = 1;

	vec2i_set(&pos, &start);
	key = memdup(&pos, sizeof(struct vec2i));
	hmap_add(map, (struct hmap_key) {.p = key},
		(struct hmap_val) {.c = '.'});

	while (!dvec_empty(&states)) {
		assert(!dvec_empty(&states));
		state = dvec_back(&states);
		for (; state->next <= 4; state->next++) {
			if (state->next == state->back)
				continue;
			move_dir = state->next;
			vec2i_add(&npos, &pos, &dirs[move_dir]);
			link = hmap_get(map,
				(struct hmap_key) {.p = &npos});
			if (!link) break;
		}

		if (state->next > 4) {
			if (state->back <= 0) break;
			move_result = robot_move(icc, state->back);
			assert(move_result == BLOCK_OPEN);
			vec2i_add(&pos, &pos, &dirs[state->back]);
			dvec_rm_slot(&states, state);
			if (aoc.debug) {
				aoc_debug("Step: backtrack x %i y %i\n", pos.x, pos.y);
				print_map(map, pos);
				aoc_debug("\n");
			}
			continue;
		}
		state->next += 1;

		assert(move_dir >= 1 && move_dir <= 4);
		move_result = robot_move(icc, move_dir);
		assert(move_result >= 0 && move_result <= 2);

		linkp = hmap_link_pos(map, (struct hmap_key) {.p = &npos});
		if (*linkp) {
			(*linkp)->value.c = block_lut[move_result];
		} else {
			key = memdup(&npos, sizeof(struct vec2i));
			*linkp = hmap_link_alloc(map,
				(struct hmap_key) {.p = key},
				(struct hmap_val) {.c = block_lut[move_result]},
				NULL);
		}

		switch (move_result) {
		case BLOCK_GOAL:
			vec2i_set(tank, &npos);
		case BLOCK_OPEN:
			vec2i_set(&pos, &npos);
			state = dvec_add_slot(&states);
			state->back = rev[move_dir];
			state->next = 1;
			break;
		case BLOCK_WALL:
			break;
		default:
			assert(0);
		}

		if (aoc.debug) {
			aoc_debug("Step: x %i y %i dx %i dy %i\n",
				pos.x, pos.y, dirs[move_dir].x, dirs[move_dir].y);
			print_map(map, pos);
			aoc_debug("\n");
		}
	}

	dvec_deinit(&states);
}

void
part1(void)
{
	struct icc icc;
	struct hmap_iter iter;
	struct hmap map;
	struct vec2i start, tank;
	size_t dist;

	icc_init(&icc);
	hmap_init(&map, 32, vec_hmap_hash, vec_hmap_keycmp,
		&stdlib_strict_heap_allocator);

	icc_parse_inst(&icc, aoc.input, aoc.input_size);

	start = (struct vec2i) { 0, 0 };
	explore_map(&icc, &map, start, &tank);

	dist = dijkstra_dist(&map, start, &tank);

	aoc.answer = aprintf("%lu", dist);
	aoc.solution = "234";

	for (HMAP_ITER(&map, iter))
		free(iter.link->key._p);
	hmap_deinit(&map);
	icc_deinit(&icc);
}

void
part2(void)
{
	struct icc icc;
	struct hmap_iter iter;
	struct hmap map;
	struct vec2i start, tank;
	size_t dist;

	icc_init(&icc);
	hmap_init(&map, 32, vec_hmap_hash, vec_hmap_keycmp,
		&stdlib_strict_heap_allocator);

	icc_parse_inst(&icc, aoc.input, aoc.input_size);

	start = (struct vec2i) { 0, 0 };
	explore_map(&icc, &map, start, &tank);

	dist = dijkstra_dist(&map, tank, NULL);

	aoc.answer = aprintf("%lu", dist);
	aoc.solution = "292";

	for (HMAP_ITER(&map, iter))
		free(iter.link->key._p);
	hmap_deinit(&map);
	icc_deinit(&icc);
}