-
Notifications
You must be signed in to change notification settings - Fork 0
/
a13.c
111 lines (107 loc) · 2.94 KB
/
a13.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LINE_LENGTH 2048
#define LINE_LIMIT 500
#define SINGLE_LIST_FLAG 64
#define CHECK_PTR(p) if (!p) { puts("Error allocating memory"); return 1; }
#define D_A "[[2]]"
#define D_B "[[6]]"
void next(char **p) {
if (isdigit(**p)) {
do {
(*p)++;
} while (isdigit(**p));
} else {
(*p)++;
}
if (**p == ',') {
(*p)++;
}
}
int compare(const void *a, const void *b) {
char leftbuffer[LINE_LENGTH];
char rightbuffer[LINE_LENGTH];
strcpy(leftbuffer, *(const char **)a);
strcpy(rightbuffer, *(const char **)b);
char *lefthead = leftbuffer;
char *righthead = rightbuffer;
while (1) {
if (*lefthead == ']' && *righthead == ']') {
next(&lefthead);
next(&righthead);
} else if (*lefthead == ']') {
return -1;
} else if (*righthead == ']') {
return 1;
} else if (*lefthead == '[' && *righthead == '[') {
next(&lefthead);
next(&righthead);
} else if (*lefthead == '[') {
next(&lefthead);
*righthead |= SINGLE_LIST_FLAG;
} else if (*righthead == '[') {
*lefthead |= SINGLE_LIST_FLAG;
next(&righthead);
} else {
short leftint;
short rightint;
char leftsl = *lefthead & SINGLE_LIST_FLAG;
char rightsl = *righthead & SINGLE_LIST_FLAG;
*lefthead &= SINGLE_LIST_FLAG - 1;
*righthead &= SINGLE_LIST_FLAG - 1;
sscanf(lefthead, "%hd", &leftint);
sscanf(righthead, "%hd", &rightint);
if (leftint != rightint) {
return (leftint - rightint);
}
next(&lefthead);
if (leftsl) {
*--lefthead = ']';
}
next(&righthead);
if (rightsl) {
*--righthead = ']';
}
}
}
}
int main() {
char *lines[LINE_LIMIT];
short line = 0;
while (1) {
char *newstring = malloc(LINE_LENGTH);
CHECK_PTR(newstring)
lines[line] = newstring;
if (!fgets(newstring, LINE_LENGTH, stdin)) {
strcpy(newstring, D_A);
newstring = malloc(LINE_LENGTH);
CHECK_PTR(newstring)
lines[++line] = newstring;
strcpy(newstring, D_B);
break;
}
if (*lines[line] == '\n') {
free(newstring);
} else {
line++;
}
}
qsort(lines, ++line, sizeof(*lines), compare);
unsigned int d_a = 0;
unsigned int d_b = 0;
for (short x = 0; !d_a || !d_b; x++) {
if (!strcmp(lines[x], D_A)) {
d_a = x + 1;
}
if (!strcmp(lines[x], D_B)) {
d_b = x + 1;
}
}
while (line--) {
free(lines[line]);
}
printf("%u\n", d_a * d_b);
return 0;
}