BlueRains

AdventOfCode C 2024 day 10

Dec 10th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 100
  3.  
  4. /**
  5.  * Plan of action: Empty barrier, val -1 arround the entire thing
  6.  * Starting at 9, do a recursive search until point 0 is found.
  7.  * Return the sum of how many were found
  8.  * Woops this returns all paths, not just how many 9's can be reached
  9.  */
  10.  
  11. void parse(FILE* file, int inputs[][SIZE], int* bounds) {
  12.     int y = 1;
  13.     int x = 1;
  14.     char val;
  15.     while ((val = fgetc(file)) != EOF) {
  16.         if (val == '\n') {
  17.             // End of this string of values
  18.             y++;
  19.             x = 1;
  20.             continue;
  21.         }
  22.         inputs[y][x] = val - 48;
  23.         x++;
  24.     }
  25.     y++;
  26.     for (size_t i = 0; i < y; i++) {
  27.         inputs[i][0] = -1;
  28.         inputs[i][x] = -1;
  29.     }
  30.  
  31.     x++;
  32.     for (size_t i = 0; i < x; i++) {
  33.         inputs[0][i] = -1;
  34.         inputs[y][i] = -1;
  35.     }
  36.  
  37.     bounds[0] = x;
  38.     bounds[1] = y + 1;
  39. }
  40.  
  41.  
  42. int search_path(int locations[][SIZE], int x, int y, int prev_val) {
  43.     int val = locations[y][x];
  44.     if (prev_val - val != 1) {
  45.         // Invalid
  46.         return 0;
  47.     }
  48.     if (val == 0) {
  49.         // Valid route
  50.         return 1;
  51.     }
  52.     int directions[5] = { 1,0,-1,0,1 };
  53.     int total = 0;
  54.     for (size_t i = 0; i < 4; i++) {
  55.         int n_x = x + directions[i];
  56.         int n_y = y + directions[i + 1];
  57.         total += search_path(locations, n_x, n_y, val);
  58.     }
  59.     return total;
  60. }
  61.  
  62. int search_uniques(int locations[][SIZE], int found[][2], int* index, int x, int y, int prev_val) {
  63.     int val = locations[y][x];
  64.     if (val - prev_val != 1) {
  65.         // Invalid
  66.         return 0;
  67.     }
  68.     printf("Exploring to %d, %d\n", x, y);
  69.     if (val == 9) {
  70.         // Valid route
  71.         // But check if it's already in the items
  72.         for (size_t i = 0; i < *index; i++) {
  73.             int dx = found[i][0];
  74.             int dy = found[i][1];
  75.             if (x == dx && y == dy) {
  76.                 // Not a valid route
  77.                 printf("%d, %d was already reachable\n", x, y);
  78.                 return 0;
  79.             }
  80.         }
  81.         printf("%d, %d is a new reachable point\n", x, y);
  82.         found[*index][0] = x;
  83.         found[*index][1] = y;
  84.         (*index)++;
  85.         return 1;
  86.     }
  87.     int directions[5] = { 1,0,-1,0,1 };
  88.     int total = 0;
  89.     for (size_t i = 0; i < 4; i++) {
  90.         int n_x = x + directions[i];
  91.         int n_y = y + directions[i + 1];
  92.         total += search_uniques(locations, found, index, n_x, n_y, val);
  93.     }
  94.     return total;
  95. }
  96.  
  97. void print_map(int locations[][SIZE], int* bounds) {
  98.     for (size_t y = 0; y < bounds[1]; y++) {
  99.         for (size_t x = 0; x < bounds[0]; x++) {
  100.             int loc = locations[y][x];
  101.             printf("%c", loc + 48);
  102.  
  103.         }
  104.         printf("\n");
  105.     }
  106.  
  107. }
  108.  
  109. int part_one(int locations[][SIZE], int* bounds) {
  110.     int total = 0;
  111.     for (size_t y = 0; y < bounds[1]; y++) {
  112.         for (size_t x = 0; x < bounds[0]; x++) {
  113.             int loc = locations[y][x];
  114.             if (loc == 0) {
  115.                 int found_places[SIZE][2] = { 0 };
  116.                 int index = 0;
  117.                 total += search_uniques(locations, found_places, &index, x, y, -1);
  118.             }
  119.         }
  120.     }
  121.     return total;
  122. }
  123. int part_two(int locations[][SIZE], int* bounds) {
  124.     int total = 0;
  125.     for (size_t y = 0; y < bounds[1]; y++) {
  126.         for (size_t x = 0; x < bounds[0]; x++) {
  127.             int loc = locations[y][x];
  128.             if (loc == 9) {
  129.                 total += search_path(locations, x, y, 10);
  130.             }
  131.         }
  132.     }
  133.     return total;
  134. }
  135.  
  136.  
  137. int main() {
  138.     FILE* ptr = fopen("2024-10.txt", "r");
  139.     int bounds[2] = { 0 };
  140.     int locations[SIZE][SIZE] = { 0 };
  141.     parse(ptr, locations, bounds);
  142.     print_map(locations, bounds);
  143.     printf("Part one: %d\n", part_one(locations, bounds));
  144.     printf("Part two: %d\n", part_two(locations, bounds));
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment