Guest User

Untitled

a guest
Dec 12th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5. void *prev;
  6. void *next;
  7. int value;
  8. } Node;
  9.  
  10. Node* assembleRingNodes(int *nodeValues, int len){
  11. // assemble ring of `struct Node`s
  12. Node *firstNode = (Node*) malloc(sizeof(Node));
  13. firstNode->value = nodeValues[0];
  14.  
  15. Node *nextNode, *lastNode = firstNode;
  16. for(int i=1; i<len; i+=1){
  17. nextNode = (Node*) malloc(sizeof(Node));
  18.  
  19. nextNode->value = nodeValues[i];
  20. nextNode->prev = lastNode;
  21.  
  22. lastNode->next = nextNode;
  23.  
  24. lastNode = nextNode;
  25. }
  26.  
  27. // connect the last node to the first node, thus forming a ring
  28. nextNode->next = firstNode;
  29.  
  30. return firstNode;
  31. }
  32.  
  33. int main(){
  34. // read in challenge input as ASCII codes
  35. int challengeInputLength;
  36. int challengeInput[1024];
  37. unsigned char buffer[1024];
  38. FILE *fp = fopen("day10challengeinput","rb");
  39. challengeInputLength = fread(buffer, 1, 1024, fp);
  40. for(int i=0; i<challengeInputLength; i+=1){
  41. challengeInput[i] = (int) buffer[i];
  42. }
  43. fclose(fp);
  44.  
  45. int tailEndLengths[5] = {17, 31, 73, 47, 23};
  46. for(int i=0; i<5; i+=1){
  47. challengeInput[challengeInputLength] = tailEndLengths[i];
  48. challengeInputLength += 1;
  49. }
  50.  
  51. // populate values: 0,1,2,3,4, ... 254,255
  52. int nodeValues[256];
  53. for(int i=0; i<256; i+=1) nodeValues[i] = i;
  54.  
  55. // create a ring of nodes with the values in nodeValues
  56. Node *firstNode = assembleRingNodes(&nodeValues[0], sizeof(nodeValues)/sizeof(int));
  57.  
  58. // start solving, runs 64 rounds
  59. int skipSize = 0;
  60. Node *position = firstNode;
  61.  
  62. for(int round = 0; round < 64; round +=1){
  63. for(int i=0; i<challengeInputLength; i+=1){
  64. int currentLength = challengeInput[i];
  65.  
  66. // collect values for this run
  67. int collectedValues[currentLength];
  68. Node *collectionNode = position;
  69. for(int j=0; j<currentLength; j+=1){
  70. collectedValues[j] = collectionNode->value;
  71. collectionNode = collectionNode->next;
  72. }
  73.  
  74. // distribute values in reverse order AND move position forward
  75. for(int j=currentLength-1; j>=0; j-=1){
  76. position->value = collectedValues[j];
  77. position = position->next;
  78. }
  79.  
  80. // move position node forward skip times
  81. for(int j=0; j<skipSize; j+=1){
  82. position = position->next;
  83. }
  84.  
  85. // increment skip by 1
  86. skipSize += 1;
  87. }
  88. }
  89.  
  90. // populate sparsehash
  91. unsigned char sparseHash[256];
  92. position = firstNode;
  93. for(int i=0; i<256; i+=1){
  94. sparseHash[i] = (unsigned char) position->value;
  95. position = position->next;
  96. }
  97.  
  98. // calculate denseHash
  99. unsigned char denseHash[16] = {0};
  100. for(int i=0; i<16; i+=1){
  101. for(int j=0; j<16; j+=1){
  102. denseHash[i] ^= sparseHash[i*16 + j];
  103. }
  104. }
  105.  
  106. // print the denseHash
  107. printf("Dense Hash hex representation: ");
  108. for(int i=0; i<16; i+=1){
  109. printf("%02x", denseHash[i]);
  110. }
  111. printf("\n");
  112.  
  113. return 0;
  114. }
Add Comment
Please, Sign In to add comment