mosredna

AoC 2021 day 14

Dec 14th, 2021
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "data.h"
  3.  
  4. struct keyvalue {
  5.    char key;
  6.    uint64_t value = 0;
  7. };
  8.  
  9. const int listSize = sizeof rules / sizeof rules[0];
  10. char ruleList[listSize][3] = {"\0"};
  11. char charList[listSize][3] = {"\0"};;
  12. uint64_t countList[listSize] = {0};
  13. uint64_t bufferList[listSize] = {0};
  14. keyvalue countMap[listSize];
  15.  
  16. void pairInsertion(){
  17.   for (int i = 0; i < listSize; i++) {
  18.     if(countList[i]== 0)continue;
  19.  
  20.     char buf1[3]= {'\0'};
  21.     buf1[0] = ruleList[i][0];
  22.     buf1[1] = charList[i][0];
  23.  
  24.     char buf2[3]= {'\0'};
  25.     buf2[0] = charList[i][0];
  26.     buf2[1] = ruleList[i][1];
  27.  
  28.     for (int j = 0; j < listSize; j++) {
  29.       if(strcmp(buf1, ruleList[j]) == 0)bufferList[j]+=countList[i];
  30.       if(strcmp(buf2, ruleList[j]) == 0)bufferList[j]+=countList[i];
  31.     }
  32.   }
  33.  
  34.   for (int i = 0; i < listSize; i++) {
  35.     countList[i] = bufferList[i];
  36.     bufferList[i] = 0;
  37.   }
  38. }
  39.  
  40. void initData(){
  41.  
  42.   int index = 0;
  43.   for(auto r : rules){
  44.     strcpy(ruleList[index],r[0]);
  45.     strcpy(charList[index],r[1]);
  46.     index++;
  47.   };
  48.  
  49.   for (size_t i = 0; i < sizeof str / sizeof str[0] -1; i++)
  50.   {
  51.     char buf[3]= {'\0'};
  52.     buf[0] = str[i];
  53.     buf[1] = str[i + 1];
  54.  
  55.     for (int j = 0; j < listSize; j++)
  56.     {
  57.         if(strcmp(ruleList[j], buf) == 0)countList[j]++;
  58.     }
  59.   }
  60. }
  61.  
  62. int strIndex = 0;
  63. void addKeyValue(char key, uint64_t value){
  64.   bool inList = false;
  65.   for(auto &kv : countMap){
  66.     if(kv.key == key){
  67.       kv.value+=value;
  68.       inList = true;
  69.       break;
  70.     }
  71.   }
  72.   if(!inList){
  73.     countMap[strIndex].key = key;
  74.     countMap[strIndex++].value = value;
  75.   }
  76. }
  77.  
  78. char *uint64_to_string(uint64_t input)
  79. {
  80.     static char result[21] = "";
  81.     // Clear result from any leftover digits from previous function call.
  82.     memset(&result[0], 0, sizeof(result));
  83.     // temp is used as a temporary result storage to prevent sprintf bugs.
  84.     char temp[21] = "";
  85.     char c;
  86.     uint8_t base = 10;
  87.  
  88.     while (input)
  89.     {
  90.         int num = input % base;
  91.         input /= base;
  92.         c = '0' + num;
  93.  
  94.         sprintf(temp, "%c%s", c, result);
  95.         strcpy(result, temp);
  96.     }
  97.     return result;
  98. }
  99.  
  100. void setup() {
  101.  
  102.   Serial.begin(9600);
  103.   while(!Serial);
  104.  
  105.   initData();
  106.  
  107.   for (int x = 0; x < 40 ; x++) {
  108.     pairInsertion();
  109.   }
  110.  
  111.   for (int i = 0; i < listSize; i++) {
  112.     addKeyValue(ruleList[i][1],countList[i]);
  113.   };
  114.   addKeyValue(str[0],1);
  115.  
  116.   uint64_t minCount=INFINITY;
  117.   uint64_t maxCount=0;
  118.  
  119.   for(auto &kv : countMap){
  120.     if(kv.key != NULL){
  121.       maxCount = max(kv.value, maxCount);
  122.       minCount = min(kv.value, minCount);
  123.     }
  124.   }
  125.   Serial.println( "------" );
  126.   Serial.println( uint64_to_string ( maxCount - minCount ));
  127. }
  128.  
  129. void loop() {
  130.   // put your main code here, to run repeatedly:
  131. }
Advertisement
Add Comment
Please, Sign In to add comment