#include #include #include #define max(a,b) (a >= b ? a : b) #define START_OF_PACKET 4 #define START_OF_MESSAGE 14 #define WINDOW_SIZE max(START_OF_PACKET, START_OF_MESSAGE) 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 + (WINDOW_SIZE - START_OF_MESSAGE), START_OF_MESSAGE)) { result2 = charNum; } charNum++; } printf("Packet: %i\n", result); printf("Message: %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; }