aoc

Thing1's 2025 aoc
Log | Files | Refs

2a.c (856B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <stdbool.h>
      4 #include <string.h>
      5 
      6 typedef struct range {
      7 	long long int upper, lower;
      8 } range;
      9 
     10 bool
     11 readrange(FILE *in, range *r) {
     12 	fscanf(in, "%lld-%lld", &r->lower, &r->upper);
     13 	return (getc(in) == ',') ? 1 : 0;
     14 }
     15 
     16 bool
     17 isvalid(long long int i) {
     18 	char str[20] = {0}, str2[20] = {0};
     19 	int mid = 0;
     20 
     21 	snprintf(str, 20, "%lld", i);
     22 
     23 	mid = strlen(str) / 2;
     24 	if ((strlen(str) % 2) != 0)
     25 		mid++;
     26 
     27 	memcpy(str2, str, mid);
     28 	return (memcmp(str2, str + mid, mid) != 0);
     29 }
     30 
     31 long long int
     32 checkrange(range r) {
     33 	long long int total = 0;
     34 	for (long long int i = r.lower; i <= r.upper; i++) {
     35 		if (!isvalid(i)) total += i;
     36 	}
     37 	return total;
     38 }
     39 
     40 int
     41 main() {
     42 	range r;
     43 	long long int total = 0;
     44 
     45 	while (readrange(stdin, &r))
     46 		total += checkrange(r);
     47 	total += checkrange(r);
     48 
     49 	printf("%lld\n", total);
     50 	return 0;
     51 }