Files

51 lines
1.2 KiB
C
Raw Permalink Normal View History

2022-12-10 18:50:03 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-12-10 18:54:10 +01:00
#define max(a,b) (a >= b ? a : b)
2022-12-10 18:50:03 +01:00
#define START_OF_PACKET 4
#define START_OF_MESSAGE 14
2022-12-10 18:54:10 +01:00
#define WINDOW_SIZE max(START_OF_PACKET, START_OF_MESSAGE)
2022-12-10 18:50:03 +01:00
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;
}
2022-12-10 18:54:10 +01:00
if (charNum > START_OF_MESSAGE - 1 && !result2 && unique(buf + (WINDOW_SIZE - START_OF_MESSAGE), START_OF_MESSAGE)) {
2022-12-10 18:50:03 +01:00
result2 = charNum;
}
charNum++;
}
2022-12-10 18:54:10 +01:00
printf("Packet: %i\n", result);
printf("Message: %i\n", result2);
2022-12-10 18:50:03 +01:00
}
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;
}