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

#include "lfinput.h"
#include "advent_utility.h"

void advent2024day03(void) {
    char *input = get_input("input/2024/03");
    size_t m_sz = 0;
    char **matches = get_matches(input, "mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)", &m_sz, 32);
    int p1 = 0, p2 = 0, x, y, enabled = 1;

    for (size_t i = 0; i < m_sz; i++) {
        if (strcmp(matches[i], "do()") == 0) {
            enabled = 1;
        } else if (strcmp(matches[i], "don't()") == 0) {
            enabled = 0;
        }

        if (strstr(matches[i], "mul") != NULL) {
            sscanf(matches[i], "mul(%d,%d)", &x, &y);
            p1 += x * y;
            if (enabled) {
                p2 += x * y;
            }
        }
    }

    printf("%d\n", p1);
    printf("%d\n", p2);

    free_matches(matches, m_sz);
    free(input);
}