Files
adventofcode2022/day6/marker.c
Dobos Ádám 1ff9a240fe Day6
2022-12-10 18:50:03 +01:00

50 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WINDOW_SIZE 14
#define START_OF_PACKET 4
#define START_OF_MESSAGE 14
void shift(char*, char);
int unique(char*, int n);
int main() {
char buf[WINDOW_SIZE], c;
memset(buf, 0, WINDOW_SIZE);
int charNum = 1, result = 0, result2= 0;
while ((c = getchar()) != EOF) {
shift(buf, c);
if (charNum > START_OF_PACKET - 1 && !result && unique(buf + (WINDOW_SIZE - START_OF_PACKET), START_OF_PACKET)) {
result = charNum;
}
if (charNum > START_OF_MESSAGE - 1 && !result2 && unique(buf, START_OF_MESSAGE)) {
result2 = charNum;
}
charNum++;
}
printf("%i\n", result);
printf("%i\n", result2);
}
void shift(char* buf, char c) {
for (int i = 0; i < WINDOW_SIZE - 1; i++) {
buf[i] = buf[i+1];
}
buf[WINDOW_SIZE - 1] = c;
}
int unique(char* buf, int n) {
for(int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (buf[i] == buf[j]) {
return 0;
}
}
}
return 1;
}