summaryrefslogtreecommitdiff
path: root/2020/09/part1.c
blob: d17fb2baeaef46a25aaca620bb539e8f24fc1d59 (plain)
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
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#define INPUT_LEN 1000
#define SEARCH_LEN 25

bool has_sum(uint64_t *cur_num)
{
    uint64_t search = *cur_num;
    uint64_t *p1 = cur_num - SEARCH_LEN;
    do {
        uint64_t *p2 = cur_num - SEARCH_LEN;
        do {
            if (*p1 + *p2 == search) {
                return true;
            }
        } while (++p2 < cur_num);
    } while (++p1 < cur_num);

    return false;
}

int exe_program(const char *filename)
{
    FILE *file = fopen(filename, "r");

    // Include space for newline and string terminator
    char buffer[24] = { 0 };

    uint64_t nums[INPUT_LEN];
    size_t num_size = 0;

    while (fgets(buffer, 24, file)) {
        nums[num_size++] = strtoull(buffer, NULL, 10);
    }

    fclose(file);

    uint64_t *cur_num = nums + SEARCH_LEN;
    do {
        if (!has_sum(cur_num)) {
            return *cur_num;
        }
    } while (++cur_num < nums + num_size);

    return -1;
}

int main(int argc, char *argv[])
{
    printf("%i\n", exe_program(argv[argc - 1]));
}