Daniel Ledda 3 settimane fa
parent
commit
81bf17ae81
3 ha cambiato i file con 44 aggiunte e 24 eliminazioni
  1. +19
    -17
      app.cpp
  2. +1
    -1
      build
  3. +24
    -6
      core.hpp

+ 19
- 17
app.cpp Vedi File

@@ -229,8 +229,9 @@ int day3_main() {
}

int day4_main() {
Arena arena = createArena(Megabytes(16));
list<char> file = readEntireFile(&arena, strlit("./day-4-input"));
Arena mainArena = createArena(Megabytes(16));
Arena *arena = &mainArena;
list<char> file = readEntireFile(arena, strlit("./day-4-input"));

int fileReadIndex = 0;
int lines = 0;
@@ -251,23 +252,24 @@ int day4_main() {
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++;
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++;
}
}
}



+ 1
- 1
build Vedi File

@@ -1,3 +1,3 @@
#!/bin/bash

clang ./app.cpp -o ./target/app
gcc -g -g3 ./app.cpp -o ./target/app

+ 24
- 6
core.hpp Vedi File

@@ -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<T> *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<char> 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,


Caricamento…
Annulla
Salva