2020 part 2 (C).

This commit is contained in:
2022-10-27 15:22:44 +02:00
parent 46dee29af6
commit 74ab0ba990
3 changed files with 39 additions and 52 deletions

View File

@@ -448,6 +448,11 @@ ex2.bash : res=412
time: 0:03.37 real, 3.34 user, 0.03 sys time: 0:03.37 real, 3.34 user, 0.03 sys
context-switch: 19+1, page-faults: 0+11909 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 ================= ================= day20 =================
========================================= =========================================

View File

@@ -54,7 +54,7 @@ static void printall()
printf("\n"); printf("\n");
} }
for (int i = 0; i < nmesg; ++i) { 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) static int match(struct mesg *msg, int *pos, int rule, int depth)
{ {
struct rule *r = rules+rule; struct rule *r = rules+rule;
int found = 0, ret = 0, postmp = *pos; int found = 0, postmp, recurse = 0;
char *space = " "; char *space = " ";
log_f(3, "%.*sstr=%s pos=%d rule=%d\n", depth * 2, space, msg->str, log_f(3, "%.*sstr=%s pos=%d rule=%d\n", depth * 2, space, msg->str,
*pos, rule); *pos, rule);
/* check for no char left ? */ if (r->type == CHR)
//if (!str[*pos]) { return rules[rule].str == msg->str[(*pos)++];
// printf("No char left !\n"); for (int side = 0; side < 2; ++side) {
// found = 0; if (r->sub.rule[side][0] == -1) {
// 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;
}
found = 0; 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 */ /* check for exact length */
if (depth == 0 && msg->str[*pos]) { 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; found = 0;
} }
log_f(3, "%.*sstr=%s pos=%d rule=%d ret=%s\n", log_f(3, "%.*sstr=%s pos=%d rule=%d ret=%s\n",
depth * 2, space, msg->str+*pos, *pos, rule, found? "ok": "NOK"); depth * 2, space, msg->str+*pos, *pos, rule, found? "ok": "NOK");
return !!ret; /* not reached */ return found; /* not reached */
} }
static long part1() static long part1()
@@ -172,10 +154,10 @@ static long part1()
for (int msg = 0; msg < nmesg; ++msg) { for (int msg = 0; msg < nmesg; ++msg) {
pos = 0; pos = 0;
if (match(mesg + msg, &pos, 0, 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++; ok++;
} else { } 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; return ok;
@@ -194,10 +176,10 @@ static long part2()
for (int msg = 0; msg < nmesg; ++msg) { for (int msg = 0; msg < nmesg; ++msg) {
pos = 0; pos = 0;
if (match(mesg + msg, &pos, 0, 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++; ok++;
} else { } 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; return ok;
@@ -230,6 +212,6 @@ int main(ac, av)
parse(); parse();
printf("%s : res=%ld\n", *av, part == 1? part1(): part2()); printf("%s : res=%ld\n", *av, part == 1? part1(): part2());
// printall(); //printall();
exit (0); exit (0);
} }

View File

@@ -8,7 +8,7 @@
- `C`: Days 1-9 - `C`: Days 1-9
#### Advent of Code 2020 #### Advent of Code 2020
- `C`: Days 1-18, 23-25 - `C`: Days 1-19, 23-25
- `Bash`: All (days 1-25) - `Bash`: All (days 1-25)
- `Cobol`: Day 1 (!!) - `Cobol`: Day 1 (!!)