#include "aoc.h" #include "util.h" #include #include struct aoc aoc; int main(int argc, const char **argv) { const char *envvar; FILE *f; if (argc <= 1) { fprintf(stderr, "Usage: main PART [ARG]..\n"); exit(0); } aoc.part = atoi(argv[1]); aoc.argc = argc - 2; aoc.argv = &argv[2]; envvar = getenv("AOC_DEBUG"); aoc.debug = envvar ? atoi(envvar) : 0; /* parse user input */ aoc.inputfile = getenv("AOC_INPUT"); if (!aoc.inputfile) aoc.inputfile = "input"; else aoc_debug("Using input file: '%s'\n", aoc.inputfile); /* read input file */ if (!(f = fopen(aoc.inputfile, "r"))) err(1, "fopen %s", aoc.inputfile); readall(f, (void**) &aoc.input, &aoc.input_size); fclose(f); /* add null terminator */ aoc.input = realloc(aoc.input, aoc.input_size + 1); if (!aoc.input) err(1, "realloc"); aoc.input[aoc.input_size] = '\0'; /* call solution */ aoc.answer = NULL; aoc.solution = NULL; if (aoc.part == 1) part1(); else if (aoc.part == 2) part2(); else errx(1, "Invalid part number"); /* check answer if given */ if (aoc.answer) { printf("%s\n", aoc.answer); if (!strcmp(aoc.inputfile, "input")) { if (aoc.solution && strcmp(aoc.answer, aoc.solution)) errx(1, "Incorrect solution!"); else if (!aoc.solution) errx(1, "Solution missing!"); } } free(aoc.answer); free(aoc.input); }