Advertisement
brggti

AoC 2024 day 22

Dec 23rd, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 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.  
  7. #define filename    "input_22_1.txt"
  8.  
  9. #define HLEN    64321
  10.  
  11. typedef struct {
  12.     uint32_t diffcombo[2000];   // each diffcombo consist of 4 int8_t-s in exact order, which creates a 32-bit code.
  13.     int8_t   last[2000];
  14. } data_t;
  15.  
  16. typedef struct {
  17.     uint32_t addr;
  18.     bool     pending;
  19.     int8_t   pendingvalue;
  20.     int32_t  value;
  21. } object_t;
  22.  
  23. data_t   data;//[1550];
  24. object_t hashmap[HLEN];
  25.  
  26. uint64_t calcSecret(uint64_t secret)
  27. {
  28.     uint64_t new;
  29.     new = secret * 64;
  30.     secret = new ^ secret;     //mix
  31.     new = secret % 16777216;   //prune
  32.     new = new / 32;
  33.     secret = new ^ secret;     //mix
  34.     new = secret % 16777216;   //prune
  35.     new = new * 2048;
  36.     secret = new ^ secret;     //mix
  37.     new = secret % 16777216;   //prune
  38.     return new;
  39. }
  40.  
  41. void addLastNrIntoMap(uint32_t address, int8_t value)
  42. {
  43.     uint16_t index = address % HLEN;
  44.     do {
  45.         if ((hashmap[index].addr == 0) || (hashmap[index].addr == address)) {
  46.             hashmap[index].addr = address;
  47.             if (!hashmap[index].pending) {
  48.                 hashmap[index].pending = true;
  49.                 hashmap[index].pendingvalue = value;
  50.             }
  51.             return;
  52.         }
  53.         index++;
  54.         index %= HLEN;
  55.     } while (1);
  56. }
  57.  
  58. void transferPendingValues(void)
  59. {
  60.     for (int i=0;i<HLEN;i++) {
  61.         hashmap[i].value += hashmap[i].pendingvalue;
  62.         hashmap[i].pending = false;
  63.     };
  64. }
  65.  
  66. uint64_t calc2000th(uint64_t secret, data_t * dptr)
  67. {
  68.     uint64_t new = secret;
  69.     int i =0;
  70.     dptr->last[0] = (int8_t)(-100);
  71.     dptr->diffcombo[0] = 0;
  72.     do {
  73.         new = calcSecret(new);
  74.         i++;
  75.         dptr->last[i] = (int8_t)(new % 10);
  76.         dptr->diffcombo[i] = (dptr->diffcombo[i-1] << 8) + (uint8_t)(dptr->last[i] - dptr->last[i-1]);
  77.         if (i>3)
  78.             addLastNrIntoMap(dptr->diffcombo[i], dptr->last[i]);
  79.     } while (i<2000);
  80.     transferPendingValues();
  81.     return new;
  82. }
  83.  
  84. int main()
  85. {
  86.     FILE * f;
  87.     uint64_t result1 = 0;
  88.     int num;
  89.     int i=0;
  90.     f = fopen(filename, "r");
  91.     if (f != NULL) {
  92.         char line[160] = {0};
  93.         while (!feof(f)) {
  94.             if ((NULL != fgets(line, sizeof(line), f)) && (strlen(line) > 1)) {
  95.                 num = atoi(line);
  96.                 result1 += calc2000th(num, &data/*[i]*/);
  97.                 i++;
  98.             }
  99.         }
  100.         fclose(f);
  101.     }
  102.     printf("Result 1 (%i numbers): %lld\n\r", i, result1);
  103.  
  104.     int max=0;
  105.     uint32_t maxdiff=0;
  106.     for (int h=0;h<HLEN;h++) {
  107.         if ((hashmap[h].addr != 0) && (hashmap[h].value > max)) {
  108.             max = hashmap[h].value;
  109.             maxdiff = hashmap[h].addr;
  110.         }
  111.     }
  112.     printf("Result 2: %i\n\r", max);
  113.     printf("          (%i,%i,%i,%i)\n\r", (int8_t)(maxdiff>>24), (int8_t)(maxdiff>>16), (int8_t)(maxdiff>>8), (int8_t)(maxdiff>>0));
  114.     return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement