diff --git a/.gitignore b/.gitignore index 6d787d7..a429d65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ input/ advent +compile_commands.json +.cache diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000..261d672 --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,16 @@ +CFLAGS= -std=c99 -Wall -pedantic -Iinclude -g +LDFLAGS= -lbsd +OUT= -o advent +SRC= src/*.c \ + src/2015/*.c \ + src/2016/*.c \ + src/2017/*.c \ + src/2018/*.c \ + src/2019/*.c \ + src/2020/*.c + +all: + cc ${CFLAGS} ${OUT} ${LDFLAGS} ${SRC} + +clean: + rm advent diff --git a/Makefile b/Makefile index 05dbd13..f769d76 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,16 @@ -CFLAGS= -std=c99 -Wall -pedantic -Iinclude -OUT= -o advent -SRC= src/*.c src/2015/*.c src/2016/*.c src/2017/*.c src/2018/*.c src/2019/*.c src/2020/*.c +CFLAGS= -std=c99 -Wall -pedantic -Iinclude -g +LDFLAGS= '' +OUT= -o advent +SRC= src/*.c \ + src/2015/*.c \ + src/2016/*.c \ + src/2017/*.c \ + src/2018/*.c \ + src/2019/*.c \ + src/2020/*.c all: - cc ${CFLAGS} ${OUT} ${SRC} + cc ${CFLAGS} ${OUT} ${LDFLAGS} ${SRC} clean: rm advent diff --git a/README.md b/README.md index 0b18224..0ed5a9e 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # aoc -Advent Of Code solutions using C99 +Advent Of Code solutions using C99. ## Building -This project relies on several libc extensions specific to OpenBSD. As such it will only build and run on OpenBSD. +This project relies on several BSD extensions to the stdlib. BSDs should be able to build the project out of the box. If on a BSD, `make` is enough to build the project. -`make` is enough to build the project +Linux users will need `libbsd` installed and use GNU Make with the supplied GNUmakefile, which should be automatically used with a simple `make`. ## Inputs diff --git a/generator.sh b/generator.sh index a1dcb13..4c4d3c4 100755 --- a/generator.sh +++ b/generator.sh @@ -80,7 +80,8 @@ EOF #include "input.h" void advent${year}day${d}(void) { - printf("Solution for Day ${d} of ${year} is not completed yet\n"); + char *input = get_input("input/${year}/${d}"); + printf("Solution for Day ${d} of ${year} is not completed yet\n"); } EOF done diff --git a/get_input.sh b/get_input.sh index f3d70d4..965c220 100755 --- a/get_input.sh +++ b/get_input.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash +rm -rf input for year in {2015..2020}; do - mkdir -p $year/input + mkdir -p input/$year for day in {1..25}; do if [[ day -lt 10 ]]; then d="0$day" @@ -9,7 +10,7 @@ for year in {2015..2020}; do d="$day" fi url="https://adventofcode.com/$year/day/$day/input" - curl --cookie "session=$1" $url | perl -pe 'chomp if eof' > $year/input/$d + curl --cookie "session=$1" $url | perl -pe 'chomp if eof' > input/$year/$d done - touch $year/input/test + touch input/$year/test done diff --git a/include/input.h b/include/input.h index e69de29..952c5b0 100644 --- a/include/input.h +++ b/include/input.h @@ -0,0 +1,11 @@ +#ifndef ADVENT_H_INPUT +#define ADVENT_H_INPUT + +#include + +char *get_input(const char *); +char **get_lines(const char *, size_t *); +int *get_ints(const char *, size_t *); +void del_lines(char **); + +#endif // ADVENT_H_INPUT diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..2479740 --- /dev/null +++ b/src/input.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +#include + +#include "input.h" + +char *get_input(const char *path) { + FILE *fp = NULL; + fp = fopen(path, "r"); + if (fp == NULL) { + printf("Failed to open %s. Exiting.\n", path); + exit(1); + } + + fseek(fp, 0, SEEK_END); + size_t fsz = ftell(fp); + rewind(fp); + + char* buf = NULL; + buf = malloc(fsz + 1); + if (buf == NULL) { + printf("Failed to malloc buf. Exiting.\n"); + exit(1); + } + + fread(buf, 1, fsz, fp); + buf[fsz] = '\0'; + fclose(fp); + + return buf; +} + +char **get_lines(const char *path, size_t *lsz) { + char **lines = NULL; + char *delim = "\n"; + char *s = get_input(path), *t = strtok(s, delim); + size_t n = 0; + + while (t != NULL) { + lines = realloc(lines, sizeof(char *) * ++n); + if (lines == NULL) { + printf("Failed to realloc lines buffer. Exiting.\n"); + exit(1); + } + lines[n - 1] = t; + t = strtok(NULL, delim); + } + + *lsz = n; + return lines; +} + +int *get_ints(const char *path, size_t *sz) { + char **lines = get_lines(path, sz); + int *i = malloc(sizeof(int) * *sz); + + for (size_t idx = 0; idx < *sz; idx++) { + int n; + const char *errstr; + n = strtonum(lines[idx], INT_MIN, INT_MAX, &errstr); + if (errstr) { + printf("Failed to convert %s to int. Exiting.\n", lines[idx]); + exit(1); + } + i[idx] = n; + } + + del_lines(lines); + return i; +} + +void del_lines(char **lines) { + free(lines[0]); + free(lines); +} diff --git a/src/main.c b/src/main.c index d0c6f39..f156c3e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "advent2015.h" #include "advent2016.h"