-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2part1.c
More file actions
43 lines (33 loc) · 1.05 KB
/
day2part1.c
File metadata and controls
43 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE* fptr = fopen("puzzle_input.txt", "r");
char buffer[4096];
long int result = 0;
if (fptr != NULL) {
while (fgets(buffer, sizeof(buffer), fptr)) {
char* range;
char* rest = buffer;
while ((range = strtok_r(rest, ",", &rest))) {
long int start = atol(strtok(range, "-"));
long int end = atol(strtok(NULL, "-"));
for (long int i = start; i <= end; i++) {
char id[256];
sprintf(id, "%ld", i);
int length = strlen(id);
if (length % 2 != 0) {
continue;
}
if (strncmp(id, id + length / 2, length / 2) == 0) {
result += i;
}
}
}
}
fclose(fptr);
}
printf("Result: %ld\n", result);
return EXIT_SUCCESS;
}