Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct mem{
  5. long addy;
  6. long val;
  7. };
  8. int main() {
  9. FILE *fp;
  10. char str[128];
  11. char *filename = "D:\\Programming\\AOC2020\\Day 14\\day14in.txt";
  12. fp = fopen(filename, "r");
  13. if(fp == NULL) {
  14. printf("Bad file");
  15. return 1;
  16. }
  17. unsigned long andMask = 9223372036854775807;
  18. unsigned long orMask = 0;
  19. int l = 0;
  20. int l2 = 1;
  21. int l3 = 0;
  22. struct mem *mems = malloc(sizeof(struct mem) * l);
  23. struct mem *mems2 = malloc(sizeof(struct mem) * l3);
  24. long *p2and = malloc(sizeof(long) * l2);
  25. long *p2or = malloc(sizeof(long) * l2);
  26. char mask[36];
  27. while(fgets(str,255,fp)){
  28. if(str[1] == 'a'){
  29. andMask = 9223372036854775807;
  30. orMask = 0;
  31. l2 = 1;
  32. p2and = realloc(p2and,sizeof(long) * l2);
  33. p2or = realloc(p2or,sizeof(long) * l2);
  34. p2and[0] = 9223372036854775807;
  35. p2or[0] = 0;
  36.  
  37. for(int i = 7; i < 43; i++){
  38. if(str[i] == 'X'){
  39. l2 *= 2;
  40. p2and = realloc(p2and,sizeof(long) * l2);
  41. p2or = realloc(p2or,sizeof(long) * l2);
  42. for(int j = 0; j < l2/2; j++){
  43. p2and[j+l2/2] = p2and[j] ^ ((unsigned long)1 <<(42-i));
  44. }
  45. }
  46. if(str[i] == '1'){
  47. orMask |= ((unsigned long)1 << (42-i));
  48. for(int j = 0; j < l2; j++){
  49. p2or[j] |= (unsigned long)1 << (42-i);
  50. }
  51. }
  52. if(str[i] == '0'){
  53. andMask ^= ((unsigned long)1 << (42-i));
  54. }
  55. }
  56. for(int j = 0; j < l2; j++) {
  57. p2or[j] = 0;
  58. p2or[j] |= orMask;
  59. for (int i = 0; i < 36; i++) {
  60. if (((p2and[j] >> i) % 2) && (str[42-i] == 'X')){
  61. //printf("t %ld %i\n",p2and[j],i);
  62. p2or[j] |= (long)1 << i;
  63.  
  64. }
  65. }
  66. }
  67. }
  68. if(str[1] == 'e'){
  69. long addy = atol(&(str[4]));
  70. int loc;
  71. for(int i = 0; ;i++){
  72. if(str[i] == '='){
  73. loc = i+2;
  74. break;
  75. }
  76. }
  77. long val = atol(&(str[loc]));
  78. // Part 2
  79. for(int i = 0; i < l2; i++){
  80. int matched = 0;
  81. for(int j = 0; j < l3; j++){
  82. if(mems2[j].addy == (addy & p2and[i] | p2or[i])){
  83. //printf("rewrite %ld %ld %ld\n",p2and[i],p2or[i],(addy & p2and[i] | p2or[i]));
  84. mems2[j].val = val;
  85. matched = 1;
  86. break;
  87. }
  88. }
  89.  
  90. if(!matched){
  91. //printf("add %ld %ld %ld\n",p2and[i],p2or[i],(addy & p2and[i] | p2or[i]));
  92. mems2 = realloc(mems2,sizeof(struct mem) * ++l3);
  93. struct mem temp = {(addy & p2and[i] | p2or[i]),val};
  94. mems2[l3-1] = temp;
  95. }
  96. }
  97.  
  98. // Part 1
  99. int assigned = 0;
  100.  
  101. for(int i = 0; i < l; i++){
  102. if(mems[i].addy == addy){
  103. mems[i].val = (val & andMask) | orMask;
  104. assigned = 1;
  105. break;
  106. }
  107. }
  108.  
  109. if(assigned){continue;}
  110. mems = realloc(mems,sizeof(struct mem) * ++l);
  111. struct mem temp = {addy,(val & andMask) | orMask};
  112. mems[l-1] = temp;
  113. }
  114. }
  115. unsigned long out = 0;
  116. for(int i = 0; i < l; i++){
  117. out+= mems[i].val;
  118. }
  119.  
  120.  
  121. printf("Part 1: %ld\n",out);
  122. out = 0;
  123. for(int i = 0; i < l3; i++){
  124. //printf("addy,val: %ld %i\n",mems2[i].addy,mems2[i].val);
  125. out+= mems2[i].val;
  126. }
  127. printf("part 2: %ld\n", out);
  128. return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement