diff options
| author | Bond_009 <bond.009@outlook.com> | 2020-12-09 10:24:38 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2020-12-09 10:24:38 +0100 |
| commit | d1aefe3ee5b1c38bc00262670fb67b1362fbc61d (patch) | |
| tree | 950591ed24e450de23e0cd3939eb3a0aecbc4a98 /9/part1.c | |
| parent | 210ef0b7b90d685a248d5ed493ee502f78524085 (diff) | |
Add day 9
Diffstat (limited to '9/part1.c')
| -rw-r--r-- | 9/part1.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/9/part1.c b/9/part1.c new file mode 100644 index 0000000..a89fa0c --- /dev/null +++ b/9/part1.c @@ -0,0 +1,56 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <malloc.h> +#include <string.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 = malloc(INPUT_LEN * sizeof(uint64_t)); + 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])); +} |
