#include #include #include int main(void) { int counts[9] = {0}; /* Wow, the parser is more complex than the algorithm :( */ char *line = NULL; size_t linesize = 0; getline(&line, &linesize, stdin); char *p; for (p = strtok(line, ","); p; p = strtok(NULL, ",")) { counts[atoi(p)]++; } free(line); /* Phew, made it */ for (int i = 0; i < 80; i++) { /* Rather than pop & append, just edit in place and rotate * the start index in use; i.e. on day 2, idx 1 is '0'. * * In fact, really all we need to do is add the current * "populating fish" (mod 0) to the 7th index (which will * be the 6th index on the next "lifecycle". * */ counts[(i + 7) % 9] += counts[i % 9]; } int sum = 0; for (int i = 0; i < 9; i++) { sum += counts[i]; } printf("%d\n", sum); return 0; }