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
|
#include "aoc.h"
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXA(a, b) (ABS(a) > ABS(b) ? (a) : (b))
#define MINA(a, b) (ABS(a) > ABS(b) ? (b) : (a))
struct line {
int sx, sy;
int ex, ey;
struct line *next;
};
struct line *
parse_lines(const char **pos, const char *end)
{
char buf[2048], part[64];
const char *lpos, *lend;
struct line *line, *start, **iter;
int x, y, val;
aoc_debug("START\n");
if (!readtok(buf, sizeof(buf), '\n', pos, end))
die("parse_lines: end of file");
start = line = NULL;
iter = &line;
x = y = 0;
lpos = buf;
lend = buf + strlen(buf);
while (readtok(part, sizeof(part), ',', &lpos, lend)) {
*iter = line = malloc(sizeof(struct line));
if (!line) die("parse_lines: oom");
if (!start) start = line;
line->next = NULL;
line->sx = x;
line->sy = y;
val = (int) parsei64(part+1);
switch (part[0]) {
case 'L':
x -= val;
break;
case 'R':
x += val;
break;
case 'D':
y -= val;
break;
case 'U':
y += val;
break;
default:
die("parse_lines: invalid move: %s", part);
}
aoc_debug("%i %i\n", x, y);
line->ex = x;
line->ey = y;
iter = &(*iter)->next;
}
aoc_debug("END\n");
return start;
}
void
free_lines(struct line *line)
{
struct line *next;
while (line) {
next = line->next;
free(line);
line = next;
}
}
int
do_intersect(struct line *l1, struct line *l2)
{
int inx, iny;
inx = MAX(l1->sx, l1->ex) >= MIN(l2->sx, l2->ex)
&& MAX(l2->sx, l2->ex) >= MIN(l1->sx, l1->ex);
iny = MAX(l1->sy, l1->ey) >= MIN(l2->sy, l2->ey)
&& MAX(l2->sy, l2->ey) >= MIN(l1->sy, l1->ey);
return inx && iny;
}
void
intersection_near_origin(struct line *l1, struct line *l2, int *x, int *y)
{
if (l1->sx == l1->ex && l2->sx == l2->ex) {
*x = l1->sx;
*y = MAXA(
MINA(l1->sy, l1->ey),
MINA(l2->sy, l2->ey)
);
} else if (l1->sy == l1->ey && l2->sy == l2->ey) {
*y = l1->sy;
*x = MAXA(
MINA(l1->sx, l1->ex),
MINA(l2->sy, l2->ey)
);
} else {
*x = (l1->sx == l1->ex) ? l1->sx : l2->sx;
*y = (l1->sy == l1->ey) ? l1->sy : l2->sy;
}
}
void
intersection_least_steps(struct line *l1, struct line *l2, int *x, int *y)
{
int in1, in2;
if (l1->sx == l1->ex && l2->sx == l2->ex) {
/* if both ends or both starts of lines are part of the intersection,
* any one of them will give the same best dist,
* otherwise the start that intersects is the best fit */
*x = l1->sx;
in1 = l1->sy <= MAX(l2->sy, l2->ey) && l1->sy >= MIN(l2->sy, l2->ey);
in2 = l2->sy <= MAX(l1->sy, l1->ey) && l2->sy >= MIN(l1->sy, l1->ey);
*y = in1 ? l1->sy : (in2 ? l2->sy : l2->ey);
} else if (l1->sy == l1->ey && l2->sy == l2->ey) {
/* same for x-axis */
*y = l1->sy;
in1 = l1->sx <= MAX(l2->sx, l2->ex) && l1->sx >= MIN(l2->sx, l2->ex);
in2 = l2->sx <= MAX(l1->sx, l1->ex) && l2->sx >= MIN(l1->sx, l1->ex);
*x = in1 ? l1->sx : (in2 ? l2->sx : l2->ex);
} else {
*x = (l1->sx == l1->ex) ? l1->sx : l2->sx;
*y = (l1->sy == l1->ey) ? l1->sy : l2->sy;
}
}
int
calc_dist(struct line *lines, struct line *end, int x, int y)
{
struct line *iter;
int dist;
dist = 0;
for (iter = lines; iter != end; iter = iter->next) {
dist += ABS(iter->ex - iter->sx);
dist += ABS(iter->ey - iter->sy);
aoc_debug("D: %i\n", dist);
}
dist += ABS(x - iter->sx);
dist += ABS(y - iter->sy);
aoc_debug("D: %i\n", dist);
return dist;
}
void
part1(void)
{
struct line *lines1, *lines2;
struct line *iter1, *iter2;
const char *pos, *end;
int nx, ny, hit;
int x, y;
pos = aoc.input;
end = aoc.input + aoc.input_size;
lines1 = parse_lines(&pos, end);
lines2 = parse_lines(&pos, end);
x = y = 0;
for (iter1 = lines1; iter1; iter1 = iter1->next) {
for (iter2 = lines2; iter2; iter2 = iter2->next) {
hit = do_intersect(iter1, iter2);
if (!hit) continue;
intersection_near_origin(iter1, iter2, &nx, &ny);
if (!x && !y || ABS(nx) + ABS(ny) < ABS(x) + ABS(y)) {
x = nx;
y = ny;
}
}
}
aoc_debug("dist(%i, %i) = %i\n", x, y, ABS(x) + ABS(y));
aoc.answer = aprintf("%i", ABS(x) + ABS(y));
aoc.solution = "209";
free_lines(lines1);
free_lines(lines2);
}
void
part2(void)
{
struct line *lines1, *lines2;
struct line *iter1, *iter2;
const char *pos, *end;
int nx, ny, nd, hit;
int x, y, d;
pos = aoc.input;
end = aoc.input + aoc.input_size;
lines1 = parse_lines(&pos, end);
lines2 = parse_lines(&pos, end);
x = y = 0;
for (iter1 = lines1; iter1; iter1 = iter1->next) {
for (iter2 = lines2; iter2; iter2 = iter2->next) {
hit = do_intersect(iter1, iter2);
if (!hit) continue;
intersection_least_steps(iter1, iter2, &nx, &ny);
nd = calc_dist(lines1, iter1, nx, ny);
nd += calc_dist(lines2, iter2, nx, ny);
if (!x && !y || nd < d) {
d = nd;
x = nx;
y = ny;
}
}
}
aoc_debug("steps(%i, %i) = %i\n", x, y, d);
aoc.answer = aprintf("%i", d);
aoc.solution = "43258";
free_lines(lines1);
free_lines(lines2);
}
|