aocc23

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

common.c (985B)


      1 #include "common.h"
      2 
      3 static inline int is_digit(char x) { return '0' <= x && x <= '9'; }
      4 
      5 size_t read_ranges(size_t *nranges, range_t *ranges, char **lines,
      6                    size_t nlines) {
      7   size_t i = 2; // skip to range data
      8   size_t count = 0;
      9   uint64_t t, s, b;
     10   range_t ran;
     11   char *cp;
     12   nranges[count] = 0;
     13 
     14   while (i < nlines) {
     15     if (is_digit(lines[i][0])) {
     16       cp = sread_next_u64(&t, lines[i]);
     17       cp = sread_next_u64(&s, cp);
     18       cp = sread_next_u64(&b, cp);
     19 
     20       ran.lb = s;
     21       ran.ub = s + b - 1;
     22       ran.delta = (int64_t)t - (int64_t)s;
     23       ranges[count * nlines + nranges[count]] = ran;
     24       nranges[count]++;
     25     } else if (lines[i][0] == '\n') {
     26       count++;
     27       nranges[count] = 0;
     28     }
     29 
     30     i++;
     31   }
     32 
     33   return count + 1;
     34 }
     35 
     36 stack_u64 read_starts(char **lines) {
     37   char *cp = lines[0];
     38   uint64_t t;
     39   stack_u64 r;
     40   stack_u64_init(&r);
     41 
     42   while ((cp = sread_next_u64(&t, cp)) != NULL) {
     43     stack_u64_push(&r, t);
     44   }
     45 
     46   return r;
     47 }