this repo has no description

day 2, part 1

+47
+47
2/main.c
··· 1 + #include <assert.h> 2 + #include <stdio.h> 3 + #include <stdlib.h> 4 + #include <string.h> 5 + #include <stdbool.h> 6 + 7 + bool is_invalid_id(const long long num) { 8 + char str[32]; 9 + sprintf(str, "%lld", num); 10 + 11 + int len = strlen(str); 12 + if (len % 2 != 0) 13 + return false; 14 + 15 + 16 + const int half = len / 2; 17 + for (int i = 0; i < half; i++) 18 + if (str[i] != str[i + half]) return false; 19 + 20 + return true; 21 + } 22 + 23 + int main(int argc, char **argv) { 24 + assert(argc == 2); 25 + 26 + FILE *file = fopen(argv[1], "r"); 27 + assert(file != NULL); 28 + 29 + char *line = NULL; 30 + size_t len = 0; 31 + getline(&line, &len, file); 32 + fclose(file); 33 + 34 + long long total = 0; 35 + char *token = strtok(line, ","); 36 + 37 + while (token != NULL) { 38 + long long start, end; 39 + if (sscanf(token, "%lld-%lld", &start, &end) == 2) 40 + for (long long id = start; id <= end; id++) if (is_invalid_id(id)) total += id; 41 + 42 + token = strtok(NULL, ","); 43 + } 44 + 45 + free(line); 46 + printf("Part 1: %lld\n", total); 47 + }