Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

230 строки
5.3 KiB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <sys/mman.h>
  6. #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
  7. typedef int8_t int8;
  8. typedef int16_t int16;
  9. typedef int32_t int32;
  10. typedef int64_t int64;
  11. typedef uint8_t uint8;
  12. typedef uint16_t uint16;
  13. typedef uint32_t uint32;
  14. typedef uint64_t uint64;
  15. typedef uint8 byte;
  16. typedef uint16 b16;
  17. typedef uint32 b32;
  18. typedef uint64 b64;
  19. typedef float real32;
  20. typedef double real64;
  21. struct string {
  22. uint8 *str;
  23. size_t len;
  24. };
  25. struct StringNode {
  26. string *str;
  27. size_t start;
  28. size_t end;
  29. };
  30. string asString(StringNode *node) {
  31. return {
  32. node->str->str + node->start,
  33. node->end - node->start,
  34. };
  35. }
  36. string createStr(byte *str, size_t len) {
  37. string result;
  38. result.str = str;
  39. result.len = len;
  40. return result;
  41. }
  42. #define strlit(lit) createStr((byte *)(lit), sizeof(lit) - 1)
  43. #define Bytes(n) (n)
  44. #define Kilobytes(n) (n << 10)
  45. #define Megabytes(n) (n << 20)
  46. #define Gigabytes(n) (((uint64)n) << 30)
  47. #define Terabytes(n) (((uint64)n) << 40)
  48. #define Thousand(n) ((n)*1000)
  49. #define Million(n) ((n)*1000000)
  50. #define Billion(n) ((n)*1000000000LL)
  51. struct Arena {
  52. void *memory;
  53. size_t capacity;
  54. size_t head;
  55. };
  56. void *pushSize(Arena *arena, size_t size) {
  57. if (arena->capacity - arena->head >= size) {
  58. arena->head += size;
  59. return (char *)arena->memory + arena->head + size;
  60. }
  61. return 0;
  62. }
  63. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  64. #define PushStruct(arena, type, size) (type *)pushSize(arena, sizeof(type))
  65. Arena createArena(size_t capacity) {
  66. Arena result = {};
  67. result.memory = mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  68. result.capacity = capacity;
  69. result.head = 0;
  70. return result;
  71. }
  72. size_t getFileSize(string file) {
  73. struct stat st;
  74. stat((char *)file.str, &st);
  75. return st.st_size;
  76. }
  77. struct Buffer {
  78. size_t size;
  79. byte *data;
  80. };
  81. Buffer readEntireFile(Arena *arena, string file) {
  82. FILE *input = fopen((char *)file.str, "r");
  83. size_t filesize = getFileSize(file);
  84. byte *readBuffer = PushArray(arena, byte, filesize);
  85. fread(readBuffer, sizeof(byte), filesize, input);
  86. fclose(input);
  87. return { filesize, readBuffer };
  88. }
  89. int cmpint(const void *a, const void *b) {
  90. int *x = (int *)a;
  91. int *y = (int *)b;
  92. return (*x > *y) - (*x < *y);
  93. }
  94. int day1() {
  95. Arena arena = createArena(Megabytes(16));
  96. Buffer file = readEntireFile(&arena, strlit("./day-1-input"));
  97. int LIST_SIZE = 1000;
  98. int *firsts = PushArray(&arena, int, LIST_SIZE);
  99. int *seconds = PushArray(&arena, int, LIST_SIZE);
  100. char currentNum[5];
  101. int numIndex = 0;
  102. int line = 0;
  103. int *target = firsts;
  104. for (int i = 0; i < file.size; i++) {
  105. char c = file.data[i];
  106. if (c == ' ') {
  107. continue;
  108. } else if (c == '\n') {
  109. line++;
  110. continue;
  111. } else {
  112. currentNum[numIndex++] = c;
  113. }
  114. if (numIndex == 5) {
  115. target[line] = atoi(currentNum);
  116. if (target == firsts) {
  117. target = seconds;
  118. } else {
  119. target = firsts;
  120. }
  121. numIndex = 0;
  122. }
  123. }
  124. qsort(firsts, line, sizeof(int), cmpint);
  125. qsort(seconds, line, sizeof(int), cmpint);
  126. int sum = 0;
  127. for (int i = 0; i < line; i++) {
  128. int dist = firsts[i] - seconds[i];
  129. if (dist < 0) {
  130. dist *= -1;
  131. }
  132. sum += dist;
  133. }
  134. printf("%i\n", sum);
  135. // Part 2
  136. int total = 0;
  137. for (int i = 0; i < line; i++) {
  138. int firstElement = firsts[i];
  139. int countInSecondList = 0;
  140. for (int j = 0; j < line; j++) {
  141. if (seconds[j] == firstElement) {
  142. countInSecondList++;
  143. }
  144. }
  145. total += firstElement * countInSecondList;
  146. }
  147. printf("%i\n", total);
  148. return 0;
  149. }
  150. int day2_main() {
  151. Arena arena = createArena(Megabytes(16));
  152. Buffer file = readEntireFile(&arena, strlit("./day-2-input"));
  153. int safes = 0;
  154. int lines = 0;
  155. int *nums = PushArray(&arena, int, 100);
  156. int numLen = 1;
  157. int index = 0;
  158. while (index < file.size) {
  159. int linestart = index;
  160. int lineend = index;
  161. while (file.data[lineend] != '\n' && lineend < file.size) {
  162. lineend++;
  163. }
  164. lines++;
  165. char numStr[16] = {};
  166. int numStrLen = 1;
  167. numLen = 1;
  168. for (int j = linestart; j < lineend; j++) {
  169. if (file.data[j] != ' ') {
  170. numStr[numStrLen++ - 1] = file.data[j];
  171. } else {
  172. numStr[numStrLen - 1] = '\0';
  173. nums[numLen++ - 1] = atoi(numStr);
  174. numStrLen = 1;
  175. }
  176. }
  177. bool increasing = nums[0] < nums[1];
  178. bool safe = true;
  179. for (int j = 0; j < numLen; j++) {
  180. int diff = increasing ? nums[j + 1] - nums[j] : nums[j] - nums[j + 1];
  181. if (increasing && (diff < 1 || diff > 3)) {
  182. safe = false;
  183. break;
  184. }
  185. }
  186. if (safe) {
  187. safes++;
  188. }
  189. index = lineend + 1;
  190. }
  191. printf("safes: %i", safes);
  192. printf("lines: %i", lines);
  193. return 0;
  194. }
  195. int main() {
  196. return day2_main();
  197. }