aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

5a.c (897B)


      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #define N 100
      7 #define ISNUM(c) (c >= '0' && c <= '9')
      8 #define MIN(x,y) ((x)<(y)?(x):(y))
      9 
     10 int main() {
     11 	char *buf, line[N];
     12 	int64_t i, m, ns, seed[N], next[N], r[3];
     13 
     14 	for (ns = 0, buf = fgets(line, N, stdin); *buf; buf++) {
     15 		if (!ISNUM(*buf)) continue;
     16 		next[ns++] = atoll(buf);
     17 		while (ISNUM(*buf)) buf++;
     18 	}
     19 
     20 
     21 	while ((buf = fgets(line, N, stdin)) != NULL) {
     22 		if (!ISNUM(*buf)) {
     23 			memcpy(seed, next, ns * sizeof(int64_t));
     24 			fgets(line, N, stdin); /* Discard description */
     25 			continue;
     26 		}
     27 
     28 		for (i = 0; *buf; buf++) {
     29 			r[i++] = atoll(buf);
     30 			while (ISNUM(*buf)) buf++;
     31 		}
     32 
     33 		for (i = 0; i < ns; i++)
     34 			if (seed[i] >= r[1] && seed[i] < r[1] + r[2])
     35 				next[i] = seed[i] + (r[0] - r[1]);
     36 	}
     37 
     38 	for (i = 1, m = next[0]; i < ns; i++)
     39 		m = MIN(m, next[i]);
     40 
     41 	printf("%" PRId64 "\n", m);
     42 	return 0;
     43 }