Advertisement
Vulpes-Vulpeos

AoC 2024 day 5

Dec 5th, 2024
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. int main(){
  6.     FILE *fptr = fopen("/media/int_drive/Development/c/AoC/2024/05/input.txt", "r");
  7.     if (fptr == NULL) { printf("Failed to open the input file"); return 1; };
  8.    
  9.     // Count number of rules and updates
  10.     int rctr = 0, uctr = 0;
  11.     char buf[1024];
  12.     while(fgets(buf, sizeof(buf), fptr)){ if (strchr(buf, '|')){ ++rctr; } else if (strchr(buf, ',') ) { ++uctr; }; };
  13.     printf("Rules: %d Updates: %d\n", rctr, uctr);
  14.     // Reseting position in file
  15.     rewind(fptr);
  16.     // Creating arrays to store rules pairs
  17.     int rarr1[rctr], rarr2[rctr];
  18.     // Parsing rules into arrays
  19.     int num1 = 0, num2 = 0, index = 0;
  20.     while(fscanf(fptr, "%d|%d", &num1, &num2) == 2){ rarr1[index] = num1; rarr2[index] = num2; ++index; };
  21.     fseek(fptr, 6*sizeof(char)*rctr, SEEK_SET); // Skipping empty line
  22.     // Parsing updates into 2D array
  23.     int uarr[uctr][23], uarr_bad[uctr];
  24.     int nums[23] = {0}, bad_index = 0;
  25.     index = 0;
  26.     while(fscanf(fptr, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
  27.                  &nums[0],  &nums[1],  &nums[2],  &nums[3],  &nums[4],  &nums[5],  &nums[6],  &nums[7],
  28.                  &nums[8],  &nums[9],  &nums[10], &nums[11], &nums[12], &nums[13], &nums[14], &nums[15],
  29.                  &nums[16], &nums[17], &nums[18], &nums[19], &nums[20], &nums[21], &nums[22]) > 0){
  30.         for (int i = 0; i < 23; ++i){uarr[index][i] = nums[i]; };
  31.         ++index;
  32.         memset(nums, 0, 23 * sizeof(int)); // Reset nums values to 0
  33.     };
  34.     fclose(fptr);
  35.    
  36.     // Print parsing results
  37.     // for (int i = 0; i < rctr; ++i){ printf("%d|%d\n", rarr1[i], rarr2[i]); }; putchar('\n');
  38.     // for (int i = 0; i < uctr; ++i){ for (int j = 0; j < 23; ++j){printf("%d, ", uarr[i][j]); }; putchar('\n'); };
  39.    
  40.     // PART 1
  41.     int mid_sum = 0;
  42.     // Go rule one by one and check if second number is before first
  43.     for (int i = 0; i < uctr; ++i){                   // updates row
  44.         int good = 1;
  45.         for (int j = 0; j < 23; ++j){                 // updates elements
  46.             if (uarr[i][j] == 0){ break; };           // go to next row if 0
  47.             for (int k = 0; k < rctr; ++k){           // check each rule
  48.                 if (uarr[i][j] == rarr1[k]){          // compare update number to current rule number
  49.                     //printf("Testing rule: %d|%d on update #%d\n", rarr1[k], rarr2[k], i+1);
  50.                     for (int l = 0; l < j; ++l){
  51.                         if (uarr[i][l] == 0){break; };
  52.                         if (uarr[i][l] == rarr2[k]){good = 0;  break;}; // if second rule number is before current number then report bad
  53.                     };
  54.                 };
  55.             };
  56.         };
  57.         if (good){ // Add middle number to the sum
  58.             int last_num_index = 0;
  59.             for (int m = 0; m < 23; ++m){ if (uarr[i][m] != 0){++last_num_index;}; };
  60.             mid_sum += uarr[i][last_num_index / 2];
  61.             //printf("Update #%d is good. Middle index: %d\n", i+1, last_num_index / 2);
  62.         } else {uarr_bad[bad_index++] = i; };
  63.     };
  64.    
  65.     printf("Sum of middle numbers of good reports: %d\n", mid_sum);
  66.     printf("Number of bad reportes reports: %d\n", bad_index);
  67.      
  68.     // PART 2
  69.     mid_sum = 0;
  70.     int while_index = 0;
  71.     // Go rule one by one and check if second number is before first
  72.     for (int i = 0; i < bad_index; ++i){                        // updates row
  73.         int bad = 1;
  74.         for (int j = 0; j < 23; ++j){                           // updates elements
  75.             if (uarr[uarr_bad[i]][j] == 0){ break; };           // go to next row if 0
  76.             for (int k = 0; k < rctr; ++k){                     // check each rule
  77.                 if (uarr[uarr_bad[i]][j] == rarr1[k]){          // compare update number to current rule number
  78.                     for (int l = 0; l < j; ++l){
  79.                         if (uarr[uarr_bad[i]][l] == 0){bad = 0; break; };
  80.                         if (uarr[uarr_bad[i]][l] == rarr2[k]){  // if second rule number is before current number just swap them
  81.                             int temp = uarr[uarr_bad[i]][j];
  82.                             uarr[uarr_bad[i]][j] = uarr[uarr_bad[i]][l];
  83.                             uarr[uarr_bad[i]][l] = temp;
  84.                             //printf("After swap: %d, %d, %d, %d, %d\n",  uarr[uarr_bad[i]][0], uarr[uarr_bad[i]][1], uarr[uarr_bad[i]][2], uarr[uarr_bad[i]][3], uarr[uarr_bad[i]][4]);
  85.                             j = 0;                             // Go back untill fixed
  86.                             break;
  87.                         };
  88.                     };
  89.                 };
  90.             };
  91.         };
  92.  
  93.         int last_num_index = 0;
  94.         for (int m = 0; m < 23; ++m){ if (uarr[uarr_bad[i]][m] != 0){++last_num_index;}; };
  95.         mid_sum += uarr[uarr_bad[i]][last_num_index / 2];
  96.         //printf("Update #%d is good. Middle index: %d (%d) | %d %d %d %d %d \n", i+1, last_num_index / 2, uarr[uarr_bad[i]][last_num_index / 2],
  97.         //        uarr[uarr_bad[i]][0], uarr[uarr_bad[i]][1], uarr[uarr_bad[i]][2], uarr[uarr_bad[i]][3], uarr[uarr_bad[i]][4]);
  98.  
  99.     };
  100.    
  101.     printf("Sum of middle numbers of previously bad reports: %d\n", mid_sum);
  102.  
  103.     return 0;
  104. }
  105.  
Tags: AoC2024
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement