Advent of Code

Artifact [fbd36b6015]
Login

Artifact [fbd36b6015]

Artifact fbd36b601519ee1e33224bb5de02d7310d5b28cd126f1a14925cda35bda64b61:


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

struct House {
    int row, col;
};

#ifndef MAX_HOUSES
#  define MAX_HOUSES 2400
#else
#  error MAX_HOUSES already defined
#endif
void aoc201503(char *data, size_t len) {
    (void)len; // unused argument
    struct House house[MAX_HOUSES] = {0};
    unsigned nhouse = 1; // house at [0, 0] visited
    int x = 0, y = 0;
    while (*data) {
        switch (*data) {
            default: break;
            case '^': y++; break;
            case 'v': y--; break;
            case '>': x++; break;
            case '<': x--; break;
        }
        data++;
        bool visited = false;
        for (size_t k = 0; k < nhouse; k++) {
            if ((house[k].row == x) && (house[k].col == y)) {
                visited = true;
                break;
            }
        }
        if (!visited) {
            if (nhouse == MAX_HOUSES) {
                fprintf(stderr, "Need more houses in aoc201503()\n");
                exit(EXIT_FAILURE);
            }
            house[nhouse].row = x;
            house[nhouse].col = y;
            nhouse += 1;
        }
    }
    printf("Santa delivered at least 1 present to %u houses.\n", nhouse);
}
#undef MAX_HOUSES

void aoc201502(char *data, size_t len) {
    (void)len; // unused argument
    unsigned sqf = 0, f = 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;
        unsigned M = max3u(l, w, h);
        unsigned feet = 2*(l+w+h - M) + l*h*w;
        f += feet;
    }
    printf("The elves need %u square feet of paper.\n", sqf);
    printf("The elves need %u feet of ribbon.\n", f);
}

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);
}