From 74ab0ba990f0a0bb5ec44705da90197916464578 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Thu, 27 Oct 2022 15:22:44 +0200 Subject: [PATCH] 2020 part 2 (C). --- 2020/RESULTS.txt | 5 +++ 2020/day19/aoc-c.c | 84 ++++++++++++++++++---------------------------- README.md | 2 +- 3 files changed, 39 insertions(+), 52 deletions(-) diff --git a/2020/RESULTS.txt b/2020/RESULTS.txt index 9e49c74..5a31033 100644 --- a/2020/RESULTS.txt +++ b/2020/RESULTS.txt @@ -448,6 +448,11 @@ ex2.bash : res=412 time: 0:03.37 real, 3.34 user, 0.03 sys context-switch: 19+1, page-faults: 0+11909 + +aoc-c : res=412 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+95 + ========================================= ================= day20 ================= ========================================= diff --git a/2020/day19/aoc-c.c b/2020/day19/aoc-c.c index 32b4444..10aca15 100644 --- a/2020/day19/aoc-c.c +++ b/2020/day19/aoc-c.c @@ -54,7 +54,7 @@ static void printall() printf("\n"); } for (int i = 0; i < nmesg; ++i) { - printf("%3d: %s\n", i, mesg[i].str); + printf("%3d: len=%d %s\n", i, mesg[i].len, mesg[i].str); } } @@ -108,62 +108,44 @@ static void parse() static int match(struct mesg *msg, int *pos, int rule, int depth) { struct rule *r = rules+rule; - int found = 0, ret = 0, postmp = *pos; + int found = 0, postmp, recurse = 0; char *space = " "; log_f(3, "%.*sstr=%s pos=%d rule=%d\n", depth * 2, space, msg->str, *pos, rule); - /* check for no char left ? */ - //if (!str[*pos]) { - // printf("No char left !\n"); - // found = 0; - // goto end; - //} - switch (r->type) { - case SUB: - found = 1; - log_f(3, "%.*sLEFT\n", depth * 2, space); - for (int sub = 0; sub < 3 && r->sub.rule[0][sub] >= 0; ++sub) { - if (!match(msg, pos, r->sub.rule[0][sub], depth + 1)) { - *pos = postmp; - found = 0; - break; - } - } - if (found) - ret++; - //if (found || r->sub.rule[1][0] == -1) - // goto end; - log_f(3, "%.*sRIGHT\n", depth * 2, space); - found = 1; - for (int sub = 0; sub < 3 && r->sub.rule[1][sub] >= 0; ++sub) { - if (!match(msg, pos, r->sub.rule[1][sub], depth + 1)) { - *pos = postmp; - found = 0; - goto end; - } - } - if (found) - ret++; - goto end; - case CHR: - if (rules[rule].str == msg->str[*pos]) { - (*pos)++; - found = 1; - ret++; - goto end; - } + if (r->type == CHR) + return rules[rule].str == msg->str[(*pos)++]; + for (int side = 0; side < 2; ++side) { + if (r->sub.rule[side][0] == -1) { found = 0; - goto end; + break; + } + postmp = *pos; + recurse = 0; + found = 1; + for (int sub = 0; sub < 3 && r->sub.rule[side][sub] >= 0; ++sub) { + if (*pos == msg->len) + return recurse; + if (r->sub.rule[side][sub] == rule) { + log(3, "recursing %d rule\n", rule); + recurse = 1; + } + if (!match(msg, pos, r->sub.rule[side][sub], depth + 1)) { + found = 0; + *pos = postmp; + break; + } + } + if (found) + break; } -end: /* check for exact length */ if (depth == 0 && msg->str[*pos]) { - log_f(3, "chars remaining !\n"); + log_f(3, "pos=%d len=%d chars remaining !\n", *pos, msg->len); found = 0; } log_f(3, "%.*sstr=%s pos=%d rule=%d ret=%s\n", depth * 2, space, msg->str+*pos, *pos, rule, found? "ok": "NOK"); - return !!ret; /* not reached */ + return found; /* not reached */ } static long part1() @@ -172,10 +154,10 @@ static long part1() for (int msg = 0; msg < nmesg; ++msg) { pos = 0; if (match(mesg + msg, &pos, 0, 0)) { - log(2, "%s: ok\n", mesg[msg].str); + log(2, "len=%d pos=%d %s: ok\n", mesg[msg].len, pos, mesg[msg].str); ok++; } else { - log(2, "%s: ok\n", mesg[msg].str); + log(2, "len=%d pos=%d %s: NOK\n", mesg[msg].len, pos, mesg[msg].str); } } return ok; @@ -194,10 +176,10 @@ static long part2() for (int msg = 0; msg < nmesg; ++msg) { pos = 0; if (match(mesg + msg, &pos, 0, 0)) { - printf("%s: ok\n", mesg[msg].str); + log(2, "len=%d pos=%d %s: ok\n", mesg[msg].len, pos, mesg[msg].str); ok++; } else { - printf("%s: NOK\n", mesg[msg].str); + log(2, "len=%d pos=%d %s: NOK\n", mesg[msg].len, pos, mesg[msg].str); } } return ok; @@ -230,6 +212,6 @@ int main(ac, av) parse(); printf("%s : res=%ld\n", *av, part == 1? part1(): part2()); -// printall(); + //printall(); exit (0); } diff --git a/README.md b/README.md index fe60798..4bffbf6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ - `C`: Days 1-9 #### Advent of Code 2020 - - `C`: Days 1-18, 23-25 + - `C`: Days 1-19, 23-25 - `Bash`: All (days 1-25) - `Cobol`: Day 1 (!!)