Browse Source

Add Linux support, write input functions

master
Evan Burkey 2 months ago
parent
commit
c5dfbd6911
  1. 2
      .gitignore
  2. 16
      GNUmakefile
  3. 15
      Makefile
  4. 6
      README.md
  5. 3
      generator.sh
  6. 7
      get_input.sh
  7. 11
      include/input.h
  8. 78
      src/input.c
  9. 1
      src/main.c

2
.gitignore

@ -1,2 +1,4 @@
input/
advent
compile_commands.json
.cache

16
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

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

6
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

3
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

7
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

11
include/input.h

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

@ -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
src/main.c

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bsd/stdlib.h>
#include "advent2015.h"
#include "advent2016.h"

Loading…
Cancel
Save