#include #include #include #include typedef struct LanternFishListItem LanternFishListItem; struct LanternFishListItem { unsigned int daysToSpawn; LanternFishListItem *next; }; void printFrom( LanternFishListItem *first ){ printf("%i,", first->daysToSpawn); if ( first->next != NULL ) printFrom(first->next); } unsigned int countFrom( LanternFishListItem *current ){ unsigned int count = 1; while ( current->next != NULL ){ count++; current = current->next; } return count; } int main( int argc, char *argv[] ){ if( argc == 2 ) { FILE *fp=fopen(argv[1], "r"); LanternFishListItem *first = NULL; LanternFishListItem *previous = NULL; unsigned int no; char ch; while ( fscanf( fp, "%i%c", &no, &ch ) ){ LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem)); lf->next = NULL; lf->daysToSpawn = no; if ( first == NULL ){ first = lf; } else { previous->next = lf; } previous = lf; if ( ch == '\n' ) break; } for ( unsigned int i = 0; i < 80; i++ ){ unsigned int toAdd=0; LanternFishListItem *current = first; while (current != NULL){ if ( current->daysToSpawn == 0 ){ toAdd++; current->daysToSpawn = 7; } current->daysToSpawn--; if ( current->next != NULL ) current = current->next; else break; } previous = current; //printf("On day %i add %i fish\n", i, toAdd); for (unsigned int j = 0; j < toAdd; j++){ LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem)); lf->next = NULL; lf->daysToSpawn = 8; previous->next = lf; previous = lf; } //printf("After %3i day: ", i+1); //printFrom(first); //printf("\n"); } printf("There are %i Fish.\n",countFrom(first)); fclose(fp); return 0; } else { printf("You need to provide a file\n"); return 1; } }