Advertisement
brggti

AoC 2024 day 23

Dec 23rd, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <time.h>
  7.  
  8. #define filename    "input_23_1.txt"
  9.  
  10. #define CHARCNT     ((uint16_t)('z'-'a'+1))
  11. #define MAPLEN      (CHARCNT*CHARCNT)
  12.  
  13. bool conn[MAPLEN][MAPLEN];
  14. char results[22222][150];   /* Must not be more than 22222 3-way networks
  15.                                and final result must not be longer than 50 nodes (adjust according to your data) */
  16.  
  17. uint16_t getIndex(char * node)
  18. {
  19.     return ((uint16_t)(node[0]-'a') * CHARCNT) + (uint16_t)(node[1]-'a');
  20. }
  21.  
  22. int printIndex(uint16_t index, char * ptr)
  23. {
  24.     if (ptr) return sprintf(ptr, "%c%c,", (char)((index/CHARCNT)+'a'), (char)((index%CHARCNT)+'a'));
  25.     else     return printf(      "%c%c,", (char)((index/CHARCNT)+'a'), (char)((index%CHARCNT)+'a'));
  26. }
  27.  
  28. int main()
  29. {
  30.     FILE * f;
  31.     int result1 = 0;
  32.     int strcount = 0;
  33.     int num;
  34.  
  35.     f = fopen(filename, "r");
  36.     if (f != NULL) {
  37.         char line[160] = {0};
  38.         while (!feof(f)) {
  39.             if ((NULL != fgets(line, sizeof(line), f)) && (strlen(line) > 1)) {
  40.                 uint16_t node1 = getIndex(&line[0]);
  41.                 uint16_t node2 = getIndex(&line[3]);
  42.                 conn[node1][node2] = true;
  43.                 conn[node2][node1] = true;
  44.             }
  45.         }
  46.         fclose(f);
  47.     }
  48.  
  49.     uint16_t ta = getIndex("ta");
  50.     uint16_t tz = getIndex("tz");
  51.     for (uint16_t x = 0; x < MAPLEN; x++) {
  52.         for (uint16_t i = x+1; i < MAPLEN; i++) {
  53.             if (conn[x][i]) {
  54.                 for (uint16_t t = i+1; t < MAPLEN; t++) {
  55.                     if (conn[t][i] && conn[t][x]) {
  56.                         int len=0;
  57.                         len+=printIndex(x, &results[strcount][len]);
  58.                         len+=printIndex(i, &results[strcount][len]);
  59.                              printIndex(t, &results[strcount][len]);
  60.                         strcount++;
  61.                         if (((t>=ta) && (t<=tz)) ||
  62.                             ((x>=ta) && (x<=tz)) ||
  63.                             ((i>=ta) && (i<=tz)))  {
  64.                             result1++;
  65.                             /*
  66.                             printf("connection between: ");
  67.                             printIndex(x,0);
  68.                             printIndex(i,0);
  69.                             printIndex(t,0);
  70.                             printf("\n\r");
  71.                             fflush(stdout);
  72.                             */
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     printf("Result 1 (3-node networks with t-node): %i\n\r", result1);
  80.     printf("Total count of networks with 3+ nodes: %i\n\r", strcount);
  81.  
  82.     int lastAddedIndex = 0;
  83.     int lookNext = 3;
  84.     int lookCount;
  85.     do {
  86.         lookCount = lookNext;
  87.         for (int i = 0; i < strcount; i++) {
  88.             if (strlen(results[i]) == lookCount*3) {
  89.                 uint16_t lastIndex = getIndex(&results[i][(lookCount-1)*3]);
  90.                 for (uint16_t newIndex = lastIndex+1; newIndex < MAPLEN; newIndex++) {
  91.                     // check whether newIndex is connected to all existing items in this results[i] string
  92.                     bool match = true;
  93.                     for (uint16_t look = 0; look < lookCount; look++) {
  94.                         uint16_t testIndex = getIndex(&results[i][look*3]);
  95.                         if (!conn[newIndex][testIndex]) {
  96.                             match = false;
  97.                             break;
  98.                         }
  99.                     }
  100.                     if (match) {
  101.                         // add newIndex into array
  102.                         int len=strlen(results[i]);
  103.                         printIndex(newIndex, &results[i][len]);
  104.                         lookNext = lookCount+1;
  105.                         lastAddedIndex = i;
  106.                         break;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     } while(lookNext > lookCount);
  112.     printf("Nodes in biggest network: %i\n\r", lookCount);
  113.     printf("Result 2: %.*s\n\r", (int)(strlen(results[lastAddedIndex])-1), results[lastAddedIndex]);
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement