-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeday5.c
More file actions
40 lines (36 loc) · 883 Bytes
/
adventofcodeday5.c
File metadata and controls
40 lines (36 loc) · 883 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
struct range_node {
ulong min;
ulong max;
struct range_node *next;
};
int main(void) {
struct range_node *head = malloc(sizeof(struct range_node));
struct range_node *current = head;
FILE *file = fopen("input", "r");
char *line = malloc(500);
while (fgets(line, 500, file)) {
if (line[0] == '\n') {
break;
}
sscanf(line, "%lu-%lu", ¤t->min, ¤t->max);
current->next = malloc(sizeof(struct range_node));
current = current->next;
}
current->next = NULL;
int sum = 0;
ulong i;
while (fscanf(file, "%lu", &i) != EOF) {
for (struct range_node *node = head; node != NULL; node = node->next) {
if (i >= node->min && i <= node->max) {
sum++;
break;
}
}
}
printf("Sum is: %d\n", sum);
fclose(file);
}