commit 061f6232d6d6b3d3c3fd1cebdb23c78273479be7 parent d25b64e41aba9e94c31c6c72c11eb97ed4a6b19e Author: Jake Bauer <jbauer@paritybit.ca> Date: Tue, 6 Dec 2022 21:10:51 -0500 Improve Day 6 solution Diffstat:
M | day6/main.c | | | 13 | ++++--------- |
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/day6/main.c b/day6/main.c @@ -27,24 +27,19 @@ main (void) for (int i = 0; i < NUM_CHARS; i++) window[i] = line[i]; - // Then check if they are unique and slide the window along the text + // Then check if they are unique and test the rest of the text for (int i = NUM_CHARS; i <= linelen; i++) { for (int j = 0; j < NUM_CHARS; j++) - for (int k = 0; k < NUM_CHARS; k++) - if (k == j) - continue; - else if (window[j] == window[k]) + for (int k = j+1; k < NUM_CHARS; k++) + if (window[j] == window[k]) goto next_char; printf("A marker appears after %d characters.\n", i); break; next_char: - for (int i = 0; i < NUM_CHARS-1; i++) - window[i] = window[i+1]; - window[NUM_CHARS-1] = line[i]; - + window[i % NUM_CHARS] = line[i]; } free(line);