aocc22

Advent of Code 2022
git clone git://www.tkruger.se/aocc22.git
Log | Files | Refs | README

common.c (835B)


      1 #include "common.h"
      2 
      3 size_t count_elves(char **lines, size_t nlines) {
      4   size_t c = 0;
      5   size_t i = 0;
      6   for (i = 0; i < nlines; i++) {
      7     if (lines[i][0] == '\n') {
      8       c++;
      9     }
     10   }
     11 
     12   return c + 1;
     13 }
     14 
     15 void get_estack(stack_u64 *estack, size_t nelves, char **lines, size_t nlines) {
     16   size_t i;
     17   size_t celf = 0;
     18 
     19   if (nelves == 0)
     20     return;
     21 
     22   stack_u64_init(&estack[0]);
     23 
     24   for (i = 0; i < nlines; i++) {
     25     assert(celf < nelves);
     26 
     27     if (lines[i][0] == '\n') {
     28       celf++;
     29       stack_u64_init(&estack[celf]);
     30     } else {
     31       stack_u64_push(&estack[celf], strtoul(lines[i], NULL, 10));
     32     }
     33   }
     34 }
     35 
     36 void comp_sums(uint64_t *sums, stack_u64 *estack, size_t nelves) {
     37   size_t i, j;
     38   for (i = 0; i < nelves; i++) {
     39     sums[i] = 0;
     40     for (j = 0; j < estack[i].nmemb; j++)
     41       sums[i] += estack[i].data[j];
     42   }
     43 }