advent-of-code-2021/15/15-1.c

126 lines
3.4 KiB
C
Raw Normal View History

2021-12-17 16:34:38 +08:00
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
2021-12-17 17:14:16 +08:00
#include <limits.h>
#include "../check_alloc.h"
2021-12-17 16:34:38 +08:00
#define PART_2
2021-12-17 17:14:16 +08:00
#define TILE_SIZE 100
#ifdef PART_2
#define REPEAT_TILE 5
#define SPACE_SIZE (TILE_SIZE * REPEAT_TILE)
#else
#define SPACE_SIZE TILE_SIZE
#endif
2021-12-17 16:34:38 +08:00
typedef struct
{
int risk;
int min_risk_path;
bool explored;
} CELL;
typedef CELL **SPACE;
#define VALID_POSITION(x, y) (x < SPACE_SIZE && x >= 0 && y < SPACE_SIZE && y >= 0)
#define END(x, y) (x == SPACE_SIZE - 1 && y == SPACE_SIZE - 1)
void print_space(SPACE space)
{
for (size_t i = 0; i < SPACE_SIZE; i++)
{
for (size_t j = 0; j < SPACE_SIZE; j++)
2021-12-17 17:14:16 +08:00
{
2021-12-17 16:34:38 +08:00
// printf("%d(%c,%d)", space[i][j].risk, space[i][j].explored ? '*' : '.', space[i][j].min_risk_path);
2021-12-17 17:14:16 +08:00
// printf("%d(%d)", space[i][j].risk, space[i][j].min_risk_path);
printf("%d", space[i][j].risk);
if (j % TILE_SIZE == TILE_SIZE - 1)
putchar(' ');
}
2021-12-17 16:34:38 +08:00
putchar('\n');
2021-12-17 17:14:16 +08:00
if (i % TILE_SIZE == TILE_SIZE - 1)
putchar('\n');
2021-12-17 16:34:38 +08:00
}
putchar('\n');
}
2021-12-17 17:14:16 +08:00
void node_risk(SPACE space, int x, int y, int risk, bool *modified)
2021-12-17 16:34:38 +08:00
{
if (!VALID_POSITION(x, y))
return;
risk += space[x][y].risk;
if (risk < space[x][y].min_risk_path)
2021-12-17 17:14:16 +08:00
{
2021-12-17 16:34:38 +08:00
space[x][y].min_risk_path = risk;
2021-12-17 17:14:16 +08:00
*modified = true;
}
2021-12-17 16:34:38 +08:00
}
int main(int argc, char const *argv[])
{
FILE *p_file = fopen("input", "r");
2021-12-17 17:14:16 +08:00
CHECK_ALLOC(p_file);
2021-12-17 16:34:38 +08:00
SPACE space = calloc(SPACE_SIZE, sizeof(CELL *));
2021-12-17 17:14:16 +08:00
CHECK_ALLOC(space);
2021-12-17 16:34:38 +08:00
for (size_t i = 0; i < SPACE_SIZE; i++)
{
space[i] = calloc(SPACE_SIZE, sizeof(CELL));
2021-12-17 17:14:16 +08:00
CHECK_ALLOC(space[i]);
}
for (size_t i = 0; i < TILE_SIZE; i++)
{
char buf[BUFSIZ];
2021-12-17 16:34:38 +08:00
fgets(buf, sizeof buf, p_file);
2021-12-17 17:14:16 +08:00
for (size_t j = 0; j < TILE_SIZE; j++)
2021-12-17 16:34:38 +08:00
{
space[i][j].risk = buf[j] - '0';
space[i][j].explored = false;
2021-12-17 17:14:16 +08:00
space[i][j].min_risk_path = INT_MAX;
2021-12-17 16:34:38 +08:00
}
}
2021-12-17 17:14:16 +08:00
// EXPAND SPACE
#ifdef PART_2
for (size_t i = 0; i < SPACE_SIZE; i++)
{
for (size_t j = 0; j < SPACE_SIZE; j++)
{
if (i < TILE_SIZE && j < TILE_SIZE)
continue; // IGNORE START TILE
int new_risk = space[i % TILE_SIZE][j % TILE_SIZE].risk + i / TILE_SIZE + j / TILE_SIZE;
space[i][j].risk = new_risk < 10 ? new_risk : new_risk % 9;
space[i][j].explored = false;
space[i][j].min_risk_path = INT_MAX;
}
}
#endif
2021-12-17 16:34:38 +08:00
space[0][0].min_risk_path = 0;
2021-12-17 17:14:16 +08:00
bool modified;
do
2021-12-17 16:34:38 +08:00
{
2021-12-17 17:14:16 +08:00
modified = false;
2021-12-17 16:34:38 +08:00
for (size_t c = 0; c < SPACE_SIZE * 2; c++)
{
for (size_t x = 0; x <= c; x++)
{
int y = c - x;
if (VALID_POSITION(x, y))
{
space[x][y].explored = true;
2021-12-17 17:14:16 +08:00
node_risk(space, x + 1, y, space[x][y].min_risk_path, &modified);
node_risk(space, x, y + 1, space[x][y].min_risk_path, &modified);
node_risk(space, x - 1, y, space[x][y].min_risk_path, &modified);
node_risk(space, x, y - 1, space[x][y].min_risk_path, &modified);
2021-12-17 16:34:38 +08:00
}
}
}
2021-12-17 17:14:16 +08:00
} while (modified); // What the fuck? Brute forced it
2021-12-17 16:34:38 +08:00
printf("Minimum risk %d\n", space[SPACE_SIZE - 1][SPACE_SIZE - 1].min_risk_path);
return 0;
}