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
|
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUF_SIZE 2000
char buf[BUF_SIZE];
int input[200];
int length;
void read_input() {
int fd = open("input.txt", O_RDONLY);
ssize_t bytes_read = read(fd, buf, BUF_SIZE-1);
if (bytes_read <= 0) {
printf("Error reading input\n");
exit(1);
}
buf[bytes_read] = '\0';
}
void parse_input() {
int offset = 0;
int done = 0;
int buf_offset = 0;
int i = 0;
char current[10];
while (!done) {
int current_offset = 0;
while (buf[buf_offset] != '\n' && buf[buf_offset] != '\0') {
current[current_offset++] = buf[buf_offset++];
}
if (buf[buf_offset] == '\0') {
done = 1;
} else {
buf_offset++;
current[current_offset] = '\0';
input[i++] = atoi(current);
length = i;
}
}
}
int int_less(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void sort_input() {
qsort(input, length, sizeof(int), int_less);
}
int bin_search(int small_pointer, int large_pointer) {
int small = input[small_pointer];
int big = input[large_pointer];
int mid = (large_pointer + small_pointer) / 2;
int sum = small + big + input[mid];
while (sum != 2020 && small_pointer < large_pointer) {
if (sum > 2020) {
large_pointer = mid - 1;
mid = (large_pointer + small_pointer) / 2;
} else {
small_pointer = mid + 1;
mid = (large_pointer + small_pointer) / 2;
}
sum = small + big + input[mid];
}
if (sum == 2020) {
return mid;
} else {
return -1;
}
}
int search_2020() {
int small_pointer = 0;
int large_pointer = length - 1;
int sum_small = input[small_pointer] + input[small_pointer + 1] + input[large_pointer];
int sum_large = input[small_pointer] + input[large_pointer] + input[large_pointer - 1];
int done = 0;
int mid;
while (sum_large != 2020 && sum_small != 2020 && small_pointer + 1 != large_pointer && !done) {
if (sum_small > 2020) {
large_pointer--;
} else if (sum_large < 2020) {
small_pointer++;
} else {
mid = bin_search(small_pointer, large_pointer);
if (mid > 0) {
done = 1;
} else {
mid = bin_search(small_pointer + 1, large_pointer);
if (mid > 0) {
done = 1;
} else {
mid = bin_search(small_pointer, large_pointer - 1);
if (mid > 0) {
done = 1;
} else {
small_pointer++;
large_pointer--;
}
}
}
}
sum_small = input[small_pointer] + input[small_pointer + 1] + input[large_pointer];
sum_large = input[small_pointer] + input[large_pointer] + input[large_pointer - 1];
}
return input[small_pointer] * input[large_pointer] * input[mid];
}
void print_input() {
for (int i = 0; i < length; i++) {
printf("%d\n", input[i]);
}
}
int main() {
read_input();
parse_input();
sort_input();
printf("%d\n", search_2020());
}
|