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
|
#include "allocator.h"
#include "aoc.h"
#include "hmap.h"
#include "hmap_s.h"
#include "vec_s.h"
#include "util.h"
#include "list.h"
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define PORTAL_ID(a, b) ((size_t) ((uint8_t) (a) * 100 + (uint8_t) (b)))
struct djk_state {
size_t dist;
struct vec2i pos;
struct list_link link;
};
static bool
djk_dist_ordering(const void *p1, const void *p2, void *u)
{
const struct djk_state *s1 = p1, *s2 = p2;
return s1->dist < s2->dist;
}
static void
load_map(struct hmap *map, const char *input, size_t len)
{
char line[256];
const char *lpos, *lend;
struct vec2i pos, *key;
lpos = aoc.input;
lend = lpos + aoc.input_size;
vec2i_setv(&pos, 0, 0);
while (readtok(line, sizeof(line), '\n', &lpos, lend)) {
for (pos.x = 0; line[pos.x]; pos.x++) {
if (line[pos.x] == ' ')
continue;
key = malloc(sizeof(struct vec2i));
vec2i_set(key, &pos);
hmap_add(map, (struct hmap_key) {.p = key},
(struct hmap_val) {.c = line[pos.x]});
}
pos.y += 1;
}
}
static void
load_portals(struct hmap *portals, struct hmap *map)
{
struct hmap_link *link1;
struct hmap_link *link2;
struct hmap_iter iter;
struct vec2i pos;
struct vec2i npos;
struct vec2i *key;
size_t i, id;
for (HMAP_ITER(map, iter)) {
if (!isupper(iter.link->value.c))
continue;
vec2i_set(&pos, iter.link->key.p);
for (i = 0; i < 4; i++) {
vec2i_add(&npos, &pos, &adj[i]);
link1 = hmap_get(map, (struct hmap_key) {.p = &npos});
if (link1 && isupper(link1->value.c))
break;
}
if (i != ADJ_SOUTH && i != ADJ_EAST)
continue;
for (i = 0; i < 4; i++) {
vec2i_add(&npos, &pos, &adj[i]);
link2 = hmap_get(map, (struct hmap_key) {.p = &npos});
if (link2 && link2->value.c == '.')
break;
}
if (i == 4) {
for (i = 0; i < 4; i++) {
vec2i_add(&npos, link1->key.p, &adj[i]);
link2 = hmap_get(map, (struct hmap_key) {.p = &npos});
if (link2 && link2->value.c == '.')
break;
}
}
assert(i != 4);
aoc_debug("FOUND %c %c\n", iter.link->value.c, link1->value.c);
key = memdup(link2->key.p, sizeof(struct vec2i));
id = PORTAL_ID(iter.link->value.c, link1->value.c);
hmap_add(portals, (struct hmap_key) {.p = key},
(struct hmap_val) {.s = id});
}
}
static bool
get_portal(struct hmap *portals, struct vec2i *pos, struct vec2i *skip, size_t id)
{
struct hmap_iter iter;
for (HMAP_ITER(portals, iter)) {
if (skip && vec2i_eql(skip, iter.link->key.p))
continue;
if (iter.link->value.s == id) {
vec2i_set(pos, iter.link->key.p);
return true;
}
}
return false;
}
static void
add_djk_state(struct list *active, struct hmap *distmap,
struct hmap *portals, struct vec2i *pos, size_t dist)
{
struct hmap_link **hmap_linkp;
struct djk_state *nstate;
struct vec2i *key;
bool new;
hmap_linkp = hmap_link_pos(distmap, (struct hmap_key) {.p = pos});
if (!*hmap_linkp) {
key = memdup(pos, sizeof(struct vec2i));
*hmap_linkp = hmap_link_alloc(distmap,
(struct hmap_key) {.p = key},
(struct hmap_val) {.s = dist}, NULL);
new = true;
} else {
if (dist < (*hmap_linkp)->value.s) {
(*hmap_linkp)->value.s = dist;
new = true;
} else {
new = false;
}
}
if (new) {
nstate = malloc(sizeof(struct djk_state));
nstate->dist = dist;
vec2i_set(&nstate->pos, pos);
nstate->link = LIST_LINK_INIT;
list_insert_sorted(active, &nstate->link,
false, djk_dist_ordering,
LIST_OFFSET(struct djk_state, link), NULL);
}
}
static size_t
min_dist(struct hmap *map, struct hmap *portals,
struct vec2i *start, struct vec2i *end)
{
struct list active;
struct hmap distmap;
struct list_link *list_link;
struct hmap_link *hmap_link;
struct hmap_iter hmap_iter;
struct djk_state *state;
struct vec2i *key, npos;
size_t mdist, i;
list_init(&active);
hmap_init(&distmap, 256, vec2i_hmap_hash, vec2i_hmap_keycmp,
&stdlib_strict_heap_allocator);
state = malloc(sizeof(struct djk_state));
state->dist = 0;
vec2i_set(&state->pos, start);
list_insert_front(&active, &state->link);
key = memdup(start, sizeof(struct vec2i));
hmap_add(&distmap, (struct hmap_key) {.p = key},
(struct hmap_val) {.s = 0});
mdist = 0;
while (!list_empty(&active)) {
list_link = list_front(&active);
state = LIST_UPCAST(list_link, struct djk_state, link);
if (vec2i_eql(&state->pos, end)) {
mdist = state->dist;
goto exit;
}
list_link_pop(list_link);
hmap_link = hmap_get(&distmap, (struct hmap_key) {.p = &state->pos});
assert(hmap_link);
if (state->dist != hmap_link->value.s) {
free(state);
continue;
}
hmap_link = hmap_get(portals, (struct hmap_key) {.p = &state->pos});
if (hmap_link) {
if (get_portal(portals, &npos, &state->pos, hmap_link->value.s))
add_djk_state(&active, &distmap, portals,
&npos, state->dist + 1);
}
for (i = 0; i < 4; i++) {
vec2i_add(&npos, &state->pos, &adj[i]);
hmap_link = hmap_get(map, (struct hmap_key) {.p = &npos});
if (!hmap_link || hmap_link->value.c != '.') continue;
add_djk_state(&active, &distmap, portals,
&npos, state->dist + 1);
}
free(state);
}
exit:
for (HMAP_ITER(&distmap, hmap_iter))
free(hmap_iter.link->key._p);
hmap_deinit(&distmap);
list_free_items(&active, free, LIST_OFFSET(struct djk_state, link));
return mdist;
}
void
part1(void)
{
struct hmap map;
struct hmap portals;
struct hmap_iter iter;
struct vec2i start, end;
size_t answer;
hmap_init(&map, 256, vec2i_hmap_hash, vec2i_hmap_keycmp,
&stdlib_strict_heap_allocator);
hmap_init(&portals, 256, vec2i_hmap_hash, vec2i_hmap_keycmp,
&stdlib_strict_heap_allocator);
load_map(&map, aoc.input, aoc.input_size);
load_portals(&portals, &map);
assert(get_portal(&portals, &start, NULL, PORTAL_ID('A', 'A')));
assert(get_portal(&portals, &end, NULL, PORTAL_ID('Z', 'Z')));
answer = min_dist(&map, &portals, &start, &end);
aoc.answer = aprintf("%lu", answer);
aoc.solution = "656";
for (HMAP_ITER(&portals, iter))
free(iter.link->key._p);
hmap_deinit(&portals);
for (HMAP_ITER(&map, iter))
free(iter.link->key._p);
hmap_deinit(&map);
}
|