part 1 day 1 working. Now for 2

This commit is contained in:
Preston Baxter 2023-12-04 23:07:15 -06:00
parent 4217ded01b
commit 86e6128486
11 changed files with 1136 additions and 9 deletions

9
day1/Makefile Normal file
View File

@ -0,0 +1,9 @@
BASENAME= $(shell basename `pwd`)
build:
rm $(BASENAME) || echo ""
gcc $(BASENAME).c ../libs/* -o $(BASENAME)
run: build
./$(BASENAME) input.txt a
./$(BASENAME) input.txt b

BIN
day1/day1

Binary file not shown.

View File

@ -1,10 +1,21 @@
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../libs/buffered_reader.h"
#include "../libs/utils.h"
int decodeA(const char *str);
int decodeB(const char *str);
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("usage: day1 INPUT_FILE a|b\n");
exit(1);
}
filelines *lines = flines(argv[1]);
if (lines == NULL) {
@ -12,9 +23,58 @@ int main(int argc, char *argv[]) {
exit(1);
}
int sum = 0;
for (int i = 0; i < lines->size; i++) {
printf("%s\n", get(lines, i));
if (argv[2][0] == 'b') {
sum+= decodeB(lines->lines[i]);
} else if (argv[2][0] == 'a') {
sum += decodeA(lines->lines[i]);
}
}
printf("%d\n", sum);
return 0;
}
int decodeA(const char *str) {
int tens = -1;
int ones = -1;
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 (!isalpha(back) && ones < 0) {
ones = back - 48;
}
printf("front %c, tens %d, back %d, ones %d\n", front, tens, back, ones);
}
return tens * 10 + ones;
}
int decodeB(const char *str) {
int tens = -1;
int ones = -1;
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 (!isalpha(back) && ones < 0) {
ones = back - 48;
}
printf("front %c, tens %d, back %d, ones %d\n", front, tens, back, ones);
}
return tens * 10 + ones;
}

File diff suppressed because it is too large Load Diff

6
day1/out.txt Normal file
View File

@ -0,0 +1,6 @@
rm day1 || echo ""
gcc day1.c ../libs/* -o day1
./day1 input.txt a
53194
./day1 input.txt b
53706

1
day1/testb.txt Normal file
View File

@ -0,0 +1 @@
eightwothree

View File

@ -13,7 +13,7 @@ filelines* flines(const char* filename) {
//Define loop things
bool loop = true;
char *buffer;
size_t bufsize = 128;
size_t bufsize = 8;
size_t characters;
filelines *lines;
@ -23,21 +23,29 @@ filelines* flines(const char* filename) {
//loop over file
if (file) {
while (loop) {
buffer = (char *)malloc(bufsize * sizeof(char));
int size = bufsize;
buffer = (char *)malloc(size * sizeof(char));
int i = 0;
while((c = getc(file)) != EOF) {
if (c == '\n' || i == bufsize) {
printf("breaking\n");
if (c == '\n') {
break;
}
if (i == size) {
size <<= 1;
buffer = (char*)realloc(buffer, size * sizeof(char));
}
//TODO: resize string if input is too big
printf("Adding %d\n", c);
buffer[i] = c;
i += 1;
}
if (i > 0){
if (i == size) {
buffer = (char*)realloc(buffer, size * sizeof(char) + 1);
}
buffer[i] = '\0';
insert(lines, buffer);
}
if (c == EOF) {

0
libs/data_structures.c Normal file
View File

20
libs/data_structures.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef data_structures
#include <stdlib.h>
typedef struct {
size_t size;
size_t capacity;
size_t head;
size_t tail;
char* arr;
}list;
void initList(list **l);
void pushs(list *l, char c);
char pops(list *l);
void enqueue(list *l, char c);
char dequeue(list *l);
#endif

20
libs/utils.c Normal file
View File

@ -0,0 +1,20 @@
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strreplace(char *s, const char *s1, const char *s2) {
char *p = strstr(s, s1);
while (p != NULL){
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
if (len1 != len2){
memmove(p + len2, p + len1, strlen(p + len1) + 1);
}
memcpy(p, s2, len2);
p = strstr(s, s1);
}
return s;
}

6
libs/utils.h Normal file
View File

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