a.c (956B) [raw]
1 #include <string.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main(void) { 6 int counts[9] = {0}; 7 8 /* Wow, the parser is more complex than the algorithm :( */ 9 char *line = NULL; 10 size_t linesize = 0; 11 getline(&line, &linesize, stdin); 12 char *p; 13 for (p = strtok(line, ","); p; p = strtok(NULL, ",")) { 14 counts[atoi(p)]++; 15 } 16 free(line); 17 18 /* Phew, made it */ 19 for (int i = 0; i < 80; i++) { 20 /* Rather than pop & append, just edit in place and rotate 21 * the start index in use; i.e. on day 2, idx 1 is '0'. 22 * 23 * In fact, really all we need to do is add the current 24 * "populating fish" (mod 0) to the 7th index (which will 25 * be the 6th index on the next "lifecycle". 26 * */ 27 counts[(i + 7) % 9] += counts[i % 9]; 28 } 29 30 int sum = 0; 31 for (int i = 0; i < 9; i++) { 32 sum += counts[i]; 33 } 34 printf("%d\n", sum); 35 36 return 0; 37 }