diff --git a/app.cpp b/app.cpp index 4a77615..025d3e8 100644 --- a/app.cpp +++ b/app.cpp @@ -229,8 +229,9 @@ int day3_main() { } int day4_main() { - Arena arena = createArena(Megabytes(16)); - list file = readEntireFile(&arena, strlit("./day-4-input")); + Arena mainArena = createArena(Megabytes(16)); + Arena *arena = &mainArena; + list file = readEntireFile(arena, strlit("./day-4-input")); int fileReadIndex = 0; int lines = 0; @@ -251,23 +252,24 @@ int day4_main() { fileReadIndex += lineLength + 1; } - list 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++; + 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++; + } } } diff --git a/build b/build index 984ffe3..41f7456 100755 --- a/build +++ b/build @@ -1,3 +1,3 @@ #!/bin/bash -clang ./app.cpp -o ./target/app +gcc -g -g3 ./app.cpp -o ./target/app diff --git a/core.hpp b/core.hpp index a9cd613..b456c64 100644 --- a/core.hpp +++ b/core.hpp @@ -42,10 +42,11 @@ struct Arena { 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; +void *pushSize(Arena *arena, size_t bytes) { + if (arena->capacity - arena->head >= bytes) { + void *ptr = (char *)arena->memory + arena->head; + arena->head += bytes; + return ptr; } return 0; } @@ -88,6 +89,9 @@ void zeroList(list *list) { memset(list->data, 0, list->capacity * sizeof(T)); } +#define foreach(it, list, type) for (size_t __i = 0; __i < list.length; __i++) { type it = list.data[__i]; +#define foreach_end() } + // ### Strings ### #define strlit(lit) ((string){(char *)(lit), sizeof(lit) - 1}) @@ -96,16 +100,18 @@ struct string { size_t len; }; +#define PushString(arena, length) ((string){ (char *)pushSize(arena, length), (length) }) + const char *cstring(Arena *arena, list buf) { char *arr = PushArray(arena, char, buf.length + 1); - memcpy(arr, buf.data, sizeof(char) * buf.length); + memmove(arr, buf.data, buf.length); arr[buf.length] = '\0'; return arr; } const char *cstring(Arena *arena, string str) { char *arr = PushArray(arena, char, str.len + 1); - memcpy(arr, str.str, str.len * sizeof(char)); + memmove(arr, str.str, str.len); arr[str.len] = '\0'; return arr; } @@ -122,6 +128,18 @@ bool strEql(string s1, string s2) { return true; } +string strReverse(Arena *arena, string str) { + string reversed = PushString(arena, str.len); + for ( + int mainIndex = str.len - 1, reversedIndex = 0; + mainIndex < str.len; + mainIndex--, reversedIndex++ + ) { + reversed.str[reversedIndex] = str.str[mainIndex]; + } + return reversed; +} + string strSlice(char *data, size_t start, size_t stop) { return { data + start,