#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "aocutils.h"
#include "aocdailies.h" // prototypes for all aocYYYYdd functions!

#define MAX_YEAR 2024

int main(int argc, char **argv) {
    unsigned y = 0, d = 0;
    char dataname[99];
    if (argc >= 3) {
        char *err;
        y = strtoul(argv[1], &err, 10);
        if (err && *err) y = 0;
        if (y < 2015) y = 0;
        if (y > MAX_YEAR) y = 0;
        d = strtoul(argv[2], &err, 10);
        if (err && *err) d = 0;
        if (d < 1) d = 0;
        if (d > 25) d = 0;
        if (argc >= 4) {
            sprintf(dataname, "%.98s", argv[3]);
        } else {
            sprintf(dataname, ".%04u%02u.txt", y, d);
        }
    }
    if ((y == 0) || (d == 0)) {
        fprintf(stderr, "syntax: %s yyyy dd [input-file]\n", argv[0]);
        fprintf(stderr, "    where 2015 <= yyyy <= %d\n", MAX_YEAR);
        fprintf(stderr, "    and 1 <= dd <= 25\n");
        fprintf(stderr, "if no [input-file] is supplied loads the file .yyyydd.txt\n");
        exit(EXIT_FAILURE);
    }
    // read data from dataname into input
    char *input = NULL;
    size_t ilen = slurp(&input, dataname);

    // call the right function
    aocfunc *p = aocselect(y, d);
    if (p) p(input, ilen);
    else fprintf(stderr, "Impossible!\n");
    free(input);
}
