// Compile with gcc -Wall -O3 ws.c -o ws && ./ws #include // fopen(), feof(), fgets(), fclose(), printf() #include // strcpy() #include // atoi() #include // clock() #define BUFSIZE 1024 struct article { char title[BUFSIZE]; int views; } articles[10]; char line[BUFSIZE]; int column, slot, i, j, v, views; clock_t start; void findLowestSlot() { slot = -1; v = 1 << 30; for( i=0; i<10; i++ ) { if (articles[i].views < v) { slot = i; v = articles[i].views; } } } void printSorted() { for( j=0; j<10; j++ ) { v = 0; slot = 0; for( i=0; i<10; i++ ) { if( articles[i].views > v ) { v = articles[i].views; slot = i; } } printf("%s (%d)\n", articles[slot].title, articles[slot].views); articles[slot].views = 0; } printf("Query took %ld ms\n", (clock() - start) / (CLOCKS_PER_SEC / 1000)); } int main(void) { start = clock(); FILE* file = fopen("pagecounts-20141029-230000", "r"); while( !feof(file) ) { fgets(&line[0], BUFSIZE-1, file); if( line[0] != 'e' || line[1] != 'n' || line[2] != ' ' ) { continue; } column = 3; while( line[column++] != ' ' ); views = atoi(&line[column]); if( views < 500 ) { continue; } findLowestSlot(); if( slot != -1 && articles[slot].views < views ) { articles[slot].views = views; line[column-1] = '\0'; strcpy(articles[slot].title, &line[3]); } } fclose(file); printSorted(); return 0; }