A new C solution: without lookahead or backtracking! I keep a running tally of how many letters of each digit word were matched so far: https://github.com/sjmulder/aoc/blob/master/2023/c/day01.c
int main(int argc, char **argv)
{
static const char names[][8] = {"zero", "one", "two", "three",
"four", "five", "six", "seven", "eight", "nine"};
int p1=0, p2=0, i,c;
int p1_first = -1, p1_last = -1;
int p2_first = -1, p2_last = -1;
int nmatched[10] = {0};
while ((c = getchar()) != EOF)
if (c == '\n') {
p1 += p1_first*10 + p1_last;
p2 += p2_first*10 + p2_last;
p1_first = p1_last = p2_first = p2_last = -1;
memset(nmatched, 0, sizeof(nmatched));
} else if (c >= '0' && c <= '9') {
if (p1_first == -1) p1_first = c-'0';
if (p2_first == -1) p2_first = c-'0';
p1_last = p2_last = c-'0';
memset(nmatched, 0, sizeof(nmatched));
} else for (i=0; i<10; i++)
/* advance or reset no. matched digit chars */
if (c != names[i][nmatched[i]++])
nmatched[i] = c == names[i][0];
/* matched to end? */
else if (!names[i][nmatched[i]]) {
if (p2_first == -1) p2_first = i;
p2_last = i;
nmatched[i] = 0;
}
printf("%d %d\n", p1, p2);
return 0;
}