|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #include <math.h>
- #include "core.hpp"
-
- int day1() {
- Arena arena = createArena(Megabytes(16));
-
- list<char> 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<char> file = readEntireFile(&arena, strlit("./day-2-input"));
-
- int safes = 0;
- int lines = 0;
- int fileReadIndex = 0;
- list<int> nums = PushList(&arena, int, 100);
- list<char> numStr = PushList(&arena, char, 16);
- list<int> 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<char> 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 arena = createArena(Megabytes(16));
- list<char> 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;
- }
-
- list<char> fileStripped = PushList(&arena, char, file.length - lines);
- for (size_t i = 0; i < file.length; i++) {
- if (file.data[i] != '\n') {
- appendList(&fileStripped, (char)file.data[i]);
- }
- }
-
- string xmas = strlit("XMAS");
- int c = 0;
- int count = 0;
- for (int c = 0; c < fileStripped.length; c++) {
- string segment = strSlice(fileStripped.data, c, c+xmas.len);
- const char *print = cstring(&arena, segment);
- printf("%s\n", print);
- if (strEql(segment, xmas)) {
- c += xmas.len;
- count++;
- }
- }
-
- printf("count: %i\n", count);
-
- return 0;
- }
-
- int main() {
- return day4_main();
- }
|