diff --git a/day1/day1.c b/day1/day1.c index eb5ce08..8f6504a 100644 --- a/day1/day1.c +++ b/day1/day1.c @@ -5,11 +5,14 @@ #include #include "../libs/buffered_reader.h" -#include "../libs/utils.h" +#include "../libs/data_structures.h" int decodeA(const char *str); int decodeB(const char *str); +list *fl; +list *bl; + int main(int argc, char *argv[]) { if (argc < 3) { printf("usage: day1 INPUT_FILE a|b\n"); @@ -23,13 +26,18 @@ int main(int argc, char *argv[]) { exit(1); } + if (argv[2][0] == 'b') { + initList(&fl); + initList(&bl); + } + int sum = 0; for (int i = 0; i < lines->size; i++) { if (argv[2][0] == 'b') { - sum+= decodeB(lines->lines[i]); + sum += decodeB(lines->lines[i]); } else if (argv[2][0] == 'a') { - sum += decodeA(lines->lines[i]); - } + sum += decodeA(lines->lines[i]); + } } printf("%d\n", sum); @@ -40,17 +48,37 @@ int main(int argc, char *argv[]) { int decodeA(const char *str) { int tens = -1; int ones = -1; + clears(fl); + clears(bl); int len = strlen(str); for (int i = 0; i < len; i++) { int front = str[i]; int back = str[len - 1 - i]; - if (!isalpha(front) && tens < 0) { - tens = front - 48; + if (tens < 0) { + if (isalpha(front)) { + pushs(fl, front); + char *fw = contents(fl); + front = isnumber(fw); + if (front > 0) { + tens = front - 48; + } + } else { + tens = front - 48; + } } - if (!isalpha(back) && ones < 0) { - ones = back - 48; + if (ones < 0) { + if (isalpha(back)) { + pushs(bl, front); + char *fw = contents(fl); + front = isnumber(fw); + if (front > 0) { + tens = front - 48; + } + } else { + tens = front - 48; + } } printf("front %c, tens %d, back %d, ones %d\n", front, tens, back, ones); } @@ -67,8 +95,10 @@ int decodeB(const char *str) { int front = str[i]; int back = str[len - 1 - i]; - if (!isalpha(front) && tens < 0) { - tens = front - 48; + if (!isalpha(front)) { + if (tens < 0) + tens = front - 48; + } else { } if (!isalpha(back) && ones < 0) { ones = back - 48; diff --git a/libs/data_structures.c b/libs/data_structures.c index e69de29..d7aa19b 100644 --- a/libs/data_structures.c +++ b/libs/data_structures.c @@ -0,0 +1,79 @@ +#include "data_structures.h" + +#include +#include +#include + +#define STARTING_SIZE 8 + +void initList(list **l) { + list *li; + li = (list *)malloc(sizeof(list)); + if (!li) { + printf("Failed to allocate memory for list\n"); + exit(1); + } + + li->size = 0; + li->capacity = STARTING_SIZE; + li->head = -1; + li->tail = 0; + li->arr = (char *)malloc(STARTING_SIZE * sizeof(char)); + if (!li->arr) { + printf("Failed to allocate memory for arr in list\n"); + exit(1); + } + + *l = li; +} + +void pushs(list *l, char c) { + if (l->head + 1 == l->capacity) { + l->capacity <<= 1; + l->arr = (char *)realloc(l->arr, l->capacity * sizeof(char)); + } + + l->head += 1; + l->size += 1; + l->arr[l->head] = c; +} + +char popd(list *l, char c) { + if (l->size == 0 || l->head == -1) { + return -1; + } + + l->head -= 1; + l->size -= 1; + return l->arr[l->head + 1]; +} + +//This is a bad queue. Will leave space at the front. Need to figure that out... +//resizable ring buffer? +void enqueues(list *l, char c) { + pushs(l, c); +} + +char dequeues(list *l) { + if (l->size == 0 || l->tail == l->head){ + return -1; + } + + l->tail += 1; + l->size -= 1; + return l->arr[l->tail - 1]; +} + +char* contents(list *l) { + char *out = (char*)malloc(l->size * sizeof(char) + 1); + memcpy(out, l->arr + l->tail, l->size + 1); + out[l->size] = '\0'; + + return out; +} + +void clears(list *l) { + l->head=-1; + l->size=0; + l->tail=0; +} diff --git a/libs/data_structures.h b/libs/data_structures.h index 378eeca..9152551 100644 --- a/libs/data_structures.h +++ b/libs/data_structures.h @@ -13,8 +13,12 @@ typedef struct { void initList(list **l); void pushs(list *l, char c); char pops(list *l); -void enqueue(list *l, char c); -char dequeue(list *l); +void enqueues(list *l, char c); +char dequeues(list *l); +void clears(list *l); + +//returns copy of contents as string +char* contents(list *l); #endif diff --git a/libs/utils.h b/libs/utils.h index c32323f..526a1af 100644 --- a/libs/utils.h +++ b/libs/utils.h @@ -1,6 +1,5 @@ #ifndef utils - char *strreplace(char *s, const char *s1, const char *s2); #endif