tangled
alpha
login
or
join now
shailpatels.me
/
advent_of_code_2025
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
day 2, part 1
shailpatels.me
3 months ago
991c3b28
98a1a4ee
+47
1 changed file
expand all
collapse all
unified
split
2
main.c
+47
2/main.c
···
1
1
+
#include <assert.h>
2
2
+
#include <stdio.h>
3
3
+
#include <stdlib.h>
4
4
+
#include <string.h>
5
5
+
#include <stdbool.h>
6
6
+
7
7
+
bool is_invalid_id(const long long num) {
8
8
+
char str[32];
9
9
+
sprintf(str, "%lld", num);
10
10
+
11
11
+
int len = strlen(str);
12
12
+
if (len % 2 != 0)
13
13
+
return false;
14
14
+
15
15
+
16
16
+
const int half = len / 2;
17
17
+
for (int i = 0; i < half; i++)
18
18
+
if (str[i] != str[i + half]) return false;
19
19
+
20
20
+
return true;
21
21
+
}
22
22
+
23
23
+
int main(int argc, char **argv) {
24
24
+
assert(argc == 2);
25
25
+
26
26
+
FILE *file = fopen(argv[1], "r");
27
27
+
assert(file != NULL);
28
28
+
29
29
+
char *line = NULL;
30
30
+
size_t len = 0;
31
31
+
getline(&line, &len, file);
32
32
+
fclose(file);
33
33
+
34
34
+
long long total = 0;
35
35
+
char *token = strtok(line, ",");
36
36
+
37
37
+
while (token != NULL) {
38
38
+
long long start, end;
39
39
+
if (sscanf(token, "%lld-%lld", &start, &end) == 2)
40
40
+
for (long long id = start; id <= end; id++) if (is_invalid_id(id)) total += id;
41
41
+
42
42
+
token = strtok(NULL, ",");
43
43
+
}
44
44
+
45
45
+
free(line);
46
46
+
printf("Part 1: %lld\n", total);
47
47
+
}