#include #include "core.hpp" int day1() { Arena arena = createArena(Megabytes(16)); list 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.length; 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)); list file = readEntireFile(&arena, strlit("./day-2-input")); int safes = 0; int lines = 0; int fileReadIndex = 0; list nums = PushList(&arena, int, 100); list numStr = PushList(&arena, char, 16); list modifiedNums = PushList(&arena, int, nums.capacity); while (fileReadIndex < file.length) { int linestart = fileReadIndex; int lineend = fileReadIndex; while (file.data[lineend] != '\n' && lineend < file.length) { lineend++; } lines++; zeroList(&nums); for (int j = linestart; j < lineend; j++) { if (file.data[j] != ' ') { appendList(&numStr, (char)file.data[j]); } if (file.data[j] == ' ' || j == lineend - 1) { appendList(&numStr, '\0'); appendList(&nums, atoi(numStr.data)); zeroList(&numStr); } } int unsafes = 0; for (int i = 0; i < nums.length + 1; i++) { int skipIndex = i - 1; zeroList(&modifiedNums); for (int modifiedIndex = 0; modifiedIndex < nums.length; modifiedIndex++) { if (modifiedIndex == skipIndex) { continue; } appendList(&modifiedNums, nums.data[modifiedIndex]); } bool increasing = modifiedNums.data[0] < modifiedNums.data[1]; bool safe = true; for (int checkIndex = 0; checkIndex < modifiedNums.length - 1; checkIndex++) { int next = checkIndex + 1; int diff = increasing ? modifiedNums.data[next] - modifiedNums.data[checkIndex] : modifiedNums.data[checkIndex] - modifiedNums.data[next]; if (diff < 1 || diff > 3) { safe = false; break; } } if (!safe) { unsafes += 1; } else { break; } } if (unsafes <= nums.length) { safes++; } fileReadIndex = lineend + 1; } printf("safes: %i", safes); printf("\n"); printf("lines: %i", lines); printf("\n"); printf("Arena: %zu, %zu", arena.head, arena.capacity); printf("\n"); return 0; } bool stringContains(string str, char c) { for (size_t i = 0; i < str.len; i++) { if (str.str[i] == c) { return true; } } return false; } const char NUMERIC_CHARS[] = "1234567890"; inline bool isNumeric(char c) { return stringContains(strlit(NUMERIC_CHARS), c); } inline int parsePositiveInt(string str, size_t *lengthPointer) { size_t numEnd = 0; char currChar = str.str[numEnd]; while (numEnd < str.len && isNumeric(currChar)) { currChar = str.str[++numEnd]; *lengthPointer += 1; } *lengthPointer -= 1; if (numEnd > 0) { int result = 0; for (int i = 0; i < numEnd; i++) { result *= 10; result += (double)(str.str[i] - '0'); } return result; } else { return -1; } } int day3_main() { Arena arena = createArena(Megabytes(16)); list file = readEntireFile(&arena, strlit("./day-3-input")); string mulBegin = strlit("mul("); string doIdent = strlit("do()"); string dontIdent = strlit("don't()"); int result = 0; size_t c = 0; bool skip = false; while (c < file.length) { if (!skip) { if (file.data[c] == 'm') { string segment = strSlice(file.data, c, c+mulBegin.len); int first = -1; int second = -1; if (strEql(segment, mulBegin)) { c += segment.len; first = parsePositiveInt(strSlice(file.data, c, file.length - c), &c); if (file.data[c + 1] == ',') { c += 2; second = parsePositiveInt(strSlice(file.data, c, file.length - c), &c); if (second != -1 && file.data[c + 1] == ')') { c += 1; result += first * second; } } } } else if (file.data[c] == 'd') { string segment = strSlice(file.data, c, c+dontIdent.len); if (strEql(segment, dontIdent)) { skip = true; c += dontIdent.len - 1; } } } else { if (file.data[c] == 'd') { string segment = strSlice(file.data, c, c+doIdent.len); if (strEql(segment, doIdent)) { skip = false; c += doIdent.len - 1; } } } c++; } printf("total: %i\n", result); return 0; } int day4_main() { Arena mainArena = createArena(Megabytes(16)); Arena *arena = &mainArena; list file = readEntireFile(arena, strlit("./day-4-input")); int fileReadIndex = 0; int lines = 0; int lineLength = 0; while (fileReadIndex < file.length) { int linestart = fileReadIndex; int lineend = fileReadIndex; while (file.data[lineend] != '\n' && lineend < file.length) { lineend++; } if (lineLength == 0) { lineLength = lineend - linestart; } else if (lineend - linestart != lineLength) { fputs("Bad input. One or more lines aren't the same length as the first line.", stderr); return 1; } lines++; fileReadIndex += lineLength + 1; } string xmas = strlit("XMAS"); int count = 0; for (int c = 0; c < file.length; c++) { string strings[] = { //forwards strSlice(file.data, c, c+xmas.len), //down strSlice(file.data, c, c+xmas.len), //forwardsReverse strReverse(arena, strSlice(file.data, c, c+xmas.len)), //downReverse strReverse(arena, strSlice(file.data, c, c+xmas.len)), }; for (size_t i = 0; i < ArrayCount(strings); i++) { if (strEql(strings[i], xmas)) { c += xmas.len; count++; } } } printf("count: %i\n", count); return 0; } int main() { return day4_main(); }