Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 100
- /**
- * Plan of action: Empty barrier, val -1 arround the entire thing
- * Starting at 9, do a recursive search until point 0 is found.
- * Return the sum of how many were found
- * Woops this returns all paths, not just how many 9's can be reached
- */
- void parse(FILE* file, int inputs[][SIZE], int* bounds) {
- int y = 1;
- int x = 1;
- char val;
- while ((val = fgetc(file)) != EOF) {
- if (val == '\n') {
- // End of this string of values
- y++;
- x = 1;
- continue;
- }
- inputs[y][x] = val - 48;
- x++;
- }
- y++;
- for (size_t i = 0; i < y; i++) {
- inputs[i][0] = -1;
- inputs[i][x] = -1;
- }
- x++;
- for (size_t i = 0; i < x; i++) {
- inputs[0][i] = -1;
- inputs[y][i] = -1;
- }
- bounds[0] = x;
- bounds[1] = y + 1;
- }
- int search_path(int locations[][SIZE], int x, int y, int prev_val) {
- int val = locations[y][x];
- if (prev_val - val != 1) {
- // Invalid
- return 0;
- }
- if (val == 0) {
- // Valid route
- return 1;
- }
- int directions[5] = { 1,0,-1,0,1 };
- int total = 0;
- for (size_t i = 0; i < 4; i++) {
- int n_x = x + directions[i];
- int n_y = y + directions[i + 1];
- total += search_path(locations, n_x, n_y, val);
- }
- return total;
- }
- int search_uniques(int locations[][SIZE], int found[][2], int* index, int x, int y, int prev_val) {
- int val = locations[y][x];
- if (val - prev_val != 1) {
- // Invalid
- return 0;
- }
- printf("Exploring to %d, %d\n", x, y);
- if (val == 9) {
- // Valid route
- // But check if it's already in the items
- for (size_t i = 0; i < *index; i++) {
- int dx = found[i][0];
- int dy = found[i][1];
- if (x == dx && y == dy) {
- // Not a valid route
- printf("%d, %d was already reachable\n", x, y);
- return 0;
- }
- }
- printf("%d, %d is a new reachable point\n", x, y);
- found[*index][0] = x;
- found[*index][1] = y;
- (*index)++;
- return 1;
- }
- int directions[5] = { 1,0,-1,0,1 };
- int total = 0;
- for (size_t i = 0; i < 4; i++) {
- int n_x = x + directions[i];
- int n_y = y + directions[i + 1];
- total += search_uniques(locations, found, index, n_x, n_y, val);
- }
- return total;
- }
- void print_map(int locations[][SIZE], int* bounds) {
- for (size_t y = 0; y < bounds[1]; y++) {
- for (size_t x = 0; x < bounds[0]; x++) {
- int loc = locations[y][x];
- printf("%c", loc + 48);
- }
- printf("\n");
- }
- }
- int part_one(int locations[][SIZE], int* bounds) {
- int total = 0;
- for (size_t y = 0; y < bounds[1]; y++) {
- for (size_t x = 0; x < bounds[0]; x++) {
- int loc = locations[y][x];
- if (loc == 0) {
- int found_places[SIZE][2] = { 0 };
- int index = 0;
- total += search_uniques(locations, found_places, &index, x, y, -1);
- }
- }
- }
- return total;
- }
- int part_two(int locations[][SIZE], int* bounds) {
- int total = 0;
- for (size_t y = 0; y < bounds[1]; y++) {
- for (size_t x = 0; x < bounds[0]; x++) {
- int loc = locations[y][x];
- if (loc == 9) {
- total += search_path(locations, x, y, 10);
- }
- }
- }
- return total;
- }
- int main() {
- FILE* ptr = fopen("2024-10.txt", "r");
- int bounds[2] = { 0 };
- int locations[SIZE][SIZE] = { 0 };
- parse(ptr, locations, bounds);
- print_map(locations, bounds);
- printf("Part one: %d\n", part_one(locations, bounds));
- printf("Part two: %d\n", part_two(locations, bounds));
- }
Advertisement
Add Comment
Please, Sign In to add comment