Advertisement
Guest User

Untitled

a guest
Dec 14th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.16 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define lines 100
  5. #define columns 100
  6. #define true 1
  7. #define false 0
  8. #define LAUNCH_AS_INDEPENDENT
  9.  
  10. typedef _Bool bool;
  11.  
  12. typedef struct result {
  13.     char data[lines+1][columns+1];
  14.     struct result* next;
  15. } result;
  16.  
  17. void tilt_old(int dir_y, int dir_x, char data[lines+1][columns+1], int length, int width) {
  18.     int line;
  19.     int column;
  20.    
  21.     int _line;
  22.     int _column;
  23.    
  24.     if ((dir_y == -1) || (dir_x == -1)) {
  25.        
  26.         for (line = 0; data[line][0]; line++) {
  27.            
  28.             for (column = 0; data[line][column]; column++) {
  29.                
  30.                 if (data[line][column] != 'O') continue;
  31.                
  32.                 _line = line;
  33.                 _column = column;
  34.                
  35.                 while ((_line + dir_y >= 0) && (_column + dir_x >= 0) && (data[_line + dir_y][_column + dir_x] == '.')) {
  36.                     data[_line][_column] = '.';
  37.                     data[_line + dir_y][_column + dir_x] = 'O';
  38.                     _line += dir_y;
  39.                     _column += dir_x;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     else {
  45.        
  46.         for (line = length-1; line >= 0; line--) {
  47.            
  48.             for (column = width-1; column >= 0; column--) {
  49.                
  50.                 if (data[line][column] != 'O') continue;
  51.                
  52.                 _line = line;
  53.                 _column = column;
  54.                
  55.                 while ((_line + dir_y < lines) && (_column + dir_x < columns) && (data[_line + dir_y][_column + dir_x] == '.')) {
  56.                     data[_line][_column] = '.';
  57.                     data[_line + dir_y][_column + dir_x] = 'O';
  58.                     _line += dir_y;
  59.                     _column += dir_x;
  60.                 }
  61.                
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. void tilt(int dir_y, int dir_x, char data[lines+1][columns+1], int length, int width) {
  68.     int line;
  69.     int column;
  70.    
  71.     int _line;
  72.     int _column;
  73.    
  74.     int rocks;
  75.     int start;
  76.     int end;
  77.    
  78.     if (dir_y == -1) {
  79.        
  80.         for (column = 0; column < width; column++) {
  81.            
  82.             for (line = 0; line < length; line++) {
  83.                
  84.                 rocks = 0;
  85.                 start = line;
  86.                
  87.                 while ((line < length) && (data[line][column] != '#')) {
  88.                    
  89.                     if (data[line][column] == 'O') rocks++;
  90.                     line++;
  91.                 }
  92.                
  93.                 end = line;
  94.                 for (_line = start; _line < end; _line++) {
  95.                    
  96.                     if (_line < rocks + start) data[_line][column] = 'O';
  97.                     else data[_line][column] = '.';
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     else if (dir_y == 1) {
  103.        
  104.         for (column = 0; column < width; column++) {
  105.            
  106.             for (line = length-1; line >= 0; line--) {
  107.                
  108.                 rocks = 0;
  109.                 start = line;
  110.                
  111.                 while ((line >= 0) && (data[line][column] != '#')) {
  112.                    
  113.                     if (data[line][column] == 'O') rocks++;
  114.                     line--;
  115.                 }
  116.                
  117.                 end = line;
  118.                 for (_line = start; _line > end; _line--) {
  119.                     if (_line > start-rocks) data[_line][column] = 'O';
  120.                     else data[_line][column] = '.';
  121.                 }
  122.             }
  123.         }
  124.     }
  125.     else if (dir_x == -1) {
  126.        
  127.         for (line = 0; line < length; line++) {
  128.            
  129.             for (column = 0; column < width; column++) {
  130.                
  131.                 rocks = 0;
  132.                 start = column;
  133.                
  134.                 while ((column < width) && (data[line][column] != '#')) {
  135.                    
  136.                     if (data[line][column] == 'O') rocks++;
  137.                     column++;
  138.                 }
  139.                
  140.                 end = column;
  141.                 for (_column = start; _column < end; _column++) {
  142.                    
  143.                     if (_column < rocks + start) data[line][_column] = 'O';
  144.                     else data[line][_column] = '.';
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     else if (dir_x == 1) {
  150.        
  151.         for (line = 0; line < length; line++) {
  152.            
  153.             for (column = width-1; column >= 0; column--) {
  154.                
  155.                 rocks = 0;
  156.                 start = column;
  157.                
  158.                 while ((column >= 0) && (data[line][column] != '#')) {
  159.                    
  160.                     if (data[line][column] == 'O') rocks++;
  161.                     column--;
  162.                 }
  163.                
  164.                 end = column;
  165.                 for (_column = start; _column > end; _column--) {
  166.                     if (_column > start-rocks) data[line][_column] = 'O';
  167.                     else data[line][_column] = '.';
  168.                 }
  169.             }
  170.         }
  171.     }
  172. }
  173.  
  174. void cycle(char data[lines+1][columns+1], int length, int width) {
  175.     tilt(-1, 0, data, length, width);
  176.     tilt(0, -1, data, length, width);
  177.     tilt(1, 0, data, length, width);
  178.     tilt(0, 1, data, length, width);
  179. }
  180.  
  181. int calc_load(char data[lines+1][columns+1], int length, int width) {
  182.     int load = 0;
  183.    
  184.     int i;
  185.     int j;
  186.     for (i = 0; i < length+1; i++) {
  187.         j = 0;
  188.         for (j = 0; j < width+1; j++) {
  189.            
  190.             if (data[i][j] == 'O') load += length-i;
  191.         }
  192.     }
  193.    
  194.     return load;
  195. }
  196.  
  197. void print_data(char data[lines+1][columns+1], int length, int width) {
  198.     int i;
  199.     int j;
  200.     for (i = 0; i < length; i++) {
  201.         j = 0;
  202.         printf("\n");
  203.         for (j = 0; j < width; j++) {
  204.             printf("%c", data[i][j]);
  205.         }
  206.     }
  207. }
  208.  
  209. void add_to_known_results(char data[lines+1][columns+1], result* results) {
  210.     result* current = results;
  211.     while (current->next) {
  212.         current = current->next;
  213.     }
  214.     current->next = malloc(sizeof(result));
  215.     current->next->next = NULL;
  216.     int i;
  217.     int j;
  218.     for (i = 0; i < lines+1; i++) {
  219.         for (j = 0; j < columns+1; j++) {
  220.             current->data[i][j] = data[i][j];
  221.         }
  222.     }
  223. }
  224.  
  225. int get_results_length(result* results) {
  226.     int length = 0;
  227.     result* current = results;
  228.     while (current->next) {
  229.         current = current->next;
  230.         length++;
  231.     }
  232.     return length;
  233. }
  234.  
  235. bool is_equal(char data_a[lines+1][columns+1], char data_b[lines+1][columns+1]) {
  236.     int i;
  237.     int j;
  238.     for (i = 0; i < lines+1; i++) {
  239.         for (j = 0; j < columns+1; j++) {
  240.             if (data_a[i][j] != data_b[i][j]) return false;
  241.         }
  242.     }
  243.     return true;
  244. }
  245.  
  246. int get_index_of_result(char data[lines+1][columns+1], result* results) {
  247.    
  248.     result* current = results;
  249.     int index = 0;
  250.    
  251.     bool equal = is_equal(data, current->data);
  252.     while (current->next && !equal) {
  253.         current = current->next;
  254.         equal = is_equal(data, current->data);
  255.         index++;
  256.     }
  257.    
  258.     if (equal) {
  259.         return index;
  260.     }
  261.    
  262.     return -1;
  263. }
  264.  
  265. void assign_result_at_index(char data[lines+1][columns+1], result* results, int index) {
  266.     result* current = results;
  267.     int i;
  268.     int j;
  269.     for (i = 0; i < index; i++) current = current->next;
  270.    
  271.     for (i = 0; i < lines+1; i++) {
  272.        
  273.         for (j = 0; j < columns+1; j++) {
  274.            
  275.             data[i][j] = current->data[i][j];
  276.         }
  277.     }
  278. }
  279.  
  280. int main(void) {
  281.     FILE* fptr;
  282.    
  283.     char* filename = "data";
  284.    
  285.     fptr = fopen(filename, "r");
  286.    
  287.     if (fptr == NULL) {
  288.         printf("File '%s' not found. Program will now exit.\n", filename);
  289.         exit(0);
  290.     }
  291.     else {
  292.         printf("File opened successfully.\n");
  293.     }
  294.    
  295.     char data[lines+1][columns+1];
  296.     int i;
  297.     int j;
  298.    
  299.     //Nullify the 2-dimensional array
  300.     for (i = 0; i < lines+1; i++) {
  301.         j = 0;
  302.         for (j = 0; j < columns+1; j++) {
  303.             data[i][j] = 0;
  304.         }
  305.     }
  306.  
  307.     //Read in the data
  308.     i = 0;
  309.     j = 0;
  310.     char chr;
  311.     int length = 0;
  312.     int width = 0;
  313.     while(chr = fgetc(fptr)) {
  314.         if (chr == EOF) break;
  315.         if (chr == '\n') {
  316.             data[i][j] = '\0';
  317.             if (!i) width = j;
  318.             i++;
  319.             j = 0;
  320.             length++;
  321.         }
  322.         else {
  323.             data[i][j] = chr;
  324.             j++;
  325.         };
  326.     }
  327.    
  328.     //Copy the data into another 2D-Array for Part 2
  329.     char data2[lines+1][columns+1];
  330.     for (i = 0; i < lines+1; i++) {
  331.         for (j = 0; j < columns+1; j++) {
  332.             data2[i][j] = data[i][j];
  333.         }
  334.     }
  335.    
  336.     //printf("\nLength: %i\nWidth: %i\n", length, width);
  337.    
  338.    
  339.     //Calculate Part 1
  340.     clock_t start = clock();
  341.    
  342.     tilt(-1, 0, data, length, width);
  343.    
  344.     int load = calc_load(data, length, width);
  345.    
  346.     clock_t end = clock();
  347.    
  348.     double time_to_execute = (double)(end - start) / CLOCKS_PER_SEC;
  349.    
  350.     printf("\nPart 1:\n Time needed: %.3lfs\n Load: %i\n", time_to_execute, load);
  351.    
  352.     //Calculate Part 2
  353.     start = clock();
  354.    
  355.     result* results = malloc(sizeof(result));
  356.     results->next = NULL;
  357.     int needed_iteration = 1e9;
  358.    
  359.     int loop_start = -1;
  360.     while (true) {
  361.         cycle(data2, length, width);
  362.         loop_start = get_index_of_result(data2, results);
  363.         if (loop_start != -1) break;
  364.         add_to_known_results(data2, results);
  365.     }
  366.     int loop_end = get_results_length(results);
  367.     int loop_length = loop_end - loop_start;
  368.     int result_index_for_needed_iteration = loop_start + (needed_iteration - 1 - loop_start) % loop_length;
  369.     assign_result_at_index(data2, results, result_index_for_needed_iteration);
  370.    
  371.     //printf("\nLoop: %i-%i, Length: %i\n", loop_start, loop_end, loop_length);
  372.        
  373.     load = calc_load(data2, length, width);
  374.    
  375.     end = clock();
  376.    
  377.     time_to_execute = (double)(end - start) / CLOCKS_PER_SEC;
  378.    
  379.     printf("\nPart 2:\n Time needed: %.3lfs\n Load: %i\n", time_to_execute, load);
  380.    
  381.     fclose(fptr);
  382.    
  383.     //To stop the program to exit after execution if it is supposed to start outside the terminal
  384. #ifdef LAUNCH_AS_INDEPENDENT
  385.     getchar();
  386. #endif /*LAUNCH_AS_INDEPENDENT*/
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement