Advertisement
Vulpes-Vulpeos

AoC 2024 day 6

Dec 6th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. enum directions {UP, RIGHT, DOWN, LEFT};
  5.  
  6. int main(){
  7.     FILE *fptr = fopen("/media/int_drive/Development/c/AoC/2024/06/input.txt", "r");
  8.     if (fptr == NULL) { printf("Failed to open the input file"); return 1; };
  9.    
  10.     // Count number of lines and number of columns
  11.     int rctr = 0, cctr = 0;
  12.     char buf[1024];
  13.     while(fgets(buf, sizeof(buf), fptr)){ ++rctr; if(cctr == 0){cctr = strlen(buf)-1; }; };
  14.     printf("Field size: %dx%d (rows x columns)\n", rctr, cctr);
  15.     rewind(fptr);
  16.     // Copy field into 2D array
  17.     char field[rctr][cctr];
  18.     int row = 0, col = 0, x = -1, y =-1;
  19.     while(!feof(fptr)) {
  20.         char ch = fgetc(fptr);
  21.         switch(ch) {
  22.             case '\n': ++row; col = 0; break;
  23.             case '^': x = row, y = col; field[row][col++] = '.'; break;
  24.             default: field[row][col++] = ch; break;
  25.         };
  26.     };
  27.     fclose(fptr);
  28.  
  29.     printf("Starting coordinates: %dx%d (row x column)\n", x, y);
  30.     //for (int i = 0; i < rctr; ++i){ for (int j = 0; j < cctr; ++j) { putchar(field[i][j]); }; putchar('\n'); };
  31.     // PART 1
  32.     // Moving guard and counting steps
  33.     int direction = UP, positions = 0, wctr = 0;
  34.     int p1x = x, p1y = y;
  35.     while(1){
  36.         if (p1x < 0 || p1y < 0 || p1x >= rctr || p1y >= cctr){break; };
  37.         if (field[p1x][p1y] == '.') { ++positions; field[p1x][p1y] = 'X'; };
  38.         switch(direction){
  39.             case UP:    { (field[p1x-1][p1y] == '#') ? (direction = RIGHT) : (--p1x); break; };
  40.             case RIGHT: { (field[p1x][p1y+1] == '#') ? (direction = DOWN)  : (++p1y); break; };
  41.             case DOWN:  { (field[p1x+1][p1y] == '#') ? (direction = LEFT)  : (++p1x); break; };
  42.             case LEFT:  { (field[p1x][p1y-1] == '#') ? (direction = UP)    : (--p1y); break; };
  43.         };
  44.  
  45.         if (++wctr > 10000){printf("Breaking infinite while loop!."); break;};
  46.     };
  47.    
  48.     printf("Total number of distinct positions: %d (number of loops: %d)\n", positions, wctr);
  49.  
  50.     // PART 2
  51.     positions = 0;
  52.     int p2x = x, p2y = y;
  53.     for (int row = 0; row < rctr; ++row){ for (int col = 0; col < cctr; ++col){ if (field[row][col] == 'X'){ field[row][col] = '.'; }; }; }; // Removing 'X' from part 1
  54.     //for (int i = 0; i < rctr; ++i){ for (int j = 0; j < cctr; ++j) { putchar(field[i][j]); }; putchar('\n'); };
  55.    
  56.     for (int row = 0; row < rctr; ++row){ for (int col = 0; col < cctr; ++col){ // Looping through rows and cols
  57.             if(row == x && col == y) { continue; };
  58.             if(field[row][col] == '.'){ field[row][col] = '#'; } else { continue; }; // Change cell to '#' only if it is possible
  59.             wctr = 0, p2x = x, p2y = y, direction = UP;
  60.             while(1){
  61.                 if (p2x < 0 || p2y < 0 || p2x >= rctr || p2y >= cctr){ break; };
  62.                 switch(direction){
  63.                     case UP:    { (field[p2x-1][p2y] == '#') ? (direction = RIGHT) : (--p2x); break; };
  64.                     case RIGHT: { (field[p2x][p2y+1] == '#') ? (direction = DOWN)  : (++p2y); break; };
  65.                     case DOWN:  { (field[p2x+1][p2y] == '#') ? (direction = LEFT)  : (++p2x); break; };
  66.                     case LEFT:  { (field[p2x][p2y-1] == '#') ? (direction = UP)    : (--p2y); break; };
  67.                 };
  68.  
  69.                 if (++wctr > 10000){ ++positions; break; };
  70.             };
  71.            
  72.             field[row][col] = '.'; // Removing added obstruction for next loop
  73.     }; };
  74.    
  75.     printf("Total number of obstruction positions: %d\n", positions);
  76.    
  77.     return 0;
  78. }
  79.  
Tags: AoC2024
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement