sign off commit. No part 2

This commit is contained in:
Preston Baxter 2023-12-04 23:46:55 -06:00
parent 86e6128486
commit 8e325085e7
4 changed files with 125 additions and 13 deletions

View File

@ -5,11 +5,14 @@
#include <string.h>
#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;

View File

@ -0,0 +1,79 @@
#include "data_structures.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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

View File

@ -1,6 +1,5 @@
#ifndef utils
char *strreplace(char *s, const char *s1, const char *s2);
#endif