-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.c
92 lines (67 loc) · 1.71 KB
/
5.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
// Compile with :
// nasm -o asm.o -f elf64 5.asm && gcc -static -o program 5.c asm.o
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
extern int getPos(const char *val, int len, int high, char upper);
// int getPos(const char *val, int len, int high, char upper) {
// int low = 0;
// for (int i = 0; i < len; ++i) {
// int mid = (high + low) / 2;
// if (val[i] == upper)
// low = mid + 1;
// else
// high = mid;
// }
// return low;
// }
extern int getId(const char *val);
// int getId(const char *val) {
// int row = getPos(val, 7, 127, 'B');
// int col = getPos(val + 7, 3, 7, 'R');
// return row * 8 + col;
// }
extern int part1(FILE *fin);
// int cpart1(FILE *fin) {
// int mx = 0;
// char buf[16];
// do {
// fgets(buf, 16, fin);
// if (feof(fin))
// break;
// int id = getId(buf);
// mx = id > mx ? id : mx;
// } while (1);
// return mx;
// }
int intcmp(const void *a, const void *b) {
return *(const int*)a > *(const int*)b;
}
#define NIDS 761
int part2(FILE *fin) {
char buf[16];
int *ids = malloc(4 * NIDS);
int idsptr = 0;
do {
fgets(buf, 16, fin);
if (feof(fin))
break;
int id = getId(buf);
ids[idsptr++] = id;
} while (1);
// Sort ids
qsort(ids, NIDS, 4, intcmp);
for (int i = 0; i < NIDS - 1; ++i)
if (ids[i + 1] - ids[i] != 1)
return ids[i] + 1;
free(ids);
// Error
return -1;
}
int main() {
// int result1 = part1(stdin);
int result2 = part2(stdin);
// printf("Part 1 : %d\n", result1);
printf("Part 2 : %d\n", result2);
return 0;
}