Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdint.h>
  6.  
  7. char **get_lines_n(const char *filename, int *len) {
  8. FILE *f = fopen(filename, "r");
  9. fseek(f, 0, SEEK_END);
  10. long fsize = ftell(f);
  11. fseek(f, 0, SEEK_SET);
  12.  
  13. char *string = malloc(fsize + 1);
  14. fread(string, 1, fsize, f);
  15. fclose(f);
  16.  
  17. string[fsize] = 0;
  18.  
  19. int i, blks = 0;
  20. char **lines = malloc(101 * sizeof(char *));
  21. char *line;
  22. char *str, *saveptr;
  23.  
  24. for(i = 0, str = string; ; i++, str = NULL) {
  25. if(i >= 100) {
  26. i = 0;
  27. blks++;
  28. lines = realloc(lines, (100 * (1 + blks) + 1) * sizeof(char *));
  29. }
  30. char *tmp = strtok_r(str, "\n", &saveptr);
  31. if(tmp == NULL) {
  32. lines[100*blks + i] = NULL;
  33. break;
  34. }
  35. line = malloc(sizeof(char) * (strlen(tmp)+1));
  36. strcpy(line, tmp);
  37. lines[100*blks + i] = line;
  38. }
  39.  
  40. *len = i;
  41.  
  42. free(string);
  43.  
  44. return lines;
  45. }
  46.  
  47. char **get_lines(const char *filename) {
  48. int foo;
  49.  
  50. return get_lines_n(filename, &foo);
  51. }
  52.  
  53. void free_lines(char **lines) {
  54. int i = 0;
  55. char *line = lines[0];
  56. while(line != NULL) {
  57. free(line);
  58. line = lines[++i];
  59. }
  60. free(lines);
  61. }
  62.  
  63. typedef struct instruction {
  64. int opcode;
  65. int arg1;
  66. int arg2;
  67. } instruction;
  68.  
  69. void part23() {
  70. int len;
  71. char **lines = get_lines_n("23.txt", &len);
  72. char *line, *saveptr;
  73. int i = 0;
  74. instruction *instructions = malloc(sizeof(instruction) * len);
  75.  
  76. /* "Opcodes":
  77. 0 - hlf
  78. 1 - tpl
  79. 2 - inc
  80. 3 - jmp
  81. 4 - jie
  82. 5 - jio
  83. Arguments are registers (0: a, 1: b) or offsets
  84. */
  85.  
  86. while(lines[i] != NULL) {
  87. line = lines[i];
  88. int opcode;
  89. char *ins = strtok_r(line, " ", &saveptr);
  90. switch(ins[0]) {
  91. case 'h':
  92. opcode = 0;
  93. break;
  94. case 't':
  95. opcode = 1;
  96. break;
  97. case 'i':
  98. opcode = 2;
  99. break;
  100. case 'j':
  101. if(ins[1] == 'm') {
  102. opcode = 3;
  103. } else if(ins[2] == 'e') {
  104. opcode = 4;
  105. } else {
  106. opcode = 5;
  107. } break;
  108. }
  109. instructions[i].opcode = opcode;
  110.  
  111. ins = strtok_r(NULL, ",", &saveptr);
  112. if(ins[0] == 'a')
  113. instructions[i].arg1 = 0;
  114. else if(ins[0] == 'b')
  115. instructions[i].arg1 = 1;
  116. else
  117. instructions[i].arg1 = atoi(ins);
  118.  
  119. if(opcode == 4 || opcode == 5)
  120. instructions[i].arg2 = atoi(strtok_r(NULL, ",", &saveptr));
  121. i++;
  122. }
  123.  
  124. int part2 = 0;
  125. int a;
  126. again: a = part2;
  127. int b = 0;
  128. int ip = 0;
  129. int r;
  130. while(ip >= 0 && ip < len) {
  131. instruction cur = instructions[ip];
  132. if(cur.opcode == 0) {
  133. if(cur.arg1)
  134. b /= 2;
  135. else
  136. a /= 2;
  137. ip++;
  138. } else if(cur.opcode == 1) {
  139. if(cur.arg1)
  140. b *= 3;
  141. else
  142. a *= 3;
  143. ip++;
  144. } else if(cur.opcode == 2) {
  145. if(cur.arg1)
  146. b++;
  147. else
  148. a++;
  149. ip++;
  150. } else if(cur.opcode == 3) {
  151. ip += cur.arg1;
  152. } else if(cur.opcode == 4) {
  153. r = (cur.arg1) ? b : a;
  154. if(r % 2 == 0)
  155. ip += cur.arg2;
  156. else
  157. ip++;
  158. } else {
  159. r = (cur.arg1) ? b : a;
  160. if(r == 1)
  161. ip += cur.arg2;
  162. else
  163. ip++;
  164. }
  165. }
  166.  
  167. printf("%d\n", b);
  168.  
  169. if(!part2) {
  170. part2 = 1;
  171. goto again;
  172. }
  173.  
  174. free_lines(lines);
  175. free(instructions);
  176.  
  177. }
  178.  
  179. void reverse(int n[], int len) {
  180. int start = 0;
  181. int end = len;
  182.  
  183. while(start < end) {
  184. int tmp = n[start];
  185. n[start] = n[end];
  186. n[end--] = tmp;
  187. start++;
  188. }
  189. }
  190.  
  191. int min_count = 100;
  192. long long min_product = INT_MAX;
  193. int nums[30];
  194.  
  195. void recursive_search(int offset, int remaining, int count, long long product) {
  196. if(count > min_count)
  197. return;
  198.  
  199. while(nums[offset] != 0) {
  200. if(nums[offset] > remaining) {
  201. ;
  202. }
  203. else if(nums[offset] == remaining) {
  204. if(count < min_count) {
  205. min_count = count;
  206. min_product = product * nums[offset];
  207. } else if(count == min_count) {
  208. if(product * nums[offset] < min_product)
  209. min_product = product * nums[offset];
  210. }
  211. }
  212. else {
  213. recursive_search(offset + 1, remaining - nums[offset], count + 1, product * nums[offset]);
  214. }
  215.  
  216. offset++;
  217. }
  218. }
  219.  
  220. void part24() {
  221. char **lines = get_lines("24.txt");
  222. int i = 0;
  223.  
  224. while(lines[i] != NULL) {
  225. nums[i] = atoi(lines[i]);
  226. free(lines[i++]);
  227. }
  228.  
  229. nums[i] = 0;
  230. reverse(nums, i-1);
  231. int sum = 0;
  232. for(i = 0; nums[i] != 0; i++)
  233. sum += nums[i];
  234. sum /= 3;
  235.  
  236. free(lines);
  237.  
  238. recursive_search(1, sum - nums[0], 2, nums[0]);
  239.  
  240. printf("%I64d\n", min_product);
  241.  
  242. min_count = 100;
  243. min_product = INT_MAX;
  244.  
  245. sum *= 3;
  246. sum /= 4;
  247.  
  248. recursive_search(1, sum - nums[0], 2, nums[0]);
  249.  
  250. printf("%I64d\n", min_product);
  251. }
  252.  
  253. void part25() {
  254. char **lines = get_lines("25.txt");
  255. char *line = lines[0];
  256. int i = 0;
  257.  
  258. while(!isdigit(line[i++]));
  259. int x = atoi(line + i - 1);
  260. while(isdigit(line[i++]));
  261. while(!isdigit(line[i++]));
  262. int y = atoi(line + i - 1);
  263.  
  264. free(line);
  265. free(lines);
  266.  
  267. int num = (x+y) * (x+y) - (x+y);
  268. num = num >> 1;
  269. num += 1-x;
  270.  
  271. uint64_t result = 20151125;
  272. for(i = 1; i < num; i++) {
  273. result *= 252533;
  274. result %= 33554393;
  275. }
  276. printf("%I64d\n", result); // Change to %llu on *nix systems
  277. }
  278.  
  279. int main() {
  280. part23();
  281. //part24();
  282. //part25();
  283.  
  284. return 0;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement