No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

284 líneas
8.0 KiB

  1. #include <math.h>
  2. #include "core.hpp"
  3. int day1() {
  4. Arena arena = createArena(Megabytes(16));
  5. list<char> file = readEntireFile(&arena, strlit("./day-1-input"));
  6. int LIST_SIZE = 1000;
  7. int *firsts = PushArray(&arena, int, LIST_SIZE);
  8. int *seconds = PushArray(&arena, int, LIST_SIZE);
  9. char currentNum[5];
  10. int numIndex = 0;
  11. int line = 0;
  12. int *target = firsts;
  13. for (int i = 0; i < file.length; i++) {
  14. char c = file.data[i];
  15. if (c == ' ') {
  16. continue;
  17. } else if (c == '\n') {
  18. line++;
  19. continue;
  20. } else {
  21. currentNum[numIndex++] = c;
  22. }
  23. if (numIndex == 5) {
  24. target[line] = atoi(currentNum);
  25. if (target == firsts) {
  26. target = seconds;
  27. } else {
  28. target = firsts;
  29. }
  30. numIndex = 0;
  31. }
  32. }
  33. qsort(firsts, line, sizeof(int), cmpint);
  34. qsort(seconds, line, sizeof(int), cmpint);
  35. int sum = 0;
  36. for (int i = 0; i < line; i++) {
  37. int dist = firsts[i] - seconds[i];
  38. if (dist < 0) {
  39. dist *= -1;
  40. }
  41. sum += dist;
  42. }
  43. printf("%i\n", sum);
  44. // Part 2
  45. int total = 0;
  46. for (int i = 0; i < line; i++) {
  47. int firstElement = firsts[i];
  48. int countInSecondList = 0;
  49. for (int j = 0; j < line; j++) {
  50. if (seconds[j] == firstElement) {
  51. countInSecondList++;
  52. }
  53. }
  54. total += firstElement * countInSecondList;
  55. }
  56. printf("%i\n", total);
  57. return 0;
  58. }
  59. int day2_main() {
  60. Arena arena = createArena(Megabytes(16));
  61. list<char> file = readEntireFile(&arena, strlit("./day-2-input"));
  62. int safes = 0;
  63. int lines = 0;
  64. int fileReadIndex = 0;
  65. list<int> nums = PushList(&arena, int, 100);
  66. list<char> numStr = PushList(&arena, char, 16);
  67. list<int> modifiedNums = PushList(&arena, int, nums.capacity);
  68. while (fileReadIndex < file.length) {
  69. int linestart = fileReadIndex;
  70. int lineend = fileReadIndex;
  71. while (file.data[lineend] != '\n' && lineend < file.length) {
  72. lineend++;
  73. }
  74. lines++;
  75. zeroList(&nums);
  76. for (int j = linestart; j < lineend; j++) {
  77. if (file.data[j] != ' ') {
  78. appendList(&numStr, (char)file.data[j]);
  79. }
  80. if (file.data[j] == ' ' || j == lineend - 1) {
  81. appendList(&numStr, '\0');
  82. appendList(&nums, atoi(numStr.data));
  83. zeroList(&numStr);
  84. }
  85. }
  86. int unsafes = 0;
  87. for (int i = 0; i < nums.length + 1; i++) {
  88. int skipIndex = i - 1;
  89. zeroList(&modifiedNums);
  90. for (int modifiedIndex = 0; modifiedIndex < nums.length; modifiedIndex++) {
  91. if (modifiedIndex == skipIndex) {
  92. continue;
  93. }
  94. appendList(&modifiedNums, nums.data[modifiedIndex]);
  95. }
  96. bool increasing = modifiedNums.data[0] < modifiedNums.data[1];
  97. bool safe = true;
  98. for (int checkIndex = 0; checkIndex < modifiedNums.length - 1; checkIndex++) {
  99. int next = checkIndex + 1;
  100. int diff = increasing ? modifiedNums.data[next] - modifiedNums.data[checkIndex] : modifiedNums.data[checkIndex] - modifiedNums.data[next];
  101. if (diff < 1 || diff > 3) {
  102. safe = false;
  103. break;
  104. }
  105. }
  106. if (!safe) {
  107. unsafes += 1;
  108. } else {
  109. break;
  110. }
  111. }
  112. if (unsafes <= nums.length) {
  113. safes++;
  114. }
  115. fileReadIndex = lineend + 1;
  116. }
  117. printf("safes: %i", safes);
  118. printf("\n");
  119. printf("lines: %i", lines);
  120. printf("\n");
  121. printf("Arena: %zu, %zu", arena.head, arena.capacity);
  122. printf("\n");
  123. return 0;
  124. }
  125. bool stringContains(string str, char c) {
  126. for (size_t i = 0; i < str.len; i++) {
  127. if (str.str[i] == c) {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. const char NUMERIC_CHARS[] = "1234567890";
  134. inline bool isNumeric(char c) {
  135. return stringContains(strlit(NUMERIC_CHARS), c);
  136. }
  137. inline int parsePositiveInt(string str, size_t *lengthPointer) {
  138. size_t numEnd = 0;
  139. char currChar = str.str[numEnd];
  140. while (numEnd < str.len && isNumeric(currChar)) {
  141. currChar = str.str[++numEnd];
  142. *lengthPointer += 1;
  143. }
  144. *lengthPointer -= 1;
  145. if (numEnd > 0) {
  146. int result = 0;
  147. for (int i = 0; i < numEnd; i++) {
  148. result *= 10;
  149. result += (double)(str.str[i] - '0');
  150. }
  151. return result;
  152. } else {
  153. return -1;
  154. }
  155. }
  156. int day3_main() {
  157. Arena arena = createArena(Megabytes(16));
  158. list<char> file = readEntireFile(&arena, strlit("./day-3-input"));
  159. string mulBegin = strlit("mul(");
  160. string doIdent = strlit("do()");
  161. string dontIdent = strlit("don't()");
  162. int result = 0;
  163. size_t c = 0;
  164. bool skip = false;
  165. while (c < file.length) {
  166. if (!skip) {
  167. if (file.data[c] == 'm') {
  168. string segment = strSlice(file.data, c, c+mulBegin.len);
  169. int first = -1;
  170. int second = -1;
  171. if (strEql(segment, mulBegin)) {
  172. c += segment.len;
  173. first = parsePositiveInt(strSlice(file.data, c, file.length - c), &c);
  174. if (file.data[c + 1] == ',') {
  175. c += 2;
  176. second = parsePositiveInt(strSlice(file.data, c, file.length - c), &c);
  177. if (second != -1 && file.data[c + 1] == ')') {
  178. c += 1;
  179. result += first * second;
  180. }
  181. }
  182. }
  183. } else if (file.data[c] == 'd') {
  184. string segment = strSlice(file.data, c, c+dontIdent.len);
  185. if (strEql(segment, dontIdent)) {
  186. skip = true;
  187. c += dontIdent.len - 1;
  188. }
  189. }
  190. } else {
  191. if (file.data[c] == 'd') {
  192. string segment = strSlice(file.data, c, c+doIdent.len);
  193. if (strEql(segment, doIdent)) {
  194. skip = false;
  195. c += doIdent.len - 1;
  196. }
  197. }
  198. }
  199. c++;
  200. }
  201. printf("total: %i\n", result);
  202. return 0;
  203. }
  204. int day4_main() {
  205. Arena mainArena = createArena(Megabytes(16));
  206. Arena *arena = &mainArena;
  207. list<char> file = readEntireFile(arena, strlit("./day-4-input"));
  208. int fileReadIndex = 0;
  209. int lines = 0;
  210. int lineLength = 0;
  211. while (fileReadIndex < file.length) {
  212. int linestart = fileReadIndex;
  213. int lineend = fileReadIndex;
  214. while (file.data[lineend] != '\n' && lineend < file.length) {
  215. lineend++;
  216. }
  217. if (lineLength == 0) {
  218. lineLength = lineend - linestart;
  219. } else if (lineend - linestart != lineLength) {
  220. fputs("Bad input. One or more lines aren't the same length as the first line.", stderr);
  221. return 1;
  222. }
  223. lines++;
  224. fileReadIndex += lineLength + 1;
  225. }
  226. string xmas = strlit("XMAS");
  227. int count = 0;
  228. for (int c = 0; c < file.length; c++) {
  229. string strings[] = {
  230. //forwards
  231. strSlice(file.data, c, c+xmas.len),
  232. //down
  233. strSlice(file.data, c, c+xmas.len),
  234. //forwardsReverse
  235. strReverse(arena, strSlice(file.data, c, c+xmas.len)),
  236. //downReverse
  237. strReverse(arena, strSlice(file.data, c, c+xmas.len)),
  238. };
  239. for (size_t i = 0; i < ArrayCount(strings); i++) {
  240. if (strEql(strings[i], xmas)) {
  241. c += xmas.len;
  242. count++;
  243. }
  244. }
  245. }
  246. printf("count: %i\n", count);
  247. return 0;
  248. }
  249. int main() {
  250. return day4_main();
  251. }