aocc23

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

uppga.c (977B)


      1 #include "common.h"
      2 
      3 static uint64_t get_value(uint64_t start, range_t *ranges, size_t *nranges,
      4                           size_t np, size_t nlines) {
      5   size_t i, j;
      6   uint64_t cv = start;
      7   range_t ran;
      8 
      9   for (i = 0; i < np; i++) {
     10     for (j = 0; j < nranges[i]; j++) {
     11       ran = ranges[i * nlines + j];
     12       if (ran.lb <= cv && cv <= ran.ub) {
     13         cv += ran.delta;
     14         break;
     15       }
     16     }
     17   }
     18 
     19   return cv;
     20 }
     21 
     22 int main(int argc, char **argv) {
     23   char **lines;
     24   size_t nlines = readlines(&lines, "input");
     25 
     26   uint64_t min_value = UINT64_MAX;
     27   uint64_t t;
     28 
     29   size_t nranges[nlines];
     30   range_t ranges[nlines * nlines];
     31 
     32   uint64_t np = read_ranges(nranges, ranges, lines, nlines);
     33   stack_u64 starts = read_starts(lines);
     34 
     35   size_t i;
     36   for (i = 0; i < starts.nmemb; i++) {
     37     t = get_value(starts.data[i], ranges, nranges, np, nlines);
     38     if (t < min_value)
     39       min_value = t;
     40   }
     41 
     42   stack_u64_clear(&starts);
     43 
     44   printf("%llu\n", min_value);
     45 
     46   return 0;
     47 }