commit d45e61e2dd478be8507e2c7cfdbaa24f720dbaf6
parent cdaaab5d63e1b17f8549bf201b53c74b2473d949
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 21 Dec 2020 00:24:02 +0100
Extend helper templates and helper lib
Diffstat:
20 files changed, 361 insertions(+), 31 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+compile_commands.json
+main
+.cache
diff --git a/data/config b/data/config
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-# year you are solving problems for
-export AOCYEAR=2019
-
-# directory you want day directories to be prepared in (format: src/dayN/..)
-export SRCDIR="src"
-
-# specify what files to copy to your day directory on prepare
-export TEMPLATE_DIR="data/template"
-export TEMPLATE_FILES="
-main.c:main.c
-makefile:makefile
-"
-
diff --git a/data/helper/config b/data/helper/config
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+# year you are solving problems for
+export AOCYEAR=2019
+
+# directory you want day directories to be prepared in (format: src/dayN/..)
+export SRCDIR="src"
+
+# specify what files to copy to your day directory on prepare
+export TEMPLATE_DIR="data/helper/template"
+export TEMPLATE_FILES="
+main.c:main.c
+Makefile:Makefile
+"
+
diff --git a/data/helper/init.sh b/data/helper/init.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+REPOROOT=$(git rev-parse --show-toplevel)
+
+ln -sf "$REPOROOT/data/helper/config" "$REPOROOT/helper/data/config"
+
+for f in "$REPOROOT/data/helper/scripts"/*; do
+ ln -sf "$f" "$REPOROOT/helper/scripts"
+done
diff --git a/data/helper/template/Makefile b/data/helper/template/Makefile
@@ -0,0 +1,9 @@
+CFLAGS = -I ../../libs/include
+LDLIBS =
+
+all: lib main
+
+lib:
+ make -C ../../libs
+
+main: main.c
diff --git a/data/helper/template/main.c b/data/helper/template/main.c
@@ -0,0 +1,17 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "partinfo.h"
+#include "util.h"
+
+void
+part1(struct PartInfo *p) {
+
+}
+
+void
+part2(struct PartInfo *p) {
+
+}
+
+#include "main.h"
diff --git a/data/init-helper.sh b/data/init-helper.sh
@@ -1,5 +0,0 @@
-#!/bin/bash
-
-REPOROOT=$(git rev-parse --show-toplevel)
-
-ln -sf "$REPOROOT/data/config" "$REPOROOT/helper/data/config"
diff --git a/data/template/main.c b/data/template/main.c
@@ -1,7 +0,0 @@
-
-
-int
-main()
-{
-
-}
diff --git a/data/template/makefile b/data/template/makefile
@@ -1,4 +0,0 @@
-
-
-
-main: main.c
diff --git a/libs/Makefile b/libs/Makefile
@@ -0,0 +1,13 @@
+CFLAGS = -I include
+LDLIBS =
+
+all: build/libaoc.a
+
+build:
+ mkdir build
+
+build/util.o: src/util.c | build
+ $(CC) -c -o $@ $^ $(CFLAGS) $(LDLIBS)
+
+build/libaoc.a: build/util.o
+ ar rcs $@ $^
diff --git a/libs/build/libaoc.a b/libs/build/libaoc.a
Binary files differ.
diff --git a/libs/build/util.o b/libs/build/util.o
Binary files differ.
diff --git a/libs/include/main.h b/libs/include/main.h
@@ -0,0 +1,29 @@
+#include "util.h"
+
+int
+main(int argc, const char **argv)
+{
+ struct PartInfo info;
+ const char *envvar;
+ FILE *f;
+
+ 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);
+
+ 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);
+
+ 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/libs/include/partinfo.h b/libs/include/partinfo.h
@@ -0,0 +1,17 @@
+#ifndef AOC_PARTINFO_H
+#define AOC_PARTINFO_H
+
+#include <stdlib.h>
+
+struct PartInfo {
+ int debug;
+ int part;
+
+ const char *inputfile;
+ char *input;
+ size_t input_size;
+
+ const char **args;
+};
+
+#endif // AOC_PARTINFO_H
diff --git a/libs/include/util.h b/libs/include/util.h
@@ -0,0 +1,24 @@
+#ifndef AOC_UTIL_H
+#define AOC_UTIL_H
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+enum { SUCCESS, FAILURE };
+
+#define die(...) exitmsg(FAILURE, __VA_ARGS__)
+#define quit(...) exitmsg(SUCCESS, __VA_ARGS__)
+
+void exitmsg(int rv, const char *fmtstr, ...);
+void debug(const char *fmtstr, ...);
+
+int min(int a, int b);
+int max(int a, int b);
+
+void* assertp(void *alloc);
+
+int readall(int fd, void **data, size_t *read);
+
+#endif // AOC_UTIL_H
diff --git a/libs/src/util.c b/libs/src/util.c
@@ -0,0 +1,63 @@
+#include "util.h"
+
+void
+exitmsg(int rv, const char *fmtstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmtstr);
+ vprintf(fmtstr, ap);
+ va_end(ap);
+
+ exit(rv);
+}
+
+void
+debug(const char *fmtstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmtstr);
+ vfprintf(stderr, fmtstr, ap);
+ va_end(ap);
+}
+
+int
+min(int a, int b)
+{
+ return a < b ? a : b;
+}
+
+int
+max(int a, int b)
+{
+ return a > b ? a : b;
+}
+
+void*
+assertp(void *alloc)
+{
+ if (!alloc) die("Out of Memory\n");
+ return alloc;
+}
+
+int
+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;
+ *totalread += got;
+ *data = assertp(realloc(*data, allocsize));
+ } else {
+ *totalread += got;
+ }
+ want = allocsize - *totalread;
+ }
+
+ return (got < 0) ? FAILURE : SUCCESS;
+}
diff --git a/src/day1/Makefile b/src/day1/Makefile
@@ -0,0 +1,9 @@
+CFLAGS = -I ../../libs/include
+LDLIBS = -L ../../libs/build -laoc
+
+all: lib main
+
+lib:
+ make -C ../../libs
+
+main: main.c
diff --git a/src/day1/input b/src/day1/input
@@ -0,0 +1,100 @@
+102473
+84495
+98490
+68860
+62204
+72810
+65185
+145951
+77892
+108861
+70764
+67286
+74002
+80773
+52442
+131505
+107162
+126993
+59784
+64231
+91564
+68585
+98735
+69020
+77332
+60445
+65826
+111506
+95431
+146687
+135119
+86804
+95915
+85434
+111303
+148127
+132921
+136213
+89004
+143137
+144853
+143017
+104386
+100612
+54760
+63813
+144191
+84481
+69718
+84936
+98621
+124993
+92736
+60369
+137284
+101902
+112726
+51784
+126496
+85005
+101661
+137278
+136637
+90340
+100209
+53683
+50222
+132060
+98797
+139054
+135638
+100632
+137849
+125333
+103981
+76954
+134352
+74229
+93402
+62552
+50286
+57066
+98439
+120708
+117827
+107884
+72837
+148663
+125645
+61460
+120555
+142473
+106668
+58612
+58576
+143366
+90058
+121087
+89546
+126161
diff --git a/src/day1/main.c b/src/day1/main.c
@@ -0,0 +1,17 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "partinfo.h"
+#include "util.h"
+
+void
+part1(struct PartInfo *p) {
+
+}
+
+void
+part2(struct PartInfo *p) {
+
+}
+
+#include "main.h"
diff --git a/src/day1/part1 b/src/day1/part1
@@ -0,0 +1,36 @@
+--- Day 1: The Tyranny of the Rocket Equation ---
+
+Santa has become stranded at the edge of the Solar System while delivering presents to other
+planets! To accurately calculate his position in space, safely align his warp drive, and return to
+Earth in time to save Christmas, he needs you to bring him measurements from [1m[33mfifty
+stars[0m.
+
+Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent
+calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants
+[1m[33mone star[0m. Good luck!
+
+The Elves quickly load you into a spacecraft and prepare to launch.
+
+At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined
+the amount of fuel required yet.
+
+Fuel required to launch a given [1m[37mmodule[0m is based on its [1m[37mmass[0m.
+Specifically, to find the fuel required for a module, take its mass, divide by three, round down,
+and subtract 2.
+
+For example:
+
+
+ - For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
+ - For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
+ - For a mass of 1969, the fuel required is 654.
+ - For a mass of 100756, the fuel required is 33583.
+
+
+The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate
+the fuel needed for the mass of each module (your puzzle input), then add together all the fuel
+values.
+
+[1m[37mWhat is the sum of the fuel requirements[0m for all of the modules on your spacecraft?
+
+