AdventOfCode/2019/days/3/2/main.c

68 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#define POINTLIMIT 200000
int main(void) {
signed int lines[2][POINTLIMIT][2];
unsigned int bufpos = 0, pp = 0, linep[2];
short line = 0, bufls = 0, res = -1;
char numbuf[16], dir, c;
fprintf(stderr,"\e[1;31mParsing input...\n");
while ((c=getchar())!=EOF) {
if (c >= 'A' && c <= 'Z') {
dir = c;
} else if (c == ',' || c == '\n') {
numbuf[bufpos] = '\0';
for (int i = 0; i<atoi(numbuf); i++) {
int start = 0;
if (pp>1) {
start = pp-1;
}
lines[line][pp][0] = lines[line][start][0];
lines[line][pp][1] = lines[line][start][1];
int va,val;
switch (dir) {
case 'U': va = 1, val=1; break;
case 'R': va = 0, val=1; break;
case 'D': va = 1, val=-1; break;
case 'L': va = 0, val=-1; break;
}
lines[line][pp][va] = lines[line][start][va]+val;
pp++;
}
if (c == '\n') {
linep[line]=pp-1;
line++;
pp = 0;
}
bufpos = 0;
bufls = 0;
} else if (c >= '0' && c <= '9') {
numbuf[bufpos++] = c;
bufls = 1;
} else {
return 1;
}
}
fprintf(stderr,"\nFinding closest match...\n");
int poi[POINTLIMIT][2][2], poip; // Comparasion buffer
for (int x=0, m=0; x<linep[0] && x<POINTLIMIT; x++) {
for (int y=0; y<linep[1] && y<POINTLIMIT; y++) {
if (
lines[0][x][0] == lines[1][y][0] &&
lines[0][x][1] == lines[1][y][1]
) {
poi[poip][0][0] = lines[0][x][0]; // Positions of the points
poi[poip][0][1] = lines[0][x][1];
poi[poip][1][0] = x; // Steps of points
poi[poip++][1][1] = y;
}
}
}
free(lines); // We don't need unimportant points anymore
fprintf(stderr,"\nBingo:\n");
printf("%i\n", res);
return 0;
}