#include <stdio.h>
#include <stdlib.h>
#include "aocdailies.h"
#include "aocutils.h"

void aoc201502(char *data, size_t len) {
    (void)len; // unused argument
    unsigned sqf = 0;
    for (;;) {
        char *err;
        unsigned l = strtoul(data, &err, 10);
        if (*err == 0) break;
        data = err + 1; // skip 'x'
        unsigned w = strtoul(data, &err, 10);
        data = err + 1; // skip 'x'
        unsigned h = strtoul(data, &err, 10);
        data = err + 1; // skip newline
        unsigned lw = l * w;
        unsigned wh = w * h;
        unsigned hl = h * l;
        unsigned m = min3u(lw, wh, hl);
        sqf += 2*(lw + wh + hl) + m;
    }
    printf("The elves need %u square feet of paper.\n", sqf);
}

void aoc201501(char *data, size_t len) {
    (void)len; // unused argument
    int floor = 0, basementsteps = -1;
    char *ddata = data;                            // save start
    while (*data) {
        if (*data == '(') floor++;
        else if (*data == ')') floor--;
        data++;
        // if entered the basement, part 2 done
        if (floor == -1) {
            basementsteps = data - ddata;          // steps taken
            break;
        }
    }
    // continue with no regard to basement
    while (*data) {
        if (*data == '(') floor++;
        else if (*data == ')') floor--;
        data++;
    }
    printf("Santa is taken to floor {%d}.\n", floor);
    printf("Santa first goes to the basement at step {%d}.\n", basementsteps);
}
