Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- enum directions {UP, RIGHT, DOWN, LEFT};
- int main(){
- FILE *fptr = fopen("/media/int_drive/Development/c/AoC/2024/06/input.txt", "r");
- if (fptr == NULL) { printf("Failed to open the input file"); return 1; };
- // Count number of lines and number of columns
- int rctr = 0, cctr = 0;
- char buf[1024];
- while(fgets(buf, sizeof(buf), fptr)){ ++rctr; if(cctr == 0){cctr = strlen(buf)-1; }; };
- printf("Field size: %dx%d (rows x columns)\n", rctr, cctr);
- rewind(fptr);
- // Copy field into 2D array
- char field[rctr][cctr];
- int row = 0, col = 0, x = -1, y =-1;
- while(!feof(fptr)) {
- char ch = fgetc(fptr);
- switch(ch) {
- case '\n': ++row; col = 0; break;
- case '^': x = row, y = col; field[row][col++] = '.'; break;
- default: field[row][col++] = ch; break;
- };
- };
- fclose(fptr);
- printf("Starting coordinates: %dx%d (row x column)\n", x, y);
- //for (int i = 0; i < rctr; ++i){ for (int j = 0; j < cctr; ++j) { putchar(field[i][j]); }; putchar('\n'); };
- // PART 1
- // Moving guard and counting steps
- int direction = UP, positions = 0, wctr = 0;
- int p1x = x, p1y = y;
- while(1){
- if (p1x < 0 || p1y < 0 || p1x >= rctr || p1y >= cctr){break; };
- if (field[p1x][p1y] == '.') { ++positions; field[p1x][p1y] = 'X'; };
- switch(direction){
- case UP: { (field[p1x-1][p1y] == '#') ? (direction = RIGHT) : (--p1x); break; };
- case RIGHT: { (field[p1x][p1y+1] == '#') ? (direction = DOWN) : (++p1y); break; };
- case DOWN: { (field[p1x+1][p1y] == '#') ? (direction = LEFT) : (++p1x); break; };
- case LEFT: { (field[p1x][p1y-1] == '#') ? (direction = UP) : (--p1y); break; };
- };
- if (++wctr > 10000){printf("Breaking infinite while loop!."); break;};
- };
- printf("Total number of distinct positions: %d (number of loops: %d)\n", positions, wctr);
- // PART 2
- positions = 0;
- int p2x = x, p2y = y;
- 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
- //for (int i = 0; i < rctr; ++i){ for (int j = 0; j < cctr; ++j) { putchar(field[i][j]); }; putchar('\n'); };
- for (int row = 0; row < rctr; ++row){ for (int col = 0; col < cctr; ++col){ // Looping through rows and cols
- if(row == x && col == y) { continue; };
- if(field[row][col] == '.'){ field[row][col] = '#'; } else { continue; }; // Change cell to '#' only if it is possible
- wctr = 0, p2x = x, p2y = y, direction = UP;
- while(1){
- if (p2x < 0 || p2y < 0 || p2x >= rctr || p2y >= cctr){ break; };
- switch(direction){
- case UP: { (field[p2x-1][p2y] == '#') ? (direction = RIGHT) : (--p2x); break; };
- case RIGHT: { (field[p2x][p2y+1] == '#') ? (direction = DOWN) : (++p2y); break; };
- case DOWN: { (field[p2x+1][p2y] == '#') ? (direction = LEFT) : (++p2x); break; };
- case LEFT: { (field[p2x][p2y-1] == '#') ? (direction = UP) : (--p2y); break; };
- };
- if (++wctr > 10000){ ++positions; break; };
- };
- field[row][col] = '.'; // Removing added obstruction for next loop
- }; };
- printf("Total number of obstruction positions: %d\n", positions);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement