commit 400c6f642ff1542a8969b193d7b017e16d84cd38
parent ec24e658ab893c65e8931676d3035292ca7f8bb2
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 17 Jan 2021 01:27:02 +0100
Add general utils and day 1 part 1 solution
Diffstat:
7 files changed, 85 insertions(+), 45 deletions(-)
diff --git a/data/helper/template/main.c b/data/helper/template/main.c
@@ -5,11 +5,13 @@
#include "util.h"
void
-part1(struct partinfo *p) {
+part1(struct partinfo *p)
+{
}
void
-part2(struct partinfo *p) {
+part2(struct partinfo *p)
+{
}
diff --git a/libs/include/partinfo.h b/libs/include/partinfo.h
@@ -4,14 +4,14 @@
#include <stdlib.h>
struct partinfo {
- int debug;
- int part;
+ int debug;
+ int part;
- const char *inputfile;
- char *input;
- size_t input_size;
+ const char *inputfile;
+ char *input;
+ size_t input_size;
- const char **args;
+ const char **args;
};
#endif // AOC_PARTINFO_H
diff --git a/libs/include/util.h b/libs/include/util.h
@@ -5,15 +5,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <string.h>
-enum { SUCCESS, FAILURE };
+#define die(...) exitmsg(1, __VA_ARGS__)
+#define quit(...) exitmsg(0, __VA_ARGS__)
-#define die(...) exitmsg(FAILURE, __VA_ARGS__)
-#define quit(...) exitmsg(SUCCESS, __VA_ARGS__)
+enum { OK, FAIL };
+
+enum { MID_TOK, LAST_TOK };
void exitmsg(int rv, const char *fmtstr, ...);
void debug(const char *fmtstr, ...);
+int ntoken(const char *start, char **ntok, size_t *len, char sep);
+
int min(int a, int b);
int max(int a, int b);
diff --git a/libs/include/wrapper.h b/libs/include/wrapper.h
@@ -4,4 +4,3 @@
void part1(struct partinfo *p);
void part2(struct partinfo *p);
-
diff --git a/libs/src/util.c b/libs/src/util.c
@@ -3,23 +3,39 @@
void
exitmsg(int rv, const char *fmtstr, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, fmtstr);
- vprintf(fmtstr, ap);
- va_end(ap);
+ va_start(ap, fmtstr);
+ vprintf(fmtstr, ap);
+ va_end(ap);
- exit(rv);
+ exit(rv);
}
void
debug(const char *fmtstr, ...)
{
- va_list ap;
+ va_list ap;
- va_start(ap, fmtstr);
- vfprintf(stderr, fmtstr, ap);
- va_end(ap);
+ va_start(ap, fmtstr);
+ vfprintf(stderr, fmtstr, ap);
+ va_end(ap);
+}
+
+int
+ntoken(const char *start, char **ntok, size_t *len, char sep)
+{
+ char *tokp;
+
+ if ((tokp = strchr(start, sep))) {
+ *len = tokp - start;
+ *ntok = tokp + 1;
+ return MID_TOK;
+ } else {
+ *len = strlen(start);
+ *ntok = NULL;
+ return LAST_TOK;
+ }
}
int
@@ -42,12 +58,13 @@ assertp(void *alloc)
}
int
-readall(int fileno, void **data, size_t *totalread) {
+readall(int fileno, void **data, size_t *totalread)
+{
size_t allocsize = 10, want = allocsize, got;
*data = assertp(malloc(allocsize));
-
*totalread = 0;
+
while ((got = read(fileno, *data + *totalread, want)) > 0) {
if (got == want) {
allocsize *= 2;
@@ -59,5 +76,5 @@ readall(int fileno, void **data, size_t *totalread) {
want = allocsize - *totalread;
}
- return (got < 0) ? FAILURE : SUCCESS;
+ return (got < 0) ? FAIL : OK;
}
diff --git a/libs/src/wrapper.c b/libs/src/wrapper.c
@@ -3,27 +3,27 @@
int
main(int argc, const char **argv)
{
- struct partinfo info;
- const char *envvar;
- FILE *f;
+ struct partinfo info;
+ const char *envvar;
+ FILE *f;
- if (argc <= 1) die("Supply the part number to solve\n");
- info.args = &argv[2];
+ if (argc <= 1) die("Supply the part number to solve\n");
+ info.args = &argv[2];
- info.inputfile = getenv("AOCINPUT");
- if (!info.inputfile) info.inputfile = "input";
- else debug("Using input file: '%s'\n", info.inputfile);
+ info.inputfile = getenv("AOCINPUT");
+ if (!info.inputfile) info.inputfile = "input";
+ else debug("Using input file: '%s'\n", info.inputfile);
- envvar = getenv("AOCDEBUG");
- info.debug = envvar ? atoi(envvar) : 0;
+ envvar = getenv("AOCDEBUG");
+ info.debug = envvar ? atoi(envvar) : 0;
- if (!(f = fopen("input", "r"))) die("Failed to open input file\n");
- if (readall(fileno(f), (void**) &info.input, &info.input_size) == FAILURE)
- die("Failed to read from input file\n");
- fclose(f);
+ if (!(f = fopen("input", "r"))) die("Failed to open input file\n");
+ if (readall(fileno(f), (void**) &info.input, &info.input_size) == FAIL)
+ die("Failed to read from input file\n");
+ fclose(f);
- info.part = atoi(argv[1]);
- if (info.part == 1) part1(&info);
- else if (info.part == 2) part2(&info);
- else die("Invalid part number\n");
+ info.part = atoi(argv[1]);
+ if (info.part == 1) part1(&info);
+ else if (info.part == 2) part2(&info);
+ else die("Invalid part number\n");
}
diff --git a/src/day1/main.c b/src/day1/main.c
@@ -1,15 +1,32 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "partinfo.h"
#include "util.h"
void
-part1(struct partinfo *p) {
-
+part1(struct partinfo *p)
+{
+ char *line = p->input, *nline, *endp = NULL;
+ size_t len;
+ int status;
+ long long ans;
+
+ do {
+ status = ntoken(line, &nline, &len, '\n');
+ if (!len) continue;
+ line[len] = 0;
+ ans += strtoul(line, &endp, 10) / 3 - 2;
+ if (endp && *endp != 0) die("Failed to parse input '%s' as integer\n", line);
+ line = nline;
+ } while (status != LAST_TOK);
+
+ printf("%llu\n", ans);
}
void
-part2(struct partinfo *p) {
+part2(struct partinfo *p)
+{
}