Ancestors

Toot

Written by Füsilier Breitlinger on 2024-12-03 at 20:16

Just for fun, here's a solution for day 3 of AoC written in standard C without using regexes:

[#]include <inttypes.h>

[#]include <stdbool.h>

[#]include <stdint.h>

[#]include <stdio.h>

[#]include <string.h>

typedef uint_least32_t u32x;

static u32x parse_uint(char **pp, const char *base) {

u32x n = 0, e = 1;

for (; *pp > base && isdigit((unsigned char)(*pp)[-1]); (*pp)--) {

    n += ((*pp)[-1] - '0') * e;

    e *= 10;

}

return n;

}

int main(void) {

u32x total = 0;

bool enabled = true;

char buf[4096];

while (fgets(buf, sizeof buf, stdin)) {

    for (char *p = buf; (p = strchr(p, ')')); p++) {

        if (enabled) {

            if (p >= buf + 6 && strncmp(p - 6, "don't(", 6) == 0) {

                enabled = false;

                continue;

            }

        } else {

            if (p >= buf + 3 && strncmp(p - 3, "do(", 3) == 0) {

                enabled = true;

            }

            continue;

        }

        char *t = p;

        const u32x b = parse_uint(&t, buf);

        if (*t == ')' || t == buf || t[-1] != ',') continue;

        t--;

        const u32x a = parse_uint(&t, buf);

        if (*t == ',' || t < buf + 4 || strncmp(t - 4, "mul(", 4) != 0) continue;

        total += a * b;

    }

}

printf("%" PRIuLEAST32 "\n", total);

}

#AdventOfCode #c

=> More informations about this toot | More toots from barubary@infosec.exchange

Descendants

Written by Mina No No No on 2024-12-03 at 22:41

@barubary

Well. Now we know, what happens behind the closed doors of every regex implementation.

Excellent work!

=> More informations about this toot | More toots from mina@berlin.social

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113590730137794450
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
271.644166 milliseconds
Gemini-to-HTML Time
0.881706 milliseconds

This content has been proxied by September (3851b).