aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

b.c (1132B) [raw]


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