C 2024 day 18 part 2
This commit is contained in:
parent
1449f587a4
commit
62ae713bd2
@ -19,6 +19,51 @@ typedef struct Grid {
|
|||||||
Node grid[DIMENSION][DIMENSION];
|
Node grid[DIMENSION][DIMENSION];
|
||||||
} Grid;
|
} Grid;
|
||||||
|
|
||||||
|
void grid_dijkstra(Grid *g) {
|
||||||
|
Node* queue[QUEUE_LENGTH];
|
||||||
|
queue[0] = &(g->grid[0][0]);
|
||||||
|
int queue_start = 0;
|
||||||
|
int queue_end = 1;
|
||||||
|
|
||||||
|
g->grid[0][0].c = '*';
|
||||||
|
g->grid[0][0].cost = 0;
|
||||||
|
|
||||||
|
while (queue_start != queue_end) {
|
||||||
|
// get first element of queue
|
||||||
|
Node* n = queue[queue_start++];
|
||||||
|
queue_start %= QUEUE_LENGTH;
|
||||||
|
n->c = ' ';
|
||||||
|
// add its neighbors to the queue
|
||||||
|
if (n->x - 1 >= 0 && g->grid[n->x - 1][n->y].c == '.') {
|
||||||
|
g->grid[n->x - 1][n->y].c = '*';
|
||||||
|
g->grid[n->x - 1][n->y].cost = n->cost + 1;
|
||||||
|
queue[queue_end++] = &(g->grid[n->x - 1][n->y]);
|
||||||
|
queue_end %= 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n->y - 1 >= 0 && g->grid[n->x][n->y - 1].c == '.') {
|
||||||
|
g->grid[n->x][n->y - 1].c = '*';
|
||||||
|
g->grid[n->x][n->y - 1].cost = n->cost + 1;
|
||||||
|
queue[queue_end++] = &(g->grid[n->x][n->y - 1]);
|
||||||
|
queue_end %= 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n->x + 1 < DIMENSION && g->grid[n->x + 1][n->y].c == '.') {
|
||||||
|
g->grid[n->x + 1][n->y].c = '*';
|
||||||
|
g->grid[n->x + 1][n->y].cost = n->cost + 1;
|
||||||
|
queue[queue_end++] = &(g->grid[n->x + 1][n->y]);
|
||||||
|
queue_end %= 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n->y + 1 < DIMENSION && g->grid[n->x][n->y + 1].c == '.') {
|
||||||
|
g->grid[n->x][n->y + 1].c = '*';
|
||||||
|
g->grid[n->x][n->y + 1].cost = n->cost + 1;
|
||||||
|
queue[queue_end++] = &(g->grid[n->x][n->y + 1]);
|
||||||
|
queue_end %= 200;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *line;
|
char *line;
|
||||||
Grid g;
|
Grid g;
|
||||||
@ -33,66 +78,30 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int x, y;
|
int x, y;
|
||||||
int i = 0;
|
|
||||||
|
//while ((line = aoc_read_line()) != NULL) {
|
||||||
|
for (int i = 0; i < 1024; i++) {
|
||||||
|
line = aoc_read_line();
|
||||||
|
sscanf(line, "%d,%d", &x, &y);
|
||||||
|
g.grid[x][y].c = '#';
|
||||||
|
}
|
||||||
|
|
||||||
|
Grid original_grid;
|
||||||
|
memcpy(&original_grid, &g, sizeof(Grid));
|
||||||
|
|
||||||
|
grid_dijkstra(&g);
|
||||||
|
|
||||||
|
printf("Part 1: %d\n", g.grid[DIMENSION - 1][DIMENSION - 1].cost);
|
||||||
|
|
||||||
while ((line = aoc_read_line()) != NULL) {
|
while ((line = aoc_read_line()) != NULL) {
|
||||||
sscanf(line, "%d,%d", &x, &y);
|
sscanf(line, "%d,%d", &x, &y);
|
||||||
if (i++ < 1024) g.grid[x][y].c = '#';
|
original_grid.grid[x][y].c = '#';
|
||||||
|
memcpy(&g, &original_grid, sizeof(Grid));
|
||||||
|
grid_dijkstra(&g);
|
||||||
|
if (g.grid[DIMENSION - 1][DIMENSION - 1].cost == -1) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* queue[QUEUE_LENGTH];
|
printf("Part 2: %d,%d\n", x, y);
|
||||||
queue[0] = &(g.grid[0][0]);
|
|
||||||
int queue_start = 0;
|
|
||||||
int queue_end = 1;
|
|
||||||
|
|
||||||
g.grid[0][0].c = '*';
|
|
||||||
g.grid[0][0].cost = 0;
|
|
||||||
|
|
||||||
while (queue_start != queue_end) {
|
|
||||||
// get first element of queue
|
|
||||||
Node* n = queue[queue_start++];
|
|
||||||
queue_start %= QUEUE_LENGTH;
|
|
||||||
n->c = ' ';
|
|
||||||
// add its neighbors to the queue
|
|
||||||
if (n->x - 1 >= 0 && g.grid[n->x - 1][n->y].c == '.') {
|
|
||||||
g.grid[n->x - 1][n->y].c = '*';
|
|
||||||
g.grid[n->x - 1][n->y].cost = n->cost + 1;
|
|
||||||
queue[queue_end++] = &(g.grid[n->x - 1][n->y]);
|
|
||||||
queue_end %= 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n->y - 1 >= 0 && g.grid[n->x][n->y - 1].c == '.') {
|
|
||||||
g.grid[n->x][n->y - 1].c = '*';
|
|
||||||
g.grid[n->x][n->y - 1].cost = n->cost + 1;
|
|
||||||
queue[queue_end++] = &(g.grid[n->x][n->y - 1]);
|
|
||||||
queue_end %= 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n->x + 1 < DIMENSION && g.grid[n->x + 1][n->y].c == '.') {
|
|
||||||
g.grid[n->x + 1][n->y].c = '*';
|
|
||||||
g.grid[n->x + 1][n->y].cost = n->cost + 1;
|
|
||||||
queue[queue_end++] = &(g.grid[n->x + 1][n->y]);
|
|
||||||
queue_end %= 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n->y + 1 < DIMENSION && g.grid[n->x][n->y + 1].c == '.') {
|
|
||||||
g.grid[n->x][n->y + 1].c = '*';
|
|
||||||
g.grid[n->x][n->y + 1].cost = n->cost + 1;
|
|
||||||
queue[queue_end++] = &(g.grid[n->x][n->y + 1]);
|
|
||||||
queue_end %= 200;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
for (int i = 0; i < DIMENSION; i++) {
|
|
||||||
for (int j = 0; j < DIMENSION; j++) {
|
|
||||||
printf("%c", g.grid[j][i].c);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
printf("Part 1: %d", g.grid[DIMENSION - 1][DIMENSION - 1].cost);
|
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
Loading…
Reference in New Issue
Block a user