|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
-
- #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
-
- typedef int8_t int8;
- typedef int16_t int16;
- typedef int32_t int32;
- typedef int64_t int64;
- typedef uint8_t uint8;
- typedef uint16_t uint16;
- typedef uint32_t uint32;
- typedef uint64_t uint64;
- typedef uint8 byte;
- typedef uint16 b16;
- typedef uint32 b32;
- typedef uint64 b64;
- typedef float real32;
- typedef double real64;
-
- struct string {
- uint8 *str;
- size_t len;
- };
-
- struct StringNode {
- string *str;
- size_t start;
- size_t end;
- };
-
- string asString(StringNode *node) {
- return {
- node->str->str + node->start,
- node->end - node->start,
- };
- }
-
- string createStr(byte *str, size_t len) {
- string result;
- result.str = str;
- result.len = len;
- return result;
- }
-
- #define strlit(lit) createStr((byte *)(lit), sizeof(lit) - 1)
-
- #define Bytes(n) (n)
- #define Kilobytes(n) (n << 10)
- #define Megabytes(n) (n << 20)
- #define Gigabytes(n) (((uint64)n) << 30)
- #define Terabytes(n) (((uint64)n) << 40)
-
- #define Thousand(n) ((n)*1000)
- #define Million(n) ((n)*1000000)
- #define Billion(n) ((n)*1000000000LL)
-
- struct Arena {
- void *memory;
- size_t capacity;
- size_t head;
- };
-
- void *pushSize(Arena *arena, size_t size) {
- if (arena->capacity - arena->head >= size) {
- arena->head += size;
- return (char *)arena->memory + arena->head + size;
- }
- return 0;
- }
-
- #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
- #define PushStruct(arena, type, size) (type *)pushSize(arena, sizeof(type))
-
- Arena createArena(size_t capacity) {
- Arena result = {};
- result.memory = mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- result.capacity = capacity;
- result.head = 0;
- return result;
- }
-
- size_t getFileSize(string file) {
- struct stat st;
- stat((char *)file.str, &st);
- return st.st_size;
- }
-
- struct Buffer {
- size_t size;
- byte *data;
- };
-
- Buffer readEntireFile(Arena *arena, string file) {
- FILE *input = fopen((char *)file.str, "r");
- size_t filesize = getFileSize(file);
- byte *readBuffer = PushArray(arena, byte, filesize);
- fread(readBuffer, sizeof(byte), filesize, input);
- fclose(input);
- return { filesize, readBuffer };
- }
-
- int cmpint(const void *a, const void *b) {
- int *x = (int *)a;
- int *y = (int *)b;
- return (*x > *y) - (*x < *y);
- }
-
- int day1() {
- Arena arena = createArena(Megabytes(16));
-
- Buffer file = readEntireFile(&arena, strlit("./day-1-input"));
-
- int LIST_SIZE = 1000;
- int *firsts = PushArray(&arena, int, LIST_SIZE);
- int *seconds = PushArray(&arena, int, LIST_SIZE);
-
- char currentNum[5];
- int numIndex = 0;
- int line = 0;
- int *target = firsts;
-
- for (int i = 0; i < file.size; i++) {
- char c = file.data[i];
- if (c == ' ') {
- continue;
- } else if (c == '\n') {
- line++;
- continue;
- } else {
- currentNum[numIndex++] = c;
- }
- if (numIndex == 5) {
- target[line] = atoi(currentNum);
- if (target == firsts) {
- target = seconds;
- } else {
- target = firsts;
- }
- numIndex = 0;
- }
- }
-
- qsort(firsts, line, sizeof(int), cmpint);
- qsort(seconds, line, sizeof(int), cmpint);
-
- int sum = 0;
- for (int i = 0; i < line; i++) {
- int dist = firsts[i] - seconds[i];
- if (dist < 0) {
- dist *= -1;
- }
- sum += dist;
- }
- printf("%i\n", sum);
-
- // Part 2
-
- int total = 0;
- for (int i = 0; i < line; i++) {
- int firstElement = firsts[i];
- int countInSecondList = 0;
- for (int j = 0; j < line; j++) {
- if (seconds[j] == firstElement) {
- countInSecondList++;
- }
- }
- total += firstElement * countInSecondList;
- }
- printf("%i\n", total);
-
- return 0;
- }
-
- int day2_main() {
- Arena arena = createArena(Megabytes(16));
- Buffer file = readEntireFile(&arena, strlit("./day-2-input"));
-
- int safes = 0;
- int lines = 0;
- int *nums = PushArray(&arena, int, 100);
- int numLen = 1;
- int index = 0;
- while (index < file.size) {
- int linestart = index;
- int lineend = index;
- while (file.data[lineend] != '\n' && lineend < file.size) {
- lineend++;
- }
- lines++;
- char numStr[16] = {};
- int numStrLen = 1;
- numLen = 1;
- for (int j = linestart; j < lineend; j++) {
- if (file.data[j] != ' ') {
- numStr[numStrLen++ - 1] = file.data[j];
- } else {
- numStr[numStrLen - 1] = '\0';
- nums[numLen++ - 1] = atoi(numStr);
- numStrLen = 1;
- }
- }
- bool increasing = nums[0] < nums[1];
- bool safe = true;
- for (int j = 0; j < numLen; j++) {
- int diff = increasing ? nums[j + 1] - nums[j] : nums[j] - nums[j + 1];
- if (increasing && (diff < 1 || diff > 3)) {
- safe = false;
- break;
- }
- }
- if (safe) {
- safes++;
- }
- index = lineend + 1;
- }
-
- printf("safes: %i", safes);
- printf("lines: %i", lines);
-
- return 0;
- }
-
- int main() {
- return day2_main();
- }
|