part 1 day 1 working. Now for 2
This commit is contained in:
parent
4217ded01b
commit
86e6128486
|
@ -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
|
62
day1/day1.c
62
day1/day1.c
|
@ -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;
|
||||
}
|
||||
|
|
1003
day1/input.txt
1003
day1/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
|||
rm day1 || echo ""
|
||||
gcc day1.c ../libs/* -o day1
|
||||
./day1 input.txt a
|
||||
53194
|
||||
./day1 input.txt b
|
||||
53706
|
|
@ -0,0 +1 @@
|
|||
eightwothree
|
|
@ -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,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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef utils
|
||||
|
||||
|
||||
char *strreplace(char *s, const char *s1, const char *s2);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue