-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
112 lines (93 loc) · 2.99 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
struct Line* get_lines(const char* filename) {
FILE* fh = fopen(filename, "r");
if (!fh) {
fprintf(stderr, "failed to open %s file for reading", filename);
return NULL;
}
const size_t buffLen = 255;
char buff[buffLen];
struct Line* head = NULL;
struct Line* current_node = NULL;
while (fgets(buff, buffLen, fh)) {
struct Line* new_node = (struct Line*) malloc(sizeof(struct Line));
new_node->len = strlen(buff);
new_node->data = malloc(sizeof(char) * new_node->len);
strncpy((char*) new_node->data, buff, new_node->len - 1);
((char*) new_node->data)[new_node->len] = '\0';
if (current_node) {
current_node->next = new_node;
}
current_node = new_node;
if (!head) {
head = current_node;
}
}
fclose(fh);
fh = NULL;
return head;
}
struct Pair* extract_pairs(struct Line* lines) {
struct Pair* head = NULL;
struct Pair* current_pair = NULL;
struct Line* current_line = lines;
while (current_line) {
struct Pair* new_pair = (struct Pair*) malloc(sizeof(struct Pair));
sscanf(current_line->data, "%d-%d,%d-%d",
&new_pair->left.start, &new_pair->left.end,
&new_pair->right.start, &new_pair->right.end
);
if (current_pair) {
current_pair->next = new_pair;
}
current_pair = new_pair;
if (!head) {
head = current_pair;
}
/* clean-up lines */
free((void*) current_line->data);
current_line->len = 0;
struct Line* next = current_line->next;
free(current_line);
current_line = next;
}
return head;
}
unsigned int count_contained_pairs(struct Pair* pairs) {
unsigned int overlaping = 0;
struct Pair* pair = pairs;
while (pair) {
if (pair->left.start >= pair->right.start && pair->left.end <= pair->right.end) {
overlaping += 1;
} else if (pair->right.start >= pair->left.start && pair->right.end <= pair->left.end) {
overlaping += 1;
}
pair = pair->next;
}
return overlaping;
}
unsigned int count_any_overlapping_pairs(struct Pair* pairs) {
unsigned int overlaping = 0;
struct Pair* pair = pairs;
while (pair) {
if ((pair->left.start < pair->right.start && pair->left.end < pair->right.start) ||
(pair->right.start < pair->left.start && pair->right.end < pair->left.start)
) {
pair = pair->next;
continue;
}
overlaping += 1;
pair = pair->next;
}
return overlaping;
}
int main(int argc, const char** argv) {
struct Line* lines = get_lines(argv[1]);
struct Pair* pairs = extract_pairs(lines);
printf("contained pairs: %d\n", count_contained_pairs(pairs));
printf("overlaping pairs: %d\n", count_any_overlapping_pairs(pairs));
return 0;
}