day 06 - small optimization & readability improvements

This commit is contained in:
yosh 2022-12-06 10:33:34 -05:00
parent f4d1597de6
commit 43b5a6c6d2
1 changed files with 4 additions and 4 deletions

8
06.c
View File

@ -10,11 +10,11 @@ int main()
int ind = 0;
int i, j;
char s[CHARS] = { 0 }; // yes this takes into account terminating null byte, buffer of chars - 1 chars
for (i = CHARS; i > 0; i--) {
char s[CHARS] = { 0 }; // only need a CHARS-1 buffer, not CHARS
for (i = 0; i < CHARS; i++) {
c = getchar();
if ((p = strchr(s,c)) != NULL)
i = (i > CHARS-(p-s)) ? i : CHARS-(p-s);
if ((p = strchr(s,c)) && i > p-s)
i = p-s;
// shift seen chars
for (j = CHARS - 2; j > 0; j--) // - 2 for null byte
s[j] = s[j - 1];