-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-12.c
131 lines (102 loc) · 2.74 KB
/
day-12.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// cJSON is licensed under MIT License. See: https://github.com/DaveGamble/cJSON
#include "utils/cJSON.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int get_sum(cJSON* json) {
int sum = 0;
if (json->type == cJSON_Object) {
cJSON* child = json->child;
while (child != NULL) {
if (child->valuestring != NULL &&
strcmp(child->valuestring, "red") == 0) {
return 0;
}
child = child->next;
}
}
cJSON* child = json->child;
while (child != NULL) {
if (child->type == cJSON_Object || child->type == cJSON_Array) {
sum += get_sum(child);
} else if (child->type == cJSON_Number) {
sum += child->valueint;
}
child = child->next;
}
return sum;
}
int main(void) {
printf("\n");
printf("*** Advent of Code 2015 ***\n");
printf("--- Day 12: JSAbacusFramework.io ---\n");
printf("\n");
FILE* fp = fopen("input/day-12.txt", "rb");
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
clock_t start = clock();
int sum = 0;
char buffer[8]; // This buffer should be large enough.
int len = 0;
char c;
do {
c = fgetc(fp);
if ((c >= '0' && c <= '9') || c == '-') {
buffer[len++] = c;
} else if (strlen(buffer) > 0) {
buffer[len] = '\0';
sum += atoi(buffer);
memset(buffer, 0, sizeof(buffer));
len = 0;
}
} while (c != EOF);
printf("Part 1: the sum of the numbers in the file is %d\n", sum);
clock_t end = clock();
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Part 1 ran for %f seconds\n", elapsed);
printf("\n");
rewind(fp);
start = clock();
int ret = fseek(fp, 0, SEEK_END);
if (ret) {
perror("seeking the end of the file failed");
return EXIT_FAILURE;
}
len = ftell(fp);
if (len < -1) {
perror("getting the file position indicator failed");
return EXIT_FAILURE;
}
ret = fseek(fp, 0, SEEK_SET);
if (ret) {
perror("seeking the beginning of the file failed");
return EXIT_FAILURE;
}
char* jsonbuf = (char*)malloc(len);
if (!jsonbuf) {
perror("allocating the JSON buffer failed");
return EXIT_FAILURE;
}
ret = fread(jsonbuf, 1, len, fp);
if (ret != len && ferror(fp)) {
perror("reading the file into the buffer failed");
return EXIT_FAILURE;
}
cJSON* json = cJSON_Parse(jsonbuf);
if (json == NULL) {
printf("error parsing JSON: %s\n", cJSON_GetErrorPtr());
return EXIT_FAILURE;
}
sum = get_sum(json);
cJSON_Delete(json);
fclose(fp);
printf("Part 2: the sum of the numbers is %d\n", sum);
end = clock();
elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Part 2 ran for %f seconds\n", elapsed);
return EXIT_SUCCESS;
}