aoc

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

6a.c (778B)


      1 /* For part 2, use this code and change the input file */
      2 
      3 #include <inttypes.h>
      4 #include <math.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 
      8 #define N 100000
      9 #define ISNUM(c) (c >= '0' && c <= '9')
     10 
     11 int64_t readl(int64_t nums[], char *buf) {
     12 	int64_t i;
     13 	for (i = 0; *buf; buf++) {
     14 		if (!ISNUM(*buf)) continue;
     15 		nums[i++] = atoll(buf);
     16 		while (ISNUM(*buf)) buf++;
     17 	}
     18 	return i;
     19 }
     20 
     21 int main() {
     22 	char line[N];
     23 	int64_t i, n, p, D, x1, x2, t[N], d[N];
     24 
     25 	n = readl(t, fgets(line, N, stdin));
     26 	readl(d, fgets(line, N, stdin));
     27 
     28 	p = 1;
     29 	for (i = 0; i < n; i++) {
     30 		D = t[i]*t[i] - 4*d[i];
     31 		if (D < 0) p = 1;
     32 		double sq = sqrt(D);
     33 		x1 = (int64_t)floor((t[i] - sq)/2) + 1;
     34 		x2 = (int64_t)ceil((t[i] + sq)/2) - 1;
     35 		p *= x2 - x1 + 1;
     36 	}
     37 
     38 	printf("%" PRId64 "\n", p);
     39 	return 0;
     40 }