-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc9.c
59 lines (57 loc) · 1.92 KB
/
aoc9.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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define BLOCK_SIZE 25
int main() {
FILE* f = fopen("input9.txt", "r");
if (f != NULL) {
char buf[100];
int lines = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
lines++;
}
fseek(f, 0, SEEK_SET);
int *nums = (int*) calloc(lines, sizeof(int));
int i = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
nums[i] = strtol(buf, 0, 0);
bool valid = false;
if (i >= BLOCK_SIZE) {
for (int j = i - BLOCK_SIZE; j < i; j++) {
for (int k = j + 1; k < i; k++) {
if ((nums[j] + nums[k]) == nums[i]) {
valid = true;
break;
}
}
}
if (!valid) {
printf("%d in not a sum of to number from the 25 previous\n", nums[i]);
// now find the block
for (int j = 0; j < i; j++) {
int sum = nums[j];
int min = sum, max = sum;
for (int k = j + 1; k < i; k++) {
sum += nums[k];
if (nums[k] < min) {
min = nums[k];
}
if (nums[k] > max) {
max = nums[k];
}
if (sum == nums[i]) {
printf("found group %d to %d min+max = %d\n",
nums[j], nums[k], min + max);
return 0;
}
}
}
}
}
i++;
}
fclose(f);
}
return 0;
}