Add Linux support, write input functions
This commit is contained in:
parent
983657cf3e
commit
e6bedd1d66
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
input/
|
||||
advent
|
||||
compile_commands.json
|
||||
.cache
|
||||
|
16
GNUmakefile
Normal file
16
GNUmakefile
Normal file
@ -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
|
15
Makefile
15
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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,11 @@
|
||||
#ifndef ADVENT_H_INPUT
|
||||
#define ADVENT_H_INPUT
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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
|
78
src/input.c
Normal file
78
src/input.c
Normal file
@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <bsd/stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <bsd/stdlib.h>
|
||||
|
||||
#include "advent2015.h"
|
||||
#include "advent2016.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user